make convert_from prototype accepts several arguments (to build command with several arguments)

Thu, 28 Feb 2008 18:13:55 +0100

author
David Douard <david.douard@logilab.fr>
date
Thu, 28 Feb 2008 18:13:55 +0100
changeset 58
98a94ec5f24e
parent 57
7013c4bebc7b
child 59
d8bd4a931516

make convert_from prototype accepts several arguments (to build command with several arguments)

pygpibtoolkit/pygpib.py file | annotate | diff | comparison | revisions
--- a/pygpibtoolkit/pygpib.py	Mon Feb 25 23:22:20 2008 +0100
+++ b/pygpibtoolkit/pygpib.py	Thu Feb 28 18:13:55 2008 +0100
@@ -118,57 +118,139 @@
     def build_get_cmd(self):
         return self.__class__.__name__
 
-    def build_set_cmd(self, value):
+    def build_set_cmd(self, *value):
         raise ValueError, "Can't set value for command '%s'"%self.__class__.__name__
 
-    def convert_from(self, value):
-        return value.strip()
+    def convert_from(self, *value):
+        return None
     
 class AbstractValue(Command):
     _readonly = False
     def build_get_cmd(self):
         return self.__class__.__name__ + "?"
 
-    def build_set_cmd(self, value):
+    def build_set_cmd(self, *value):
         value = self.convert_to(value)
         cmd = "%s %s"%(self.__class__.__name__, value)
         return cmd
     
-    def convert_to(self, value):
-        return str(value)
-
-    def convert_from(self, value):
-        return self._type(value.strip())
+    def convert_to(self, *value):
+        if value:
+            return str(value[0])
+        return ""
+    
+    def convert_from(self, *value):
+        if value:
+            return self._type(value[0].strip())
+        return None
     
 class BoolValue(AbstractValue):
     _type = bool
-    def convert_from(self, value):
-        return value and value.lower() in ['1','true','yes','on']
-    def convert_to(self, value):
-        return value and "1" or "0"
+    def convert_from(self, *value):
+        if value:
+            return value[0] and value[0].lower() in ['1','true','yes','on']
+        return False
+    
+    def convert_to(self, *value):
+        if value:
+            return  value[0] and "1" or "0"
+        return "" # XXX is it correct?
     
 class IntValue(AbstractValue):
     _type = int
-    def convert_from(self, value):
-        return int(float(value.strip())) 
+    def convert_from(self, *value):
+        if value:
+            # int is resturned as a string representing a float 
+            return self._type(float(value[0].strip()))
+        return None
 
+class flag(int):
+    def __new__(cls, val, constants):
+        val = int.__new__(cls, val)
+        val._constants = constants
+        for v, name, desc in constants:
+            if name not in ['', 'N/A']:
+                setattr(val, name, v)
+        return val
+    
+    def __str__(self):
+        return '%s <' + '|'.join([x[1] for x in self._constants if x[0]&self]) + ">"
+    def flags(self):
+        return [x[1] for x in self._constants if x[0] & (self and x[1]) not in ['','N/A']]
+        
+    def descriptions(self):
+        return [x[2] for x in self._constants if (x[0] & self) and x[1] not in ['','N/A']]
+
+class Flag(IntValue):
+    _readonly = True
+    _constants = []
+    _type = flag
+    def convert_from(self, *value):
+        if value:
+            return self._type(float(value[0].strip()), _constants)
+        return None
+    
 class FloatValue(AbstractValue):
     _type = float
 
 class PercentageValue(FloatValue):
     pass #TODO
 
-class FrequencyValue(FloatValue):
-    pass
+class FloatUnitValue(FloatValue):
+    """
+    A Float value with unit (frequency, etc.)
+    """
+    def convert_to(self, *value):
+        if value:
+            if len(value)==1:
+                if isinstance(value[0], basestring):
+                    value = value[0].strip().split()
+                    if len(value)==1:
+                        freq = float(value)
+                        unit = self._units[0]
+                    elif len(value)==2:
+                        freq = float(value[0])
+                        unit = value[1]
+                    else:
+                        raise ValueError, "Can't interpret %s as a %s"%(repr(value), self._name)
+                else:
+                    freq = float(value[0])
+                    unit = self._units[0]
+            elif len(value)==2:
+                freq = float(value[0])
+                unit = value[1]
+            else:
+                raise ValueError, "Can't interpret %s as a %s"%(repr(value), self._name)
+            assert unit.lower() in self._units, "Unit is not correct (%s)"%unit
+            return "%s%s"%(freq, unit)
+        return "" # XXX is it correct?
 
-class DurationValue(FloatValue):
-    pass
+class FrequencyValue(FloatUnitValue):
+    _units = ['Hz','kHz','mHz',]
+    _name = "frequency"
+
+class VoltageValue(FloatUnitValue):
+    _units = ['V','mV',]
+    _name = "voltage"
+    
+class DurationValue(FloatUnitValue):
+    _units = ['sec','msec','usec','min']
+    _name = "duration"
 
 class StringValue(AbstractValue):
     _type = str
+    def convert_to(self, *value):
+        if value:
+            return "'%s'"%str(value[0])
+        return "''"
 
-class EnumValue(AbstractValue):
-    pass
+class EnumValue(StringValue):
+    _values = []
+    def convert_to(self, *value):
+        if value:
+            assert value[0] in self._values
+            return "%s"%self._values.index(value[0])
+        return "" # XXX
 
 class Mode(AbstractValue):
     pass

mercurial