/dialogue-engine/src/programy/config/brain/services.py

https://github.com/cotobadesign/cotoba-agent-oss · Python · 94 lines · 71 code · 15 blank · 8 comment · 7 complexity · a33fc55099b96f71c05fb4d0ea360237 MD5 · raw file

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