|
1 | 1 | from typing import Any, Dict, List, Optional, Type, TypeVar |
2 | 2 |
|
3 | | -import jsonschema |
4 | | -from mat3ra.utils import object as object_utils |
5 | 3 | from pydantic import BaseModel, ConfigDict |
6 | 4 | from pydantic.alias_generators import to_snake |
7 | 5 | from typing_extensions import Self |
8 | 6 |
|
9 | | -from . import BaseUnderscoreJsonPropsHandler |
10 | 7 | from .mixins import DefaultableMixin, HasDescriptionMixin, HasMetadataMixin, NamedMixin |
11 | 8 |
|
12 | 9 | T = TypeVar("T", bound="InMemoryEntityPydantic") |
13 | 10 | B = TypeVar("B", bound="BaseModel") |
14 | 11 |
|
15 | 12 |
|
16 | | -# TODO: remove in the next PR |
17 | | -class ValidationErrorCode: |
18 | | - IN_MEMORY_ENTITY_DATA_INVALID = "IN_MEMORY_ENTITY_DATA_INVALID" |
19 | | - |
20 | | - |
21 | | -# TODO: remove in the next PR |
22 | | -class ErrorDetails: |
23 | | - def __init__(self, error: Optional[Dict[str, Any]], json: Dict[str, Any], schema: Dict): |
24 | | - self.error = error |
25 | | - self.json = json |
26 | | - self.schema = schema |
27 | | - |
28 | | - |
29 | | -# TODO: remove in the next PR |
30 | | -class EntityError(Exception): |
31 | | - def __init__(self, code: ValidationErrorCode, details: Optional[ErrorDetails] = None): |
32 | | - super().__init__(code) |
33 | | - self.code = code |
34 | | - self.details = details |
35 | | - |
36 | | - |
37 | 13 | class InMemoryEntityPydantic(BaseModel): |
38 | 14 | model_config = {"arbitrary_types_allowed": True} |
39 | 15 |
|
@@ -125,79 +101,6 @@ def __init_subclass__(cls, **kwargs): |
125 | 101 | setattr(cls, snake_case_name, cls._create_property(field_name)) |
126 | 102 |
|
127 | 103 |
|
128 | | -# TODO: remove in the next PR |
129 | | -class InMemoryEntity(BaseUnderscoreJsonPropsHandler): |
130 | | - jsonSchema: Optional[Dict] = None |
131 | | - |
132 | | - @classmethod |
133 | | - def get_cls(cls) -> str: |
134 | | - return cls.__name__ |
135 | | - |
136 | | - @property |
137 | | - def cls(self) -> str: |
138 | | - return self.__class__.__name__ |
139 | | - |
140 | | - def get_cls_name(self) -> str: |
141 | | - return self.__class__.__name__ |
142 | | - |
143 | | - @classmethod |
144 | | - def create(cls, config: Dict[str, Any]) -> Any: |
145 | | - return cls(config) |
146 | | - |
147 | | - def to_json(self, exclude: List[str] = []) -> Dict[str, Any]: |
148 | | - return self.clean(object_utils.clone_deep(object_utils.omit(self._json, exclude))) |
149 | | - |
150 | | - def clone(self, extra_context: Dict[str, Any] = {}) -> Any: |
151 | | - config = self.to_json() |
152 | | - config.update(extra_context) |
153 | | - # To avoid: |
154 | | - # Argument 1 to "__init__" of "BaseUnderscoreJsonPropsHandler" has incompatible type "Dict[str, Any]"; |
155 | | - # expected "BaseUnderscoreJsonPropsHandler" |
156 | | - return self.__class__(config) |
157 | | - |
158 | | - @staticmethod |
159 | | - def validate_data(data: Dict[str, Any], clean: bool = False): |
160 | | - if clean: |
161 | | - print("Error: clean is not supported for InMemoryEntity.validateData") |
162 | | - if InMemoryEntity.jsonSchema: |
163 | | - jsonschema.validate(data, InMemoryEntity.jsonSchema) |
164 | | - |
165 | | - def validate(self) -> None: |
166 | | - if self._json: |
167 | | - self.__class__.validate_data(self._json) |
168 | | - |
169 | | - def clean(self, config: Dict[str, Any]) -> Dict[str, Any]: |
170 | | - # Not implemented, consider the below for the implementation |
171 | | - # https://stackoverflow.com/questions/44694835/remove-properties-from-json-object-not-present-in-schema |
172 | | - return config |
173 | | - |
174 | | - def is_valid(self) -> bool: |
175 | | - try: |
176 | | - self.validate() |
177 | | - return True |
178 | | - except EntityError: |
179 | | - return False |
180 | | - |
181 | | - # Properties |
182 | | - @property |
183 | | - def id(self) -> str: |
184 | | - return self.prop("_id", "") |
185 | | - |
186 | | - @id.setter |
187 | | - def id(self, id: str) -> None: |
188 | | - self.set_prop("_id", id) |
189 | | - |
190 | | - @property |
191 | | - def slug(self) -> str: |
192 | | - return self.prop("slug", "") |
193 | | - |
194 | | - def get_as_entity_reference(self, by_id_only: bool = False) -> Dict[str, str]: |
195 | | - if by_id_only: |
196 | | - return {"_id": self.id} |
197 | | - else: |
198 | | - return {"_id": self.id, "slug": self.slug, "cls": self.get_cls_name()} |
199 | | - |
200 | | - |
201 | 104 | class HasDescriptionHasMetadataNamedDefaultableInMemoryEntityPydantic( |
202 | 105 | InMemoryEntityPydantic, DefaultableMixin, NamedMixin, HasMetadataMixin, HasDescriptionMixin |
203 | 106 | ): |
|
0 commit comments