__init__.py 3.7 KB

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