section_for_tests.py 2.8 KB

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