__init__.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. target = properties.pop("target")
  50. if not callable(target):
  51. target = self.str_to_callable(target)
  52. ctrl = target(**properties)
  53. if name in _controllers.keys() and self.warnings:
  54. log.warn("overwriting controller "+name)
  55. _controllers[name] = ctrl
  56. retconf["controllers"][name] = ctrl
  57. # Next configure the commands
  58. for name,properties in config.get("commands",{}).items():
  59. function = properties.pop("function")
  60. ctrl = properties.pop("controller")
  61. if not callable(ctrl):
  62. if ctrl not in _controllers.keys():
  63. raise ValueError("No controller found with name " + ctrl)
  64. ctrl = _controllers[ctrl]
  65. if not hasattr(ctrl,function):
  66. raise ValueError(
  67. "{} doesn't have attribute {}".format(ctrl,function))
  68. function = getattr(ctrl,function)
  69. if name in _commands.keys() and self.warnings:
  70. log.warn("overwriting command "+name)
  71. _commands[name] = function
  72. retconf["commands"][name] = function
  73. def str_to_callable(self,dotted_str):
  74. parts = dotted_str.split(".")
  75. next_to_import = parts.pop(0)
  76. converted = __import__(next_to_import)
  77. for p in parts:
  78. next_to_import += "." + p
  79. if not hasattr(converted,p):
  80. converted = __import__(next_to_import)
  81. converted = getattr(converted,p)
  82. return converted
  83. #