def test_keys(self): """Get out all keys of the kvs""" kvs = KVS() kvs["tv1"] = TV(1) kvs["tv2"] = TV(2) kvs["tv3"] = TV(3) print(kvs.keys())
def test_keys(self): """Get out all keys of the kvs""" kvs = KVS() kvs["tv1"] = TV(1) kvs["tv2"] = TV(2) kvs["tv3"] = TV(3) print(kvs.keys())
class DeviceRegistry(): # this is actions, so is this the 'RegistRAR'?? """A persistent registry for device class instance configurations""" DEFAULT_FILENAME = "registry.kvs" def __init__(self, filename=None): ##print("***Opening DeviceRegistry") self.store = KVS(filename) self.fsk_router = None def set_fsk_router(self, fsk_router): self.fsk_router = fsk_router def load_from(self, filename=None): """Start with a blank in memory registry, and load from the given filename""" if filename == None: filename = DeviceRegistry.DEFAULT_FILENAME # Create a new in memory store, effectively removing any existing in memory device class instances #TODO: Not good if there are routes to those class instances? self.store = KVS( filename ) #TODO: later we might make it possible to load_from multiple files self.store.load(filename, Devices.DeviceFactory.get_device_from_name) def load_into(self, context): """auto-create variables in the provided context, for all persisted registry entries""" if context == None: raise ValueError("Must provide a context to hold new variables") for name in self.store.keys(): c = self.get(name) # This creates a variable inside the context of this name, points to class instance setattr(context, name, c) def add(self, device, name): """Add a device class instance to the registry, with a friendly name""" self.store[name] = device def get(self, name): # -> Device """Get the description for a device class from the store, and construct a class instance""" c = self.store[name] if self.fsk_router != None: if c.can_send(): # if can transmit, we can receive from it if isinstance(c, Devices.MiHomeDevice): #print("Adding rx route for transmit enabled device %s" % c) address = (c.manufacturer_id, c.product_id, c.device_id) self.fsk_router.add(address, c) return c def rename(self, old_name, new_name): """Rename a device in the registry""" c = self.store[old_name] # get the class instance self.delete(old_name) # remove from memory and from any disk version self.add(c, new_name) # Add the same class back, but with the new name #Note: If rx routes are defined, they will still be correct, # because they wire directly to the device class instance def delete(self, name): """Delete the named class instance""" del self.store[name] def list(self): """List the registry in a vaguely printable format, mostly for debug""" print("REGISTERED DEVICES:") for k in self.store.keys(): print(" %s -> %s" % (k, self.store[k])) def size(self): """How many entries are there in the registry?""" return self.store.size() def devices(self): """A generator/iterator that can be used to get a list of device instances""" # Python2 and Python3 safe for k in self.store.keys(): device = self.store[k] yield device # first get a list of all devices, in case the registry changes while iterating ##devices = self.store.keys() # now 'generate' one per call ##i = 0 ##while i < len(devices): ## k = devices[i] ## device = self.store[k] ## yield device ## i += 1 def names(self): """A generator/iterator that can be used to get a list of device names""" # first get a list of all devices, in case the registry changes while iterating devices = list(self.store.keys()) # now 'generate' one per call i = 0 while i < len(devices): k = devices[i] yield k i += 1
class DeviceRegistry(): # this is actions, so is this the 'RegistRAR'?? """A persistent registry for device class instance configurations""" DEFAULT_FILENAME = "registry.kvs" def __init__(self, filename=None): ##print("***Opening DeviceRegistry") self.store = KVS(filename) self.fsk_router = None def set_fsk_router(self, fsk_router): self.fsk_router = fsk_router def load_from(self, filename=None): """Start with a blank in memory registry, and load from the given filename""" if filename == None: filename = DeviceRegistry.DEFAULT_FILENAME # Create a new in memory store, effectively removing any existing in memory device class instances #TODO: Not good if there are routes to those class instances? self.store = KVS(filename) #TODO: later we might make it possible to load_from multiple files self.store.load(filename, Devices.DeviceFactory.get_device_from_name) def load_into(self, context): """auto-create variables in the provided context, for all persisted registry entries""" if context == None: raise ValueError("Must provide a context to hold new variables") for name in self.store.keys(): c = self.get(name) # This creates a variable inside the context of this name, points to class instance setattr(context, name, c) def add(self, device, name): """Add a device class instance to the registry, with a friendly name""" self.store[name] = device def get(self, name): # -> Device """Get the description for a device class from the store, and construct a class instance""" c = self.store[name] if self.fsk_router != None: if c.can_send(): # if can transmit, we can receive from it if isinstance(c, Devices.MiHomeDevice): print("Adding rx route for transmit enabled device %s" % c) address = (c.manufacturer_id, c.product_id, c.device_id) self.fsk_router.add(address, c) return c def rename(self, old_name, new_name): """Rename a device in the registry""" c = self.store[old_name] # get the class instance self.delete(old_name) # remove from memory and from any disk version self.add(c, new_name) # Add the same class back, but with the new name #Note: If rx routes are defined, they will still be correct, # because they wire directly to the device class instance def delete(self, name): """Delete the named class instance""" del self.store[name] def list(self): """List the registry in a vaguely printable format, mostly for debug""" print("REGISTERED DEVICES:") for k in self.store.keys(): print(" %s -> %s" % (k, self.store[k])) def size(self): """How many entries are there in the registry?""" return self.store.size() def devices(self): """A generator/iterator that can be used to get a list of device instances""" # Python2 and Python3 safe for k in self.store.keys(): device = self.store[k] yield device # first get a list of all devices, in case the registry changes while iterating ##devices = self.store.keys() # now 'generate' one per call ##i = 0 ##while i < len(devices): ## k = devices[i] ## device = self.store[k] ## yield device ## i += 1 def names(self): """A generator/iterator that can be used to get a list of device names""" # first get a list of all devices, in case the registry changes while iterating devices = list(self.store.keys()) # now 'generate' one per call i = 0 while i < len(devices): k = devices[i] yield k i += 1