changeset 289:3b73f0a58a54

Move general use gyro function get_sub to Submaster, make global Submasters object - gyro's error subs now look like normal subs since they might reload and turn into good subs. - Also, add Submaster.fullsub which helps you make subs from channel names.
author David McClosky <dmcc@bigasterisk.com>
date Sat, 18 Jun 2005 01:45:05 +0000
parents 22529016c4f2
children 50ba9ec2b17e
files bin/gyrocontroller light9/Submaster.py
diffstat 2 files changed, 45 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/bin/gyrocontroller	Sat Jun 18 04:13:46 2005 +0000
+++ b/bin/gyrocontroller	Sat Jun 18 01:45:05 2005 +0000
@@ -2,7 +2,8 @@
 # vi: syntax=python
 
 import run_local
-from light9.Submaster import Submasters, Submaster, combine_subdict
+from light9.Submaster import Submasters, Submaster, combine_subdict, \
+    get_sub_by_name
 from light9.subclient import SubClient
 import light9.Patch as Patch
 
@@ -49,25 +50,7 @@
         self.subnames = subnames
         self.refresh()
     def get_sub(self, name):
-        if name in self.submasters.get_all_sub_names():
-            return self.submasters.get_sub_by_name(name)
-
-        try:
-            val = int(name)
-            s = Submaster("#%d" % val, {val : 1.0}, temporary=True)
-            return s
-        except ValueError:
-            pass
-
-        try:
-            subnum = Patch.get_dmx_channel(name)
-            s = Submaster("'%s'" % name, {subnum : 1.0}, temporary=True)
-            return s
-        except ValueError:
-            pass
-
-        # make an error sub
-        return Submaster('%s!?' % name)
+        return get_sub_by_name(name, self.submasters)
     def refresh(self):
         # reload subs from disk
         self.submasters = Submasters()
--- a/light9/Submaster.py	Sat Jun 18 04:13:46 2005 +0000
+++ b/light9/Submaster.py	Sat Jun 18 01:45:05 2005 +0000
@@ -187,6 +187,48 @@
         return self.submasters.get(name, Submaster(name))
     __getitem__ = get_sub_by_name
 
+def fullsub(*chans):
+    """Make a submaster with chans at full."""
+    return Submaster('%r' % chans,
+        dict([(c, 1.0) for c in chans]), temporary=True)
+
+# a global instance of Submasters, created on demand
+_submasters = None
+
+def get_global_submasters():
+    """Get (and make on demand) the global instance of Submasters"""
+    global _submasters
+    if _submasters is None:
+        _submasters = Submasters()
+    return _submasters
+
+def get_sub_by_name(name, submasters=None):
+    """name is a channel or sub nama, submasters is a Submasters object.
+    If you leave submasters empty, it will use the global instance of
+    Submasters."""
+    if not submasters:
+        submasters = get_global_submasters()
+
+    if name in submasters.get_all_sub_names():
+        return submasters.get_sub_by_name(name)
+
+    try:
+        val = int(name)
+        s = Submaster("#%d" % val, {val : 1.0}, temporary=True)
+        return s
+    except ValueError:
+        pass
+
+    try:
+        subnum = Patch.get_dmx_channel(name)
+        s = Submaster("'%s'" % name, {subnum : 1.0}, temporary=True)
+        return s
+    except ValueError:
+        pass
+
+    # make an error sub
+    return Submaster('%s' % name)
+
 if __name__ == "__main__":
     Patch.reload_data()
     s = Submasters()