data_accessor.py 5.9 KB

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