]> sigrok.org Git - sigrok-meter.git/commitdiff
Better driver string handling and better error messages.
authorJens Steinhauser <redacted>
Wed, 19 Nov 2014 15:04:14 +0000 (16:04 +0100)
committerJens Steinhauser <redacted>
Wed, 19 Nov 2014 15:04:14 +0000 (16:04 +0100)
samplingthread.py
sigrok-meter

index 708f99d6710c811875bb70d3399c5454c9ab7ddf..d04d120e5e7eb097d56fc0e74b2d5332c3b7056d 100644 (file)
@@ -20,6 +20,7 @@
 ##
 
 import qtcompat
+import re
 import sigrok.core as sr
 
 QtCore = qtcompat.QtCore
@@ -45,16 +46,53 @@ class SamplingThread(QtCore.QObject):
 
             self.sampling = False
 
+        def parse_driverstring(self, ds):
+            '''Dissects the driver string and returns a tuple consiting of
+            the driver name and the options (as a dictionary).'''
+
+            def parse_option(k, v):
+                '''Parse the value for a single option.'''
+                try:
+                    ck = sr.ConfigKey.get_by_identifier(k)
+                except:
+                    raise ValueError('No option named "{}".'.format(k))
+
+                try:
+                    val = ck.parse_string(v)
+                except:
+                    raise ValueError(
+                        'Invalid value "{}" for option "{}"'.format(v, k))
+
+                return (k, val)
+
+            m = re.match('(?P<name>[^:]+)(?P<opts>(:[^:=]+=[^:=]+)*)$', ds)
+            if not m:
+                raise ValueError('"{}" is not a valid driver string.'.format(ds))
+
+            opts = m.group('opts').split(':')[1:]
+            opts = [tuple(kv.split('=')) for kv in opts]
+            opts = [parse_option(k, v) for (k, v) in opts]
+            opts = dict(opts)
+
+            return (m.group('name'), opts)
+
         @QtCore.Slot()
         def start_sampling(self):
             devices = []
-            for name, options in self.drivers:
+            for ds in self.drivers:
                 try:
-                    dr = self.context.drivers[name]
-                    devices.append(dr.scan(**options)[0])
-                except:
+                    (name, opts) = self.parse_driverstring(ds)
+                    if not name in self.context.drivers:
+                        raise RuntimeError('No driver called "{}".'.format(name))
+
+                    driver = self.context.drivers[name]
+                    devs = driver.scan(**opts)
+                    if not devs:
+                        raise RuntimeError('No devices found.')
+                    devices.append(devs[0])
+                except Exception as e:
                     self.error.emit(
-                        'Unable to get device for driver "{}".'.format(name))
+                        'Error processing driver string:\n{}'.format(e))
                     return
 
             self.session = self.context.create_session()
index 453284446b7618e99cf798a61c7cf079cdc9044c..72b8d49ee832d48a167aafec866ed3020789bbb9 100755 (executable)
 ##
 
 import argparse
-import re
 import sigrok.core as sr
 import sys
 import textwrap
 
-default_drivers = [('demo', {'analog_channels': 4})]
+default_drivers = ['demo:analog_channels=4']
 default_loglevel = sr.LogLevel.WARN
 
 def parse_cli():
@@ -63,17 +62,7 @@ def parse_cli():
     }
 
     if args.driver:
-        result['drivers'] = []
-        for d in args.driver:
-            m = re.match('(?P<name>[^:]+)(?P<opts>(:[^:=]+=[^:=]+)*)', d)
-            if not m:
-                sys.exit('error parsing option "{}"'.format(d))
-
-            opts = m.group('opts').split(':')[1:]
-            opts = [tuple(kv.split('=')) for kv in opts]
-            opts = dict(opts)
-
-            result['drivers'].append((m.group('name'), opts))
+        result['drivers'] = args.driver
 
     if args.loglevel != None:
         try: