global_app_config.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright 2023 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. from __future__ import annotations
  12. from typing import Any, Dict, Optional
  13. from ..common._config_blocker import _ConfigBlocker
  14. from ..common._template_handler import _TemplateHandler as _tpl
  15. class GlobalAppConfig:
  16. """
  17. Configuration fields related to the global application.
  18. Attributes:
  19. **properties (Dict[str, Any]): A dictionary of additional properties.
  20. """
  21. def __init__(self, **properties):
  22. self._properties = properties
  23. @property
  24. def properties(self):
  25. return {k: _tpl._replace_templates(v) for k, v in self._properties.items()}
  26. @properties.setter # type: ignore
  27. @_ConfigBlocker._check()
  28. def properties(self, val):
  29. self._properties = val
  30. def __getattr__(self, item: str) -> Optional[Any]:
  31. return _tpl._replace_templates(self._properties.get(item))
  32. @classmethod
  33. def default_config(cls) -> GlobalAppConfig:
  34. return GlobalAppConfig()
  35. def _clean(self):
  36. self._properties.clear()
  37. def _to_dict(self):
  38. as_dict = {}
  39. as_dict.update(self._properties)
  40. return as_dict
  41. @classmethod
  42. def _from_dict(cls, config_as_dict: Dict[str, Any]):
  43. config = GlobalAppConfig()
  44. config._properties = config_as_dict
  45. return config
  46. def _update(self, config_as_dict):
  47. self._properties.update(config_as_dict)