data_accessor.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. import inspect
  12. import typing as t
  13. from abc import ABC, abstractmethod
  14. from .._warnings import _warn
  15. from ..utils import _TaipyData
  16. from .data_format import _DataFormat
  17. if t.TYPE_CHECKING:
  18. from ..gui import Gui
  19. class _DataAccessor(ABC):
  20. _WS_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
  21. def __init__(self, gui: "Gui") -> None:
  22. self._gui = gui
  23. @staticmethod
  24. @abstractmethod
  25. def get_supported_classes() -> t.List[t.Type]:
  26. pass
  27. @abstractmethod
  28. def get_data(
  29. self, var_name: str, value: t.Any, payload: t.Dict[str, t.Any], data_format: _DataFormat
  30. ) -> t.Dict[str, t.Any]:
  31. pass
  32. @abstractmethod
  33. def get_col_types(self, var_name: str, value: t.Any) -> t.Dict[str, str]:
  34. pass
  35. @abstractmethod
  36. def to_pandas(self, value: t.Any) -> t.Union[t.List[t.Any], t.Any]:
  37. pass
  38. @abstractmethod
  39. def on_edit(self, value: t.Any, payload: t.Dict[str, t.Any]) -> t.Optional[t.Any]:
  40. pass
  41. @abstractmethod
  42. def on_delete(self, value: t.Any, payload: t.Dict[str, t.Any]) -> t.Optional[t.Any]:
  43. pass
  44. @abstractmethod
  45. def on_add(
  46. self, value: t.Any, payload: t.Dict[str, t.Any], new_row: t.Optional[t.List[t.Any]] = None
  47. ) -> t.Optional[t.Any]:
  48. pass
  49. @abstractmethod
  50. def to_csv(self, var_name: str, value: t.Any) -> t.Optional[str]:
  51. pass
  52. class _InvalidDataAccessor(_DataAccessor):
  53. @staticmethod
  54. def get_supported_classes() -> t.List[t.Type]:
  55. return []
  56. def get_data(
  57. self, var_name: str, value: t.Any, payload: t.Dict[str, t.Any], data_format: _DataFormat
  58. ) -> t.Dict[str, t.Any]:
  59. return {}
  60. def get_col_types(self, var_name: str, value: t.Any) -> t.Dict[str, str]:
  61. return {}
  62. def to_pandas(self, value: t.Any) -> t.Union[t.List[t.Any], t.Any]:
  63. return None
  64. def on_edit(self, value: t.Any, payload: t.Dict[str, t.Any]):
  65. return None
  66. def on_delete(self, value: t.Any, payload: t.Dict[str, t.Any]):
  67. return None
  68. def on_add(self, value: t.Any, payload: t.Dict[str, t.Any], new_row: t.Optional[t.List[t.Any]] = None):
  69. return None
  70. def to_csv(self, var_name: str, value: t.Any):
  71. return None
  72. class _DataAccessors(object):
  73. def __init__(self, gui: "Gui") -> None:
  74. self.__access_4_type: t.Dict[t.Type, _DataAccessor] = {}
  75. self.__invalid_data_accessor = _InvalidDataAccessor(gui)
  76. self.__data_format = _DataFormat.JSON
  77. self.__gui = gui
  78. from .array_dict_data_accessor import _ArrayDictDataAccessor
  79. from .numpy_data_accessor import _NumpyDataAccessor
  80. from .pandas_data_accessor import _PandasDataAccessor
  81. self._register(_PandasDataAccessor)
  82. self._register(_ArrayDictDataAccessor)
  83. self._register(_NumpyDataAccessor)
  84. def _register(self, cls: t.Type[_DataAccessor]) -> None:
  85. if not inspect.isclass(cls):
  86. raise AttributeError("The argument of 'DataAccessors.register' should be a class")
  87. if not issubclass(cls, _DataAccessor):
  88. raise TypeError(f"Class {cls.__name__} is not a subclass of DataAccessor")
  89. classes = cls.get_supported_classes()
  90. if not classes:
  91. raise TypeError(f"method {cls.__name__}.get_supported_classes returned an invalid value")
  92. # check existence
  93. inst: t.Optional[_DataAccessor] = None
  94. for cl in classes:
  95. inst = self.__access_4_type.get(cl)
  96. if inst:
  97. break
  98. if inst is None:
  99. try:
  100. inst = cls(self.__gui)
  101. except Exception as e:
  102. raise TypeError(f"Class {cls.__name__} cannot be instantiated") from e
  103. if inst:
  104. for cl in classes:
  105. self.__access_4_type[cl] = inst # type: ignore
  106. def __get_instance(self, value: _TaipyData) -> _DataAccessor: # type: ignore
  107. value = value.get() if isinstance(value, _TaipyData) else value
  108. access = self.__access_4_type.get(type(value))
  109. if access is None:
  110. if value is not None:
  111. _warn(f"Can't find Data Accessor for type {str(type(value))}.")
  112. return self.__invalid_data_accessor
  113. return access
  114. def get_data(self, var_name: str, value: _TaipyData, payload: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]:
  115. return self.__get_instance(value).get_data(var_name, value.get(), payload, self.__data_format)
  116. def get_col_types(self, var_name: str, value: _TaipyData) -> t.Dict[str, str]:
  117. return self.__get_instance(value).get_col_types(var_name, value.get())
  118. def set_data_format(self, data_format: _DataFormat):
  119. self.__data_format = data_format
  120. def get_dataframe(self, value: t.Any):
  121. return self.__get_instance(value).to_pandas(value)
  122. def on_edit(self, value: t.Any, payload: t.Dict[str, t.Any]):
  123. return self.__get_instance(value).on_edit(value, payload)
  124. def on_delete(self, value: t.Any, payload: t.Dict[str, t.Any]):
  125. return self.__get_instance(value).on_delete(value, payload)
  126. def on_add(self, value: t.Any, payload: t.Dict[str, t.Any], new_row: t.Optional[t.List[t.Any]] = None):
  127. return self.__get_instance(value).on_add(value, payload, new_row)
  128. def to_csv(self, var_name: str, value: t.Any):
  129. return self.__get_instance(value).to_csv(var_name, value.get())
  130. def to_pandas(self, value: t.Any):
  131. return self.__get_instance(value).to_pandas(value.get())