__init__.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright 2019 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from configparser import ConfigParser
  15. from sydent.config.crypto import CryptoConfig
  16. from sydent.config.database import DatabaseConfig
  17. from sydent.config.email import EmailConfig
  18. from sydent.config.general import GeneralConfig
  19. from sydent.config.http import HTTPConfig
  20. from sydent.config.sms import SMSConfig
  21. class ConfigError(Exception):
  22. pass
  23. class SydentConfig:
  24. """This is the class in charge of handling Sydent's configuration.
  25. Handling of each individual section is delegated to other classes
  26. stored in a `config_sections` list.
  27. """
  28. def __init__(self):
  29. self.general = GeneralConfig()
  30. self.database = DatabaseConfig()
  31. self.crypto = CryptoConfig()
  32. self.sms = SMSConfig()
  33. self.email = EmailConfig()
  34. self.http = HTTPConfig()
  35. self.config_sections = [
  36. self.general,
  37. self.database,
  38. self.crypto,
  39. self.sms,
  40. self.email,
  41. self.http,
  42. ]
  43. def _parse_config(self, cfg: ConfigParser) -> None:
  44. """
  45. Run the parse_config method on each of the objects in
  46. self.config_sections
  47. :param cfg: the configuration to be parsed
  48. """
  49. for section in self.config_sections:
  50. section.parse_config(cfg)
  51. def parse_from_config_parser(self, cfg: ConfigParser) -> bool:
  52. """
  53. Parse the configuration from a ConfigParser object
  54. :param cfg: the configuration to be parsed
  55. ...
  56. :return: Whether or not cfg has been changed and needs saving
  57. """
  58. self._parse_config(cfg)
  59. # TODO: Don't alter config file when starting Sydent unless
  60. # user has asked for this specifially (e.g. on first
  61. # run only, or when specify --generate-config)
  62. return self.crypto.save_key