__init__.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3.5
  2. # Copyright 2017 Digital
  3. #
  4. # This file is part of BeeWatch.
  5. #
  6. # BeeWatch is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # BeeWatch is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with BeeWatch. If not, see <http://www.gnu.org/licenses/>.
  18. class Configure(object):
  19. """
  20. Configure BeeWatch from a dict
  21. This class is inspired by logging.config
  22. (https://github.com/python/cpython/blob/3.6/Lib/logging/config.py)
  23. """
  24. def __init__(self,config):
  25. super(Configure,self).__init__()
  26. self.configure(config)
  27. self.config = {}
  28. def configure(self,config):
  29. config_keys = ["controllers","commands"]
  30. for name,properties in config.pop("controllers"):
  31. target = properties.pop("target")
  32. if not callable(target):
  33. target = self.str_to_callable(target)
  34. self.retconf["controllers"][name] = target(**ctrl)
  35. for name,function in config.pop("commands"):
  36. if not callable(function):
  37. function = self.str_to_callable()
  38. self.retconf[commands][name] = function
  39. def str_to_callable(self,dotted_str):
  40. parts = dotted_str.split(".")
  41. next_to_import = parts.pop(0)
  42. converted = None
  43. for p in parts:
  44. if not hasattr(imported,p):
  45. __import__(next_to_import)
  46. converted = getattr(converted,p)
  47. next_to_import += "." + p
  48. return converted
  49. for n in name:
  50. used = used + '.' + n
  51. try:
  52. found = getattr(found, n)
  53. except AttributeError:
  54. __import__(used)
  55. found = getattr(found, n)
  56. return found
  57. #