/src/programy/config/brain/services.py

https://github.com/keiffster/program-y · Python · 78 lines · 58 code · 16 blank · 4 comment · 7 complexity · 2cc1220452daa5a07fb25e10dbcbee2f MD5 · raw file

  1. """
  2. Copyright (c) 2016-2020 Keith Sterling http://www.keithsterling.com
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  4. documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  5. the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  6. and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  8. Software.
  9. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  10. THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  11. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  12. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  13. """
  14. from programy.utils.logging.ylogger import YLogger
  15. from programy.config.section import BaseSectionConfigurationData
  16. from programy.config.brain.service import BrainServiceConfiguration
  17. from programy.utils.substitutions.substitues import Substitutions
  18. class BrainServicesConfiguration(BaseSectionConfigurationData):
  19. def __init__(self):
  20. BaseSectionConfigurationData.__init__(self, "services")
  21. self._services = {}
  22. def exists(self, name):
  23. return bool(name in self._services)
  24. def service(self, name):
  25. if name in self._services:
  26. return self._services[name]
  27. return None
  28. def services(self):
  29. return list(self._services.keys())
  30. def load_config_section(self, configuration_file, configuration, bot_root, subs: Substitutions = None):
  31. services = configuration_file.get_section(self.section_name, configuration)
  32. if services is not None:
  33. service_keys = configuration_file.get_keys(services)
  34. for name in service_keys:
  35. service = BrainServiceConfiguration(name)
  36. service.load_config_section(configuration_file, services, bot_root, subs=subs)
  37. self._services[name] = service
  38. else:
  39. YLogger.warning(self, "Config section [services] missing from Brain, no services loaded")
  40. def to_yaml(self, data, defaults=True):
  41. if defaults is True:
  42. data['REST'] = {}
  43. data['REST']['classname'] = 'programy.services.rest.GenericRESTService'
  44. data['REST']['method'] = 'GET'
  45. data['REST']['host'] = '0.0.0.0'
  46. data['Pannous'] = {}
  47. data['Pannous']['classname'] = 'programy.services.pannous.PannousService'
  48. data['Pannous']['url'] = 'http://weannie.pannous.com/api'
  49. data['Pandora'] = {}
  50. data['Pandora']['classname'] = 'programy.services.pandora.PandoraService'
  51. data['Pandora']['url'] = 'http://www.pandorabots.com/pandora/talk-xml'
  52. data['Wikipedia'] = {}
  53. data['Wikipedia']['classname'] = 'programy.services.wikipediaservice.WikipediaService'
  54. data['DuckDuckGo'] = {}
  55. data['DuckDuckGo']['classname'] = 'programy.services.duckduckgo.DuckDuckGoService'
  56. data['DuckDuckGo']['url'] = 'http://api.duckduckgo.com'
  57. else:
  58. for _, config in self._services:
  59. self.config_to_yaml(data, config, defaults)