__init__.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import logging
  19. import beewatch
  20. log = logging.getLogger(__name__+"")
  21. class Configure(object):
  22. warnings = True
  23. """
  24. Configure BeeWatch from a dict
  25. This class is inspired by logging.config
  26. (https://github.com/python/cpython/blob/3.6/Lib/logging/config.py)
  27. """
  28. def __init__(self,config):
  29. super(Configure,self).__init__()
  30. self.configure(config)
  31. def configure(self,config):
  32. retconf = {
  33. "controllers":{},
  34. "commands":{},
  35. }
  36. # Configure the Controllers first
  37. for name,properties in config.get("controllers",{}).items():
  38. target = properties.pop("target")
  39. if not callable(target):
  40. target = self.str_to_callable(target)
  41. ctrl = target(**properties)
  42. if name in _controllers.keys() and self.warnings:
  43. log.warn("overwriting controller "+name)
  44. _controllers[name] = ctrl
  45. retconf["controllers"][name] = ctrl
  46. # Next configure the commands
  47. for name,properties in config.get("commands",{}).items():
  48. function = properties.pop("function")
  49. ctrl = properties.pop("controller")
  50. if not callable(ctrl):
  51. if ctrl not in _controllers.keys():
  52. raise ValueError("No controller found with name " + ctrl)
  53. ctrl = _controllers[ctrl]
  54. if not hasattr(ctrl,function):
  55. raise ValueError(
  56. "{} doesn't have attribute {}".format(ctrl,function)
  57. )
  58. function = getattr(ctrl,function)
  59. if name in _commands.keys() and self.warnings:
  60. log.warn("overwriting command "+name)
  61. _commands[name] = function
  62. retconf["commands"][name] = function
  63. def str_to_callable(self,dotted_str):
  64. parts = dotted_str.split(".")
  65. next_to_import = parts.pop(0)
  66. converted = __import__(next_to_import)
  67. # converted = None
  68. # next_to_import = parts.pop(0)
  69. for p in parts:
  70. next_to_import += "." + p
  71. if not hasattr(converted,p):
  72. converted = __import__(next_to_import)
  73. converted = getattr(converted,p)
  74. return converted
  75. #