section_for_tests.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright 2021-2024 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 copy import copy
  12. from typing import Any, Dict, Optional
  13. from taipy.config import Config, Section
  14. from taipy.config._config import _Config
  15. from taipy.config.common._config_blocker import _ConfigBlocker
  16. class SectionForTest(Section):
  17. name = "section_name"
  18. _MY_ATTRIBUTE_KEY = "attribute"
  19. def __init__(self, id: str, attribute: Any = None, **properties):
  20. self._attribute = attribute
  21. super().__init__(id, **properties)
  22. def __copy__(self):
  23. return SectionForTest(self.id, self._attribute, **copy(self._properties))
  24. @property
  25. def attribute(self):
  26. return self._replace_templates(self._attribute)
  27. @attribute.setter # type: ignore
  28. @_ConfigBlocker._check()
  29. def attribute(self, val):
  30. self._attribute = val
  31. def _clean(self):
  32. self._attribute = None
  33. self._properties.clear()
  34. def _to_dict(self):
  35. as_dict = {}
  36. if self._attribute is not None:
  37. as_dict[self._MY_ATTRIBUTE_KEY] = self._attribute
  38. as_dict.update(self._properties)
  39. return as_dict
  40. @classmethod
  41. def _from_dict(cls, as_dict: Dict[str, Any], id: str, config: Optional[_Config] = None):
  42. as_dict.pop(cls._ID_KEY, id)
  43. attribute = as_dict.pop(cls._MY_ATTRIBUTE_KEY, None)
  44. return SectionForTest(id=id, attribute=attribute, **as_dict)
  45. def _update(self, as_dict: Dict[str, Any], default_section=None):
  46. self._attribute = as_dict.pop(self._MY_ATTRIBUTE_KEY, self._attribute)
  47. if self._attribute is None and default_section:
  48. self._attribute = default_section._attribute
  49. self._properties.update(as_dict)
  50. if default_section:
  51. self._properties = {**default_section.properties, **self._properties}
  52. @staticmethod
  53. def _configure(id: str, attribute: str, **properties):
  54. section = SectionForTest(id, attribute, **properties)
  55. Config._register(section)
  56. return Config.sections[SectionForTest.name][id]