]> sigrok.org Git - sigrok-meter.git/commitdiff
Make program run with PySide too.
authorJens Steinhauser <redacted>
Thu, 13 Nov 2014 23:24:52 +0000 (00:24 +0100)
committerUwe Hermann <redacted>
Fri, 14 Nov 2014 12:39:30 +0000 (13:39 +0100)
sigrok-meter

index d221c43c7d2083973a3bc6b0181ab621564f195c..92de89973b17bb20f30ddb438be7ca4f7cd02201 100755 (executable)
@@ -24,8 +24,6 @@
 import argparse
 import datetime
 import os.path
-import PyQt4.QtCore as QtCore
-import PyQt4.QtGui as QtGui
 import re
 import sigrok.core as sr
 import sys
@@ -34,6 +32,80 @@ import textwrap
 default_drivers = [('demo', {'analog_channels': 1})]
 default_loglevel = sr.LogLevel.WARN
 
+def parse_cli():
+    parser = argparse.ArgumentParser(
+        description='Simple sigrok GUI for multimeters and dataloggers.',
+        epilog=textwrap.dedent('''\
+            The DRIVER string is the same as for sigrok-cli(1).
+
+            examples:
+
+              %(prog)s --driver tecpel-dmm-8061-ser:conn=/dev/ttyUSB0
+
+              %(prog)s --driver uni-t-ut61e:conn=1a86.e008
+        '''),
+        formatter_class=argparse.RawDescriptionHelpFormatter)
+
+    parser.add_argument('-d', '--driver',
+        action='append',
+        help='The driver to use')
+    parser.add_argument('-l', '--loglevel',
+        type=int,
+        help='Set loglevel (5 is most verbose)')
+    parser.add_argument('--pyside',
+        action='store_true',
+        default=False,
+        help='Force use of PySide (default is to use PyQt4)')
+    args = parser.parse_args()
+
+    result = {
+        'drivers': default_drivers,
+        'loglevel': default_loglevel,
+        'pyside': args.pyside
+    }
+
+    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))
+
+    if args.loglevel != None:
+        try:
+            result['loglevel'] = sr.LogLevel.get(args.loglevel)
+        except:
+            sys.exit('error: invalid log level')
+
+    return result
+
+if __name__ == '__main__':
+    # The command line parsing and import of the Qt modules is done here,
+    # so that the modules are imported before the classes below derive
+    # from classes therein. The rest of the main function is at the bottom.
+
+    args = parse_cli()
+
+    global qt_signal
+
+    if args['pyside']:
+        from PySide import QtCore, QtGui
+        qt_signal = QtCore.Signal
+    else:
+        try:
+            from PyQt4 import QtCore, QtGui
+            qt_signal = QtCore.pyqtSignal
+        except:
+            sys.stderr.write('import of PyQt4 failed, using PySide\n')
+            from PySide import QtCore, QtGui
+            qt_signal = QtCore.Signal
+
 class SamplingThread(QtCore.QObject):
     '''A class that handles the reception of sigrok packets in the background.'''
 
@@ -41,7 +113,7 @@ class SamplingThread(QtCore.QObject):
         '''Helper class that does the actual work in another thread.'''
 
         '''Signal emitted when new data arrived.'''
-        measured = QtCore.pyqtSignal(object)
+        measured = qt_signal(object)
 
         def __init__(self, drivers, loglevel):
             super(self.__class__, self).__init__()
@@ -90,7 +162,7 @@ class SamplingThread(QtCore.QObject):
             self.thread().msleep(100)
 
     # signal used to start the worker across threads
-    _start_signal = QtCore.pyqtSignal()
+    _start_signal = qt_signal()
 
     def __init__(self, drivers, loglevel):
         super(self.__class__, self).__init__()
@@ -265,57 +337,7 @@ class SigrokMeter(QtGui.QMainWindow):
         self.lblDevName.setText(device)
         self.lblTime.setText(now)
 
-def parse_cli():
-    parser = argparse.ArgumentParser(
-        description='Simple sigrok GUI for multimeters and dataloggers.',
-        epilog=textwrap.dedent('''\
-            The DRIVER string is the same as for sigrok-cli(1).
-
-            examples:
-
-              %(prog)s --driver tecpel-dmm-8061-ser:conn=/dev/ttyUSB0
-
-              %(prog)s --driver uni-t-ut61e:conn=1a86.e008
-        '''),
-        formatter_class=argparse.RawDescriptionHelpFormatter)
-
-    parser.add_argument('-d', '--driver',
-        action='append',
-        help='The driver to use')
-    parser.add_argument('-l', '--loglevel',
-        type=int,
-        help='Set loglevel (5 is most verbose)')
-    args = parser.parse_args()
-
-    result = {
-        'drivers': default_drivers,
-        'loglevel': default_loglevel
-    }
-
-    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))
-
-    if args.loglevel != None:
-        try:
-            result['loglevel'] = sr.LogLevel.get(args.loglevel)
-        except:
-            sys.exit('error: invalid log level')
-
-    return result
-
 if __name__ == '__main__':
-    args = parse_cli()
-
     thread = SamplingThread(args['drivers'], args['loglevel'])
 
     app = QtGui.QApplication([])