__init__.py 3.2 KB

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