From: Jens Steinhauser Date: Wed, 19 Nov 2014 15:04:14 +0000 (+0100) Subject: Better driver string handling and better error messages. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=dab07392b9369b10d459dd989dd77d1cda541c33;p=sigrok-meter.git Better driver string handling and better error messages. --- diff --git a/samplingthread.py b/samplingthread.py index 708f99d..d04d120 100644 --- a/samplingthread.py +++ b/samplingthread.py @@ -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[^:]+)(?P(:[^:=]+=[^:=]+)*)$', 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() diff --git a/sigrok-meter b/sigrok-meter index 4532844..72b8d49 100755 --- a/sigrok-meter +++ b/sigrok-meter @@ -22,12 +22,11 @@ ## 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[^:]+)(?P(:[^:=]+=[^:=]+)*)', 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: