|
@@ -0,0 +1,84 @@
|
|
|
+#!/usr/bin/env python3.5
|
|
|
+# Copyright 2017 Digital
|
|
|
+#
|
|
|
+# This file is part of BeeWatch.
|
|
|
+#
|
|
|
+# BeeWatch is free software: you can redistribute it and/or modify
|
|
|
+# it under the terms of the GNU General Public License as published by
|
|
|
+# the Free Software Foundation, either version 3 of the License, or
|
|
|
+# (at your option) any later version.
|
|
|
+#
|
|
|
+# BeeWatch is distributed in the hope that it will be useful,
|
|
|
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
+# GNU General Public License for more details.
|
|
|
+#
|
|
|
+# You should have received a copy of the GNU General Public License
|
|
|
+# along with BeeWatch. If not, see <http://www.gnu.org/licenses/>.
|
|
|
+
|
|
|
+
|
|
|
+class Configure(object):
|
|
|
+ """
|
|
|
+ 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):
|
|
|
+ config_keys = ["controllers","commands"]
|
|
|
+ for name,properties in config.pop("controllers"):
|
|
|
+ target = properties.pop("target")
|
|
|
+ if not callable(target):
|
|
|
+ target = self.str_to_callable(target)
|
|
|
+ self.retconf["controllers"][name] = target(**ctrl)
|
|
|
+ for name,function in config.pop("commands"):
|
|
|
+ if not callable(function):
|
|
|
+ function = self.str_to_callable()
|
|
|
+ self.retconf[commands][name] = function
|
|
|
+ def str_to_callable(self,dotted_str):
|
|
|
+ parts = dotted_str.split(".")
|
|
|
+ next_to_import = parts.pop(0)
|
|
|
+ converted = None
|
|
|
+ for p in parts:
|
|
|
+ if not hasattr(imported,p):
|
|
|
+ __import__(next_to_import)
|
|
|
+ converted = getattr(converted,p)
|
|
|
+ next_to_import += "." + p
|
|
|
+ return converted
|
|
|
+ for n in name:
|
|
|
+ used = used + '.' + n
|
|
|
+ try:
|
|
|
+ found = getattr(found, n)
|
|
|
+ except AttributeError:
|
|
|
+ __import__(used)
|
|
|
+ found = getattr(found, n)
|
|
|
+ return found
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#
|