# HG changeset patch # User David McClosky # Date 2005-06-18 01:45:05 # Node ID 3b73f0a58a54df5f239e6f5aef555f580a5bb6af # Parent 22529016c4f290693ba673d0fc6005252ac0789e 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. diff --git a/bin/gyrocontroller b/bin/gyrocontroller --- a/bin/gyrocontroller +++ b/bin/gyrocontroller @@ -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 @@ class AbstractSimpleController(SubClient 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() diff --git a/light9/Submaster.py b/light9/Submaster.py --- a/light9/Submaster.py +++ b/light9/Submaster.py @@ -187,6 +187,48 @@ class Submasters: 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()