unique_section_for_tests.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 copy import copy
  12. from typing import Any, Dict, Optional
  13. from taipy.config import Config
  14. from taipy.config._config import _Config
  15. from taipy.config.common._config_blocker import _ConfigBlocker
  16. from taipy.config.unique_section import UniqueSection
  17. class UniqueSectionForTest(UniqueSection):
  18. name = "unique_section_name"
  19. _MY_ATTRIBUTE_KEY = "attribute"
  20. def __init__(self, attribute: str = None, **properties):
  21. self._attribute = attribute
  22. super().__init__(**properties)
  23. def __copy__(self):
  24. return UniqueSectionForTest(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=None, config: Optional[_Config] = None):
  43. as_dict.pop(cls._ID_KEY, None)
  44. attribute = as_dict.pop(cls._MY_ATTRIBUTE_KEY, None)
  45. return UniqueSectionForTest(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 _configure(attribute: str, **properties):
  55. section = UniqueSectionForTest(attribute, **properties)
  56. Config._register(section)
  57. return Config.unique_sections[UniqueSectionForTest.name]