]> sigrok.org Git - libsigrok.git/commitdiff
python: Add a ConfigKey class.
authorMartin Ling <redacted>
Wed, 24 Apr 2013 16:47:40 +0000 (17:47 +0100)
committerUwe Hermann <redacted>
Wed, 24 Apr 2013 21:20:47 +0000 (23:20 +0200)
bindings/python/sigrok/core/classes.py

index 14781b8c031f0ec4d40a676abf74c88c4e89b7dd..2998b4126b6bf2a26a3ba9306410e5e7b18f74e9 100644 (file)
@@ -24,7 +24,7 @@ from . import lowlevel
 import itertools
 
 __all__ = ['Error', 'Context', 'Driver', 'Device', 'Session', 'Packet', 'Log',
-    'LogLevel', 'PacketType', 'Quantity', 'Unit', 'QuantityFlag']
+    'LogLevel', 'PacketType', 'Quantity', 'Unit', 'QuantityFlag', 'ConfigKey']
 
 class Error(Exception):
 
@@ -35,12 +35,6 @@ def check(result):
     if result != SR_OK:
         raise Error(result)
 
-def config_key(name):
-    if not name.lower() == name:
-        raise AttributeError
-    key_name = "SR_CONF_" + name.upper()
-    return getattr(lowlevel, key_name)
-
 def gvariant_to_python(value):
     type_string = g_variant_get_type_string(value)
     if type_string == 't':
@@ -145,10 +139,11 @@ class Device(object):
         self.struct = struct
 
     def __getattr__(self, name):
-        key = config_key(name)
+        key = getattr(ConfigKey, name.upper())
         data = new_gvariant_ptr_ptr()
         try:
-            check(sr_config_get(self.driver.struct, key, data, self.struct))
+            check(sr_config_get(self.driver.struct,
+                key.id, data, self.struct))
         except Error as error:
             if error.errno == SR_ERR_NA:
                 raise NotImplementedError(
@@ -160,11 +155,12 @@ class Device(object):
 
     def __setattr__(self, name, value):
         try:
-            key = config_key(name)
+            key = getattr(ConfigKey, name.upper())
         except AttributeError:
             super(Device, self).__setattr__(name, value)
             return
-        check(sr_config_set(self.struct, key, python_to_gvariant(value)))
+        check(sr_config_set(self.struct,
+            key.id, python_to_gvariant(value)))
 
     @property
     def vendor(self):
@@ -327,13 +323,17 @@ class QuantityFlag(EnumValue):
             mask = new_mask
         return result
 
+class ConfigKey(EnumValue):
+    pass
+
 for symbol_name in dir(lowlevel):
     for prefix, cls in [
         ('SR_LOG_', LogLevel),
         ('SR_DF_', PacketType),
         ('SR_MQ_', Quantity),
         ('SR_UNIT_', Unit),
-        ('SR_MQFLAG_', QuantityFlag)]:
+        ('SR_MQFLAG_', QuantityFlag),
+        ('SR_CONF_', ConfigKey)]:
         if symbol_name.startswith(prefix):
             name = symbol_name[len(prefix):]
             value = getattr(lowlevel, symbol_name)