__init__.py 3.5 KB

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