openwebrx-clone/owrx/config.py

101 lines
2.9 KiB
Python
Raw Normal View History

2019-05-10 19:50:58 +00:00
import logging
logger = logging.getLogger(__name__)
2019-05-04 14:56:23 +00:00
class Property(object):
def __init__(self, value = None):
self.value = value
2019-05-05 13:51:33 +00:00
self.callbacks = []
2019-05-04 14:56:23 +00:00
def getValue(self):
return self.value
def setValue(self, value):
if (self.value == value):
return self
2019-05-04 14:56:23 +00:00
self.value = value
2019-05-05 13:51:33 +00:00
for c in self.callbacks:
try:
c(self.value)
except Exception as e:
2019-05-10 20:08:00 +00:00
logger.exception(e)
2019-05-05 13:51:33 +00:00
return self
def wire(self, callback):
self.callbacks.append(callback)
if not self.value is None: callback(self.value)
return self
2019-05-04 14:56:23 +00:00
class PropertyManager(object):
sharedInstance = None
@staticmethod
def getSharedInstance():
if PropertyManager.sharedInstance is None:
PropertyManager.sharedInstance = PropertyManager()
return PropertyManager.sharedInstance
2019-05-07 14:32:53 +00:00
def collect(self, *props):
return PropertyManager({name: self.getProperty(name) if self.hasProperty(name) else Property() for name in props})
2019-05-07 14:32:53 +00:00
def __init__(self, properties = None):
2019-05-04 14:56:23 +00:00
self.properties = {}
self.callbacks = []
2019-05-07 14:32:53 +00:00
if properties is not None:
for (name, prop) in properties.items():
self.add(name, prop)
def add(self, name, prop):
self.properties[name] = prop
def fireCallbacks(value):
for c in self.callbacks:
try:
c(name, value)
except Exception as e:
2019-05-10 20:08:00 +00:00
logger.exception(e)
prop.wire(fireCallbacks)
return self
2019-05-07 14:32:53 +00:00
def __contains__(self, name):
return self.hasProperty(name)
2019-05-07 14:32:53 +00:00
def __getitem__(self, name):
return self.getPropertyValue(name)
def __setitem__(self, name, value):
2019-05-09 20:44:29 +00:00
if not self.hasProperty(name):
self.add(name, Property())
2019-05-07 14:32:53 +00:00
self.getProperty(name).setValue(value)
2019-05-04 14:56:23 +00:00
def __dict__(self):
return {k:v.getValue() for k, v in self.properties.items()}
2019-05-09 20:44:29 +00:00
def hasProperty(self, name):
return name in self.properties
2019-05-04 14:56:23 +00:00
def getProperty(self, name):
2019-05-09 20:44:29 +00:00
if not self.hasProperty(name):
2019-05-07 14:32:53 +00:00
self.add(name, Property())
2019-05-04 14:56:23 +00:00
return self.properties[name]
2019-05-04 18:26:11 +00:00
def getPropertyValue(self, name):
return self.getProperty(name).getValue()
2019-05-05 17:59:03 +00:00
def wire(self, callback):
self.callbacks.append(callback)
return self
2019-05-09 20:44:29 +00:00
def unwire(self, callback):
self.callbacks.remove(callback)
return self
def defaults(self, other_pm):
for (key, p) in self.properties.items():
if p.getValue() is None:
p.setValue(other_pm[key])
return self
def loadConfig(self, filename):
cfg = __import__(filename)
for name, value in cfg.__dict__.items():
if name.startswith("__"):
continue
self[name] = value
return self