Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.

Thu, 28 Feb 2008 18:15:59 +0100

author
David Douard <david.douard@logilab.fr>
date
Thu, 28 Feb 2008 18:15:59 +0100
changeset 59
d8bd4a931516
parent 58
98a94ec5f24e
child 60
5297b8707096

Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.

pygpibtoolkit/gpibcontroller.py file | annotate | diff | comparison | revisions
--- a/pygpibtoolkit/gpibcontroller.py	Thu Feb 28 18:13:55 2008 +0100
+++ b/pygpibtoolkit/gpibcontroller.py	Thu Feb 28 18:15:59 2008 +0100
@@ -13,10 +13,18 @@
 
 class AbstractGPIBDeviceMetaclass(type):
     def __new__(mcs, name, bases, classdict):
+        # Automatically add commands to Device classes.
+        # Commands which name starts with '_' are ignored.
+        # Only commands defined in the same module as the device
+        # are added to the device.
+        # Commands are descriptors (see pygpib.py)
         new_cls = type.__new__(mcs, name, bases, classdict)
-        module = sys.modules[new_cls.__module__]
-        for pname, param in module.__dict__.items():            
-            if isclass(param) and issubclass(param, AbstractCommand) and param.__module__ == new_cls.__module__:
+        modname = new_cls.__module__
+        module = sys.modules[modname]
+        for pname, param in module.__dict__.items():
+            if not pname.startswith('_') and isclass(param) and \
+                    issubclass(param, AbstractCommand) and \
+                    param.__module__ == modname:
                 setattr(new_cls, pname, param())
         return new_cls
 
@@ -33,24 +41,29 @@
         self._idn = idn
         self._address = address
         self._controller = controller
-
+        self._use_cache = True
+        
         self._cache = {}
         for pname, param in self.__class__.__dict__.items():
             if isinstance(param, AbstractCommand) and not param._readonly:
                 self._cache[pname] = param._init_value
+    def use_cache(self, use=None):
+        if use is None:
+            return self._use_cache
+        self._use_cache = bool(use)
         
     def _get(self, name):
+        if self._use_cache and name in self._cache and self._cache[name] is not None:
+            return self._cache[name]
+        
+        param = getattr(self.__class__, name) 
+        cmd = param.build_get_cmd()
+        value = self._controller.send_command(self._address, cmd)
+        value = param.convert_from(value)
         if name in self._cache:
-            if self._cache[name] is None:
-                # where to get the info?
-                # ... compute value
-                param = getattr(self.__class__, name) 
-                cmd = param.build_get_cmd()
-                value = self._controller.send_command(self._address, cmd)
-                value = param.convert_from(value)
-                self._cache[name] = value
-            return self._cache[name]
-
+            self._cache[name] = value
+        return value
+            
     def _set(self, name, value):
         param = getattr(self.__class__, name) 
         cmd = param.build_set_cmd(value)

mercurial