command: Fix except declaration for get_comedi_cmd_pointer
[pycomedi.git] / pycomedi / constant.pyx
index 1fed075e8e6cbd5dda0f2c5e98e78ba434bd7b9b..72788b0f533b012d2a0f61acef46ff203f3e41cd 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2011-2012 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2011-2012 W. Trevor King <wking@tremily.us>
 #
 # This file is part of pycomedi.
 #
@@ -91,10 +91,17 @@ False
 >>> TRIG_SRC.now <= 2
 True
 >>> TRIG_SRC.now < 3
-False
+True
 >>> TRIG_SRC.now > None
 True
 
+The ``UNIT`` constant treats ``RF_EXTERNAL`` as a full-fledged unit:
+
+>>> UNIT.index_by_name('external')
+<_NamedInt external>
+>>> UNIT.index_by_value(_comedi.RF_EXTERNAL)
+<_NamedInt external>
+
 .. [#ops] See `emulating numeric types`_ and `NotImplementedError` in
    `the standard type hierarchy`_.
 
@@ -110,7 +117,7 @@ import sys as _sys
 import numpy as _numpy
 import comedi as _comedi
 
-from pycomedi import LOG as _LOG
+from . import LOG as _LOG
 
 
 def bitwise_value(object):
@@ -191,6 +198,9 @@ cdef class BitwiseOperator (object):
         else:
             raise ValueError(op)
 
+    def __reduce__(self):
+        return (BitwiseOperator, (self.value,))
+
 
 class _NamedInt (BitwiseOperator):
     "A flag or enum item."
@@ -205,6 +215,9 @@ class _NamedInt (BitwiseOperator):
     def __repr__(self):
         return '<%s %s>' % (self.__class__.__name__, self.name)
 
+    def __reduce__(self):
+        return (_NamedInt, (self.name, self.value, self.doc))
+
 
 class _Enum (list):
     "An enumerated list"
@@ -218,7 +231,12 @@ class _Enum (list):
             translation = {}
         self._name_keys = {}
         self._value_keys = {}
-        for attr in dir(_comedi):
+        mod = _comedi
+        if hasattr(mod, 'wrapped'):
+            mod = mod.wrapped
+            if prefix.startswith('COMEDI_'):
+                prefix = prefix[len('COMEDI_'):]
+        for attr in dir(mod):
             if attr.startswith(prefix):
                 item_name = self._item_name(attr, prefix, translation)
                 if self._is_ignored(item_name, blacklist, whitelist):
@@ -239,6 +257,8 @@ class _Enum (list):
 
     def _add_item(self, attr, item_name):
         item_value = getattr(_comedi, attr)
+        if not isinstance(item_value, int):
+            return  # item_name collided with another object, skip this non-int
         if item_value < 0:
             _LOG.debug('big value for {0:s}: {1:d} ({1:b}) converted to {2:d} ({2:b})'.format(
                     attr, item_value, (1<<32) + item_value))
@@ -256,7 +276,6 @@ class _Enum (list):
         self._value_keys[item.value] = item
         setattr(self, item.name, item)
 
-
     def index_by_name(self, name):
         return self._name_keys[name]
 
@@ -437,6 +456,11 @@ SUPPORT_LEVEL = _Enum('support_level', 'COMEDI_', whitelist=[
 
 UNIT = _Enum('unit', 'UNIT_', translation={'mA':'mA'})
 # The mA translation avoids lowercasing to 'ma'.
+UNIT.append(_NamedInt(
+        name='external',
+        value=_comedi.RF_EXTERNAL,
+        doc=('RF_EXTERNAL (value unit is defined by an external reference '
+             'channel)')))
 
 CALLBACK = _Enum('callback_flags', 'COMEDI_CB_', blacklist=['block', 'eobuf'])
 CALLBACK.eos.doc += ' (end of scan)'