__init__.py 3.4 KB

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