def __init__(self): RemoteObject.__init__(self) # event handling self.__events_proxy__ = EventsProxy() # configuration handling self.__config_proxy__ = Config(self) self.__state__ = State.STOPPED self.__location__ = "" # logging. # put every logger on behalf of chimera's logger so # we can easily setup levels on all our parts logName = self.__module__ if not logName.startswith("chimera."): logName = "chimera." + logName + " (%s)" % logName self.log = logging.getLogger(logName) # Hz self._Hz = 2 self._loop_abort = threading.Event() # To override metadata default values self.__metadataOverrideMethod__ = None
def test_str (self): c = Config({"key_str": "value"}) # valid for i in ("valid", 1, True, 1.0, object): assert c.__setitem__("key_str", i) != False, "%s (%s) is a valid str configuration" % (i, type(i)) assert type(c.__getitem__("key_str")) == StringType, "should return str object"
def test_range (self): c = Config({"key_range": (1, 10)}) # ranges are inclusive # valid for i in range(1, 11): assert c.__setitem__("key_range", i) != False, "%s (%s) is a valid configuration" % (i, type(i)) assert type(c.__getitem__("key_range")) == IntType, "should return int object" # invalid for i in (0, 11, "str"): assert_raises (OptionConversionException, c.__setitem__, "key_range", i)
def test_str(self): c = Config({"key_str": "value"}) # valid for i in ("valid", 1, True, 1.0, object): assert c.__setitem__( "key_str", i) != False, "%s (%s) is a valid str configuration" % (i, type(i)) assert type(c.__getitem__( "key_str")) == StringType, "should return str object"
def test_bool (self): c = Config({"key_bool": True}) # valid for i in (1, 1.0, 0, 0.0, "y", "Y", "yes", "YES", "n", "N", "no", "NO", "on", "ON", "off", "OFF", "true", "TRUE", "false", "FALSE", True, False): # no assert because setitem returns the old and this can be False # we get errors if any set raises OptionConversionException c.__setitem__("key_bool", i) assert type(c.__getitem__("key_bool")) == BooleanType, "should return bool object" # invalid for i in (object, "ok", 10): assert_raises (OptionConversionException, c.__setitem__, "key_bool", i)
def test_range(self): c = Config({"key_range": (1, 10)}) # ranges are inclusive # valid for i in range(1, 11): assert c.__setitem__( "key_range", i) != False, "%s (%s) is a valid configuration" % (i, type(i)) assert type(c.__getitem__( "key_range")) == IntType, "should return int object" # invalid for i in (0, 11, "str"): assert_raises(OptionConversionException, c.__setitem__, "key_range", i)
def __init__(self): RemoteObject.__init__(self) # event handling self.__events_proxy__ = EventsProxy() # configuration handling self.__config_proxy__ = Config(self) self.__state__ = State.STOPPED self.__location__ = "" # logging. # put every logger on behalf of chimera's logger so # we can easily setup levels on all our parts logName = self.__module__ if not logName.startswith("chimera."): logName = "chimera." + logName + " (%s)" % logName self.log = logging.getLogger(logName) # Hz self._Hz = 2 self._loop_abort = threading.Event()
def test_number (self): c = Config({"key_int": 10, "key_float": 1.0}) # valid for i in (1, 1.0, "1", "1.0", "-1", "-1.0", " 10 ", True): assert c.__setitem__("key_int", i) != False, "%s (%s) is a valid int configuration" % (i, type(i)) assert type(c.__getitem__("key_int")) == IntType, "should return int object" assert c.__setitem__("key_float", i) != False, "%s (%s) is a valid float configuration" % (i, type(i)) assert type(c.__getitem__("key_float")) == FloatType, "should return str object" # invalid for i in (object, "a10", "1.a"): assert_raises (OptionConversionException, c.__setitem__, "key_int", i) assert_raises (OptionConversionException, c.__setitem__, "key_float", i)
def test_bool(self): c = Config({"key_bool": True}) # valid for i in (1, 1.0, 0, 0.0, "y", "Y", "yes", "YES", "n", "N", "no", "NO", "on", "ON", "off", "OFF", "true", "TRUE", "false", "FALSE", True, False): # no assert because setitem returns the old and this can be False # we get errors if any set raises OptionConversionException c.__setitem__("key_bool", i) assert type(c.__getitem__( "key_bool")) == BooleanType, "should return bool object" # invalid for i in (object, "ok", 10): assert_raises(OptionConversionException, c.__setitem__, "key_bool", i)
def test_enum (self): Values = Enum("A_VALUE", "OTHER_VALUE") c = Config({"key_enum": Values.A_VALUE}) # default assert c["key_enum"] == Values.A_VALUE # valid for i in Values: assert c.__setitem__("key_enum", i) != False, "%s (%s) is a valid enum configuration" % (i, type(i)) assert type(c.__getitem__("key_enum")) == EnumValue, "should return EnumValue object" for i in ["A_VALUE", "OTHER_VALUE", "a_value", "other_value", "a_vAlUe", "oThEr_VaLuE"]: assert c.__setitem__("key_enum", i) != False, "%s (%s) is a valid enum configuration" % (i, type(i)) assert type(c.__getitem__("key_enum")) == EnumValue, "should return EnumValue object" # invalid assert_raises(KeyError, c.__getitem__, "WHATERVER")
def test_options(self): c = Config({ "key_opt_int": [1, 2, 3], "key_opt_float": [1, 2.0, 4.0], "key_opt_bool": [1, False], "key_opt_str": ["one", "two", "three"] }) # valid for i in (1, 2, 3, True): # True == 1 assert c.__setitem__( "key_opt_int", i) != False, "%s (%s) is a valid configuration" % (i, type(i)) assert type(c.__getitem__( "key_opt_int")) == IntType, "should return int object" for i in ("one", "two", "three"): assert c.__setitem__( "key_opt_str", i) != False, "%s (%s) is a valid configuration" % (i, type(i)) assert type(c.__getitem__( "key_opt_str")) == StringType, "should return str object" # invalid for i in (4, 5, 6, "str", False): assert_raises(OptionConversionException, c.__setitem__, "key_opt_int", i) for i in ("four", "five", "six", 1, 2, 3, True, False): assert_raises(OptionConversionException, c.__setitem__, "key_opt_str", i)
def test_number(self): c = Config({"key_int": 10, "key_float": 1.0}) # valid for i in (1, 1.0, "1", "1.0", "-1", "-1.0", " 10 ", True): assert c.__setitem__( "key_int", i) != False, "%s (%s) is a valid int configuration" % (i, type(i)) assert type(c.__getitem__( "key_int")) == IntType, "should return int object" assert c.__setitem__( "key_float", i) != False, "%s (%s) is a valid float configuration" % ( i, type(i)) assert type(c.__getitem__( "key_float")) == FloatType, "should return str object" # invalid for i in (object, "a10", "1.a"): assert_raises(OptionConversionException, c.__setitem__, "key_int", i) assert_raises(OptionConversionException, c.__setitem__, "key_float", i)
def test_enum(self): Values = Enum("A_VALUE", "OTHER_VALUE") c = Config({"key_enum": Values.A_VALUE}) # default assert c["key_enum"] == Values.A_VALUE # valid for i in Values: assert c.__setitem__( "key_enum", i) != False, "%s (%s) is a valid enum configuration" % ( i, type(i)) assert type(c.__getitem__( "key_enum")) == EnumValue, "should return EnumValue object" for i in [ "A_VALUE", "OTHER_VALUE", "a_value", "other_value", "a_vAlUe", "oThEr_VaLuE" ]: assert c.__setitem__( "key_enum", i) != False, "%s (%s) is a valid enum configuration" % ( i, type(i)) assert type(c.__getitem__( "key_enum")) == EnumValue, "should return EnumValue object" # invalid assert_raises(KeyError, c.__getitem__, "WHATERVER")
def test_coord(self): c = Config({"DMS": Coord.fromDMS(10), "HMS": Coord.fromHMS(10)}) assert c["DMS"].state == State.DMS assert c["HMS"].state == State.HMS c["DMS"] = 20 assert c["DMS"] == Coord.fromDMS(20) c["HMS"] = 20 assert c["HMS"] == Coord.fromHMS(20) assert c["HMS"] == Coord.fromDMS(20 * 15)
def test_options (self): c = Config({"key_opt_int": [1, 2, 3], "key_opt_float": [1, 2.0, 4.0], "key_opt_bool": [1, False], "key_opt_str": ["one", "two", "three"]}) # valid for i in (1, 2, 3, True): # True == 1 assert c.__setitem__("key_opt_int", i) != False, "%s (%s) is a valid configuration" % (i, type(i)) assert type(c.__getitem__("key_opt_int")) == IntType, "should return int object" for i in ("one", "two", "three"): assert c.__setitem__("key_opt_str", i) != False, "%s (%s) is a valid configuration" % (i, type(i)) assert type(c.__getitem__("key_opt_str")) == StringType, "should return str object" # invalid for i in (4, 5, 6, "str", False): assert_raises (OptionConversionException, c.__setitem__, "key_opt_int", i) for i in ("four", "five", "six", 1, 2, 3, True, False): assert_raises (OptionConversionException, c.__setitem__, "key_opt_str", i)
def test_iter(self): d = { "device": "/dev/ttyS0", "ccd": ["imaging", "tracking"], "exp_time": (0.1, 6000.0), "shutter": ["open", "close", "leave"], "readout_aborted": True, "readout_mode": 1, "date_format": "%d%m%y", "file_format": "$num-$observer-$date-%objname", "directory": "/home/someuser/images", "save_on_temp": False, "seq_num": 1, "observer": "observer name", "obj_name": "object name", "some_none_var": None } c = Config(d) # len assert len(c) == len(d) # contains assert "device" in c assert "stuff" not in c # iter protocol assert list(c.keys()) == list(c.keys()) assert list(c.values()) == list(c.values()) assert list(c.items()) == list(c.items()) # config using += dd = {"new": 10} assert_raises(KeyError, c.__iadd__, dd) assert not "new" in c c += {"device": "foo"} assert c["device"] == "foo" assert isinstance( c, Config) # __iadd__ protocol, return self to allow daisy chaining
def test_iter (self): d = {"device" : "/dev/ttyS0", "ccd" : ["imaging", "tracking"], "exp_time" : (0.1, 6000.0), "shutter" : ["open", "close", "leave"], "readout_aborted": True, "readout_mode" : 1, "date_format" : "%d%m%y", "file_format" : "$num-$observer-$date-%objname", "directory" : "/home/someuser/images", "save_on_temp" : False, "seq_num" : 1, "observer" : "observer name", "obj_name" : "object name", "some_none_var": None} c = Config(d) # len assert len(c) == len(d) # contains assert "device" in c assert "stuff" not in c # iter protocol assert c.keys() == c.keys() assert c.values() == c.values() assert c.items() == c.items() # config using += dd = {"new": 10} assert_raises(KeyError, c.__iadd__, dd) assert not "new" in c c += {"device":"foo"} assert c["device"] == "foo" assert isinstance (c, Config) # __iadd__ protocol, return self to allow daisy chaining
class ChimeraObject (RemoteObject, ILifeCycle): __metaclass__ = MetaObject def __init__(self): RemoteObject.__init__(self) # event handling self.__events_proxy__ = EventsProxy() # configuration handling self.__config_proxy__ = Config(self) self.__state__ = State.STOPPED self.__location__ = "" # logging. # put every logger on behalf of chimera's logger so # we can easily setup levels on all our parts logName = self.__module__ if not logName.startswith("chimera."): logName = "chimera." + logName + " (%s)" % logName self.log = logging.getLogger(logName) # Hz self._Hz = 2 self._loop_abort = threading.Event() # To override metadata default values self.__metadataOverrideMethod__ = None # config implementation def __getitem__(self, item): # any thread can read if none writing at the time lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireRead() return self.__config_proxy__.__getitem__(item) finally: lock.release() def __setitem__(self, item, value): # only one thread can write lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireWrite() return self.__config_proxy__.__setitem__(item, value) finally: lock.release() # bulk configuration (pass a dict to config multiple values) def __iadd__(self, configDict): # only one thread can write lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireWrite() self.__config_proxy__.__iadd__(configDict) finally: lock.release() return self.getProxy() # locking def __enter__(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).__enter__() def __exit__(self, *args): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).__exit__(*args) def acquire(self, blocking=True): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).acquire(blocking) def release(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).release() def wait(self, timeout=None): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).wait(timeout) def notify(self, n=1): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).notify(n) def notifyAll(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).notifyAll() # reflection def __get_events__(self): return getattr(self, EVENTS_ATTRIBUTE_NAME) def __get_methods__(self): return getattr(self, METHODS_ATTRIBUTE_NAME) def __get_config__(self): return getattr(self, CONFIG_PROXY_NAME).items() # ILifeCycle implementation def __start__(self): return True def __stop__(self): return True def getHz(self): return self._Hz def setHz(self, freq): tmpHz = self.getHz() self._Hz = freq return tmpHz def __main__(self): self._loop_abort.clear() timeslice = 0.5 runCondition = True while runCondition: runCondition = self.control() if self._loop_abort.isSet(): return True # FIXME: better idle loop # we can't sleep for the whole time because # if object set a long sleep time and Manager decides to # shutdown, we must be alseep to receive his message and # return. timeToWakeUp = 1.0 / self.getHz() sleeped = 0 while sleeped < timeToWakeUp: time.sleep(timeslice) if self._loop_abort.isSet(): return True sleeped += timeslice return True def __abort_loop__(self): self._loop_abort.set() def control(self): return False def getState(self): return self.__state__ def __setstate__(self, state): oldstate = self.__state__ self.__state__ = state return oldstate def getLocation(self): return self.__location__ def __setlocation__(self, location): location = Location(location) self.__location__ = location self.setGUID("/%s/%s" % (location.cls, location.name)) return True def getManager(self): if self.getDaemon(): return self.getDaemon().getProxyForObj(self.getDaemon().getManager()) def getProxy(self): # just to put everthing together (no need to change the base # implementation) return super(ChimeraObject, self).getProxy() def getGUID(self): return self.objectGUID def getMetadata(self, request): # Check first if there is metadata from an metadata override method. md = self.getMetadataOverride(request) if md is not None: return md # If not, just go on with the instrument's default metadata. return [] def setMetadataMethod(self, location): # Defines an alternative class to getMetadata() self.__metadataOverrideMethod__ = location def getMetadataOverride(self, request): # Returns metadata from the override class or None if there is no override getMetadata() class. if self.__metadataOverrideMethod__ is not None: return self.getManager().getProxy(self.__metadataOverrideMethod__, lazy=True).getMetadata(request) return None def features(self, interface): """ Checks if self is an instance of a interface. This is useful to check if some interface/capability is supported by an instrument :param interface: One of from chimera interfaces :return: True if is instance, False otherwise """ return isinstance(self, interface)
class ChimeraObject(RemoteObject, ILifeCycle): __metaclass__ = MetaObject def __init__(self): RemoteObject.__init__(self) # event handling self.__events_proxy__ = EventsProxy() # configuration handling self.__config_proxy__ = Config(self) self.__state__ = State.STOPPED self.__location__ = "" # logging. # put every logger on behalf of chimera's logger so # we can easily setup levels on all our parts logName = self.__module__ if not logName.startswith("chimera."): logName = "chimera." + logName + " (%s)" % logName self.log = logging.getLogger(logName) # Hz self._Hz = 2 self._loop_abort = threading.Event() # config implementation def __getitem__(self, item): # any thread can read if none writing at the time lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireRead() return self.__config_proxy__.__getitem__(item) finally: lock.release() def __setitem__(self, item, value): # only one thread can write lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireWrite() return self.__config_proxy__.__setitem__(item, value) finally: lock.release() # bulk configuration (pass a dict to config multiple values) def __iadd__(self, configDict): # only one thread can write lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireWrite() self.__config_proxy__.__iadd__(configDict) finally: lock.release() return self.getProxy() # locking def __enter__(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).__enter__() def __exit__(self, *args): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).__exit__(*args) def acquire(self, blocking=True): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).acquire(blocking) def release(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).release() def wait(self, timeout=None): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).wait(timeout) def notify(self, n=1): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).notify(n) def notifyAll(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).notifyAll() # reflection def __get_events__(self): return getattr(self, EVENTS_ATTRIBUTE_NAME) def __get_methods__(self): return getattr(self, METHODS_ATTRIBUTE_NAME) def __get_config__(self): return getattr(self, CONFIG_PROXY_NAME).items() # ILifeCycle implementation def __start__(self): return True def __stop__(self): return True def getHz(self): return self._Hz def setHz(self, freq): tmpHz = self.getHz() self._Hz = freq return tmpHz def __main__(self): self._loop_abort.clear() timeslice = 0.5 runCondition = True while runCondition: runCondition = self.control() if self._loop_abort.isSet(): return True # FIXME: better idle loop # we can't sleep for the whole time because # if object set a long sleep time and Manager decides to # shutdown, we must be alseep to receive his message and # return. timeToWakeUp = 1.0 / self.getHz() sleeped = 0 while sleeped < timeToWakeUp: time.sleep(timeslice) if self._loop_abort.isSet(): return True sleeped += timeslice return True def __abort_loop__(self): self._loop_abort.set() def control(self): return False def getState(self): return self.__state__ def __setstate__(self, state): oldstate = self.__state__ self.__state__ = state return oldstate def getLocation(self): return self.__location__ def __setlocation__(self, location): location = Location(location) self.__location__ = location self.setGUID("/%s/%s" % (location.cls, location.name)) return True def getManager(self): if self.getDaemon(): return self.getDaemon().getProxyForObj( self.getDaemon().getManager()) def getProxy(self): # just to put everthing together (no need to change the base implementation) return super(ChimeraObject, self).getProxy() def getGUID(self): return self.objectGUID def getMetadata(self, request): return []
class ChimeraObject(RemoteObject, ILifeCycle): __metaclass__ = MetaObject def __init__(self): RemoteObject.__init__(self) # event handling self.__events_proxy__ = EventsProxy() # configuration handling self.__config_proxy__ = Config(self) self.__state__ = State.STOPPED self.__location__ = "" # logging. # put every logger on behalf of chimera's logger so # we can easily setup levels on all our parts logName = self.__module__ if not logName.startswith("chimera."): logName = "chimera." + logName + " (%s)" % logName self.log = logging.getLogger(logName) # Hz self._Hz = 2 self._loop_abort = threading.Event() # To override metadata default values self.__metadataOverrideMethod__ = None # config implementation def __getitem__(self, item): # any thread can read if none writing at the time lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireRead() return self.__config_proxy__.__getitem__(item) finally: lock.release() def __setitem__(self, item, value): # only one thread can write lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireWrite() return self.__config_proxy__.__setitem__(item, value) finally: lock.release() # bulk configuration (pass a dict to config multiple values) def __iadd__(self, configDict): # only one thread can write lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireWrite() self.__config_proxy__.__iadd__(configDict) finally: lock.release() return self.getProxy() # locking def __enter__(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).__enter__() def __exit__(self, *args): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).__exit__(*args) def acquire(self, blocking=True): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).acquire(blocking) def release(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).release() def wait(self, timeout=None): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).wait(timeout) def notify(self, n=1): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).notify(n) def notifyAll(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).notifyAll() # reflection def __get_events__(self): return getattr(self, EVENTS_ATTRIBUTE_NAME) def __get_methods__(self): return getattr(self, METHODS_ATTRIBUTE_NAME) def __get_config__(self): return getattr(self, CONFIG_PROXY_NAME).items() # ILifeCycle implementation def __start__(self): return True def __stop__(self): return True def getHz(self): return self._Hz def setHz(self, freq): tmpHz = self.getHz() self._Hz = freq return tmpHz def __main__(self): self._loop_abort.clear() timeslice = 0.5 runCondition = True while runCondition: runCondition = self.control() if self._loop_abort.isSet(): return True # FIXME: better idle loop # we can't sleep for the whole time because # if object set a long sleep time and Manager decides to # shutdown, we must be alseep to receive his message and # return. timeToWakeUp = 1.0 / self.getHz() sleeped = 0 while sleeped < timeToWakeUp: time.sleep(timeslice) if self._loop_abort.isSet(): return True sleeped += timeslice return True def __abort_loop__(self): self._loop_abort.set() def control(self): return False def getState(self): return self.__state__ def __setstate__(self, state): oldstate = self.__state__ self.__state__ = state return oldstate def getLocation(self): return self.__location__ def __setlocation__(self, location): location = Location(location) self.__location__ = location self.setGUID("/%s/%s" % (location.cls, location.name)) return True def getManager(self): if self.getDaemon(): return self.getDaemon().getProxyForObj( self.getDaemon().getManager()) def getProxy(self): # just to put everthing together (no need to change the base # implementation) return super(ChimeraObject, self).getProxy() def getGUID(self): return self.objectGUID def getMetadata(self, request): # Check first if there is metadata from an metadata override method. md = self.getMetadataOverride(request) if md is not None: return md # If not, just go on with the instrument's default metadata. return [] def setMetadataMethod(self, location): # Defines an alternative class to getMetadata() self.__metadataOverrideMethod__ = location def getMetadataOverride(self, request): # Returns metadata from the override class or None if there is no override getMetadata() class. if self.__metadataOverrideMethod__ is not None: return self.getManager().getProxy(self.__metadataOverrideMethod__, lazy=True).getMetadata(request) return None def features(self, interface): """ Checks if self is an instance of a interface. This is useful to check if some interface/capability is supported by an instrument :param interface: One of from chimera interfaces :return: True if is instance, False otherwise """ return isinstance(self, interface)
class ChimeraObject (RemoteObject, ILifeCycle): __metaclass__ = MetaObject def __init__(self): RemoteObject.__init__(self) # event handling self.__events_proxy__ = EventsProxy() # configuration handling self.__config_proxy__ = Config(self) self.__state__ = State.STOPPED self.__location__ = "" # logging. # put every logger on behalf of chimera's logger so # we can easily setup levels on all our parts logName = self.__module__ if not logName.startswith("chimera."): logName = "chimera." + logName + " (%s)" % logName self.log = logging.getLogger(logName) # Hz self._Hz = 2 self._loop_abort = threading.Event() # config implementation def __getitem__(self, item): # any thread can read if none writing at the time lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireRead() return self.__config_proxy__.__getitem__(item) finally: lock.release() def __setitem__(self, item, value): # only one thread can write lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireWrite() return self.__config_proxy__.__setitem__(item, value) finally: lock.release() # bulk configuration (pass a dict to config multiple values) def __iadd__(self, configDict): # only one thread can write lock = getattr(self, RWLOCK_ATTRIBUTE_NAME) try: lock.acquireWrite() self.__config_proxy__.__iadd__(configDict) finally: lock.release() return self.getProxy() # locking def __enter__(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).__enter__() def __exit__(self, *args): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).__exit__(*args) def acquire(self, blocking=True): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).acquire(blocking) def release(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).release() def wait(self, timeout=None): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).wait(timeout) def notify(self, n=1): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).notify(n) def notifyAll(self): return getattr(self, INSTANCE_MONITOR_ATTRIBUTE_NAME).notifyAll() # reflection def __get_events__(self): return getattr(self, EVENTS_ATTRIBUTE_NAME) def __get_methods__(self): return getattr(self, METHODS_ATTRIBUTE_NAME) def __get_config__(self): return getattr(self, CONFIG_PROXY_NAME).items() # ILifeCycle implementation def __start__(self): return True def __stop__(self): return True def getHz(self): return self._Hz def setHz(self, freq): tmpHz = self.getHz() self._Hz = freq return tmpHz def __main__(self): self._loop_abort.clear() timeslice = 0.5 runCondition = True while runCondition: runCondition = self.control() if self._loop_abort.isSet(): return True # FIXME: better idle loop # we can't sleep for the whole time because # if object set a long sleep time and Manager decides to # shutdown, we must be alseep to receive his message and # return. timeToWakeUp = 1.0 / self.getHz() sleeped = 0 while sleeped < timeToWakeUp: time.sleep(timeslice) if self._loop_abort.isSet(): return True sleeped += timeslice return True def __abort_loop__(self): self._loop_abort.set() def control(self): return False def getState(self): return self.__state__ def __setstate__(self, state): oldstate = self.__state__ self.__state__ = state return oldstate def getLocation(self): return self.__location__ def __setlocation__(self, location): location = Location(location) self.__location__ = location self.setGUID("/%s/%s" % (location.cls, location.name)) return True def getManager(self): if self.getDaemon(): return self.getDaemon().getProxyForObj(self.getDaemon().getManager()) def getProxy(self): # just to put everthing together (no need to change the base # implementation) return super(ChimeraObject, self).getProxy() def getGUID(self): return self.objectGUID def getMetadata(self, request): return []