|
@@ -16,29 +16,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
+import logging
|
|
|
+import beewatch
|
|
|
+
|
|
|
+log = logging.getLogger(__name__+"")
|
|
|
+
|
|
|
|
|
|
class Configure(object):
|
|
|
+ warnings = True
|
|
|
"""
|
|
|
Configure BeeWatch from a dict
|
|
|
This class is inspired by logging.config
|
|
|
(https://github.com/python/cpython/blob/3.6/Lib/logging/config.py)
|
|
|
"""
|
|
|
+
|
|
|
def __init__(self,config):
|
|
|
super(Configure,self).__init__()
|
|
|
self.configure(config)
|
|
|
- self.config = {}
|
|
|
+
|
|
|
def configure(self,config):
|
|
|
- for name,properties in config.pop("controllers").items():
|
|
|
- print(name)
|
|
|
+ retconf = {
|
|
|
+ "controllers":{},
|
|
|
+ "commands":{},
|
|
|
+ }
|
|
|
+
|
|
|
+ for name,properties in config.get("controllers",{}).items():
|
|
|
target = properties.pop("target")
|
|
|
if not callable(target):
|
|
|
target = self.str_to_callable(target)
|
|
|
- print(target)
|
|
|
- self.retconf["controllers"][name] = target(**properties)
|
|
|
- for name,function in config.pop("commands").items():
|
|
|
- if not callable(function):
|
|
|
- function = self.str_to_callable()
|
|
|
- self.retconf[commands][name] = function
|
|
|
+ ctrl = target(**properties)
|
|
|
+ if name in _controllers.keys() and self.warnings:
|
|
|
+ log.warn("overwriting controller "+name)
|
|
|
+ _controllers[name] = ctrl
|
|
|
+ retconf["controllers"][name] = ctrl
|
|
|
+
|
|
|
+ for name,properties in config.get("commands",{}).items():
|
|
|
+ function = properties.pop("function")
|
|
|
+ ctrl = properties.pop("controller")
|
|
|
+ if not callable(ctrl):
|
|
|
+ if ctrl not in _controllers.keys():
|
|
|
+ raise ValueError("No controller found with name " + ctrl)
|
|
|
+ ctrl = _controllers[ctrl]
|
|
|
+ if not hasattr(ctrl,function):
|
|
|
+ raise ValueError(
|
|
|
+ "{} doesn't have attribute {}".format(ctrl,function)
|
|
|
+ )
|
|
|
+ function = getattr(ctrl,function)
|
|
|
+ if name in _commands.keys() and self.warnings:
|
|
|
+ log.warn("overwriting command "+name)
|
|
|
+ _commands[name] = function
|
|
|
+ retconf["commands"][name] = function
|
|
|
+
|
|
|
def str_to_callable(self,dotted_str):
|
|
|
parts = dotted_str.split(".")
|
|
|
next_to_import = parts.pop(0)
|
|
@@ -48,10 +76,8 @@ class Configure(object):
|
|
|
for p in parts:
|
|
|
next_to_import += "." + p
|
|
|
if not hasattr(converted,p):
|
|
|
- print("importing " + next_to_import)
|
|
|
converted = __import__(next_to_import)
|
|
|
converted = getattr(converted,p)
|
|
|
- print("converted",converted)
|
|
|
return converted
|
|
|
|
|
|
|