openwebrx-clone/owrx/config.py

150 lines
4.1 KiB
Python
Raw Normal View History

2019-12-08 16:15:48 +00:00
import importlib.util
2019-05-10 19:50:58 +00:00
import logging
2019-05-10 19:50:58 +00:00
logger = logging.getLogger(__name__)
class Subscription(object):
def __init__(self, subscriptee, subscriber):
self.subscriptee = subscriptee
self.subscriber = subscriber
def call(self, *args, **kwargs):
self.subscriber(*args, **kwargs)
def cancel(self):
self.subscriptee.unwire(self)
2019-05-04 14:56:23 +00:00
class Property(object):
def __init__(self, value=None):
2019-05-04 14:56:23 +00:00
self.value = value
self.subscribers = []
2019-05-04 14:56:23 +00:00
def getValue(self):
return self.value
2019-05-04 14:56:23 +00:00
def setValue(self, value):
if self.value == value:
return self
2019-05-04 14:56:23 +00:00
self.value = value
for c in self.subscribers:
try:
c.call(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
2019-05-05 13:51:33 +00:00
def wire(self, callback):
sub = Subscription(self, callback)
self.subscribers.append(sub)
if not self.value is None:
sub.call(self.value)
return sub
def unwire(self, sub):
try:
self.subscribers.remove(sub)
except ValueError:
# happens when already removed before
pass
2019-05-05 13:51:33 +00:00
return self
2019-05-04 14:56:23 +00:00
2019-12-08 16:15:48 +00:00
class ConfigNotFoundException(Exception):
pass
2019-05-04 14:56:23 +00:00
class PropertyManager(object):
sharedInstance = None
2019-05-04 14:56:23 +00:00
@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.subscribers = []
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.subscribers:
try:
c.call(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):
sub = Subscription(self, callback)
self.subscribers.append(sub)
return sub
def unwire(self, sub):
try:
self.subscribers.remove(sub)
except ValueError:
# happens when already removed before
pass
2019-05-09 20:44:29 +00:00
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
2019-12-08 16:15:48 +00:00
def loadConfig(self):
for file in ["/etc/config_webrx.py", "./config_webrx.py"]:
try:
spec = importlib.util.spec_from_file_location("config_webrx", file)
cfg = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cfg)
for name, value in cfg.__dict__.items():
if name.startswith("__"):
continue
self[name] = value
return self
except FileNotFoundError:
logger.debug("not found: %s", file)
raise ConfigNotFoundException("no usable config found! please make sure you have a valid configuration file!")