__init__.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. def get_example_config_path():
  38. print(__file__)
  39. class Configure(object):
  40. """
  41. Configure BeeWatch from a dict
  42. This class is inspired by logging.config
  43. (https://github.com/python/cpython/blob/3.6/Lib/logging/config.py)
  44. Parameters
  45. ----------
  46. config: dict
  47. dictionary holding config information. for more information see :any:`/config/index`
  48. """
  49. warnings = True
  50. def __init__(self,config):
  51. super(Configure,self).__init__()
  52. self.configure(config)
  53. def configure(self,config):
  54. retconf = {
  55. "controllers":{},
  56. "commands":{},
  57. }
  58. # Configure the Controllers first
  59. for name,properties in config.get("controllers",{}).items():
  60. log.debug("configuring {}".format(name))
  61. target = properties.pop("target")
  62. if not callable(target):
  63. target = self.str_to_callable(target)
  64. ctrl = target(**properties)
  65. if name in _controllers.keys() and self.warnings:
  66. log.warn("overwriting controller "+name)
  67. _controllers[name] = ctrl
  68. retconf["controllers"][name] = ctrl
  69. # Next configure the commands
  70. for name,properties in config.get("commands",{}).items():
  71. log.debug("configuring {}".format(name))
  72. function = properties.pop("function")
  73. ctrl = properties.pop("controller")
  74. if not callable(ctrl):
  75. if ctrl not in _controllers.keys():
  76. raise ValueError("No controller found with name " + ctrl)
  77. ctrl = _controllers[ctrl]
  78. if not hasattr(ctrl,function):
  79. raise ValueError(
  80. "{} doesn't have attribute {}".format(ctrl,function))
  81. function = getattr(ctrl,function)
  82. if name in _commands.keys() and self.warnings:
  83. log.warn("overwriting command "+name)
  84. _commands[name] = function
  85. retconf["commands"][name] = function
  86. def str_to_callable(self,dotted_str):
  87. parts = dotted_str.split(".")
  88. next_to_import = parts.pop(0)
  89. converted = __import__(next_to_import)
  90. for p in parts:
  91. next_to_import += "." + p
  92. if not hasattr(converted,p):
  93. converted = __import__(next_to_import)
  94. converted = getattr(converted,p)
  95. return converted
  96. #