|
| 1 | +from typing import Any, Dict, List, Optional |
| 2 | + |
| 3 | +import jsonschema |
| 4 | +from mat3ra.utils import object as object_utils |
| 5 | + |
| 6 | +from . import BaseUnderscoreJsonPropsHandler |
| 7 | +from .mixins import DefaultableMixin, HasDescriptionMixin, HasMetadataMixin, NamedMixin |
| 8 | + |
| 9 | + |
| 10 | +class ValidationErrorCode: |
| 11 | + IN_MEMORY_ENTITY_DATA_INVALID = "IN_MEMORY_ENTITY_DATA_INVALID" |
| 12 | + |
| 13 | + |
| 14 | +class ErrorDetails: |
| 15 | + def __init__(self, error: Optional[Dict[str, Any]], json: Dict[str, Any], schema: Dict): |
| 16 | + self.error = error |
| 17 | + self.json = json |
| 18 | + self.schema = schema |
| 19 | + |
| 20 | + |
| 21 | +class EntityError(Exception): |
| 22 | + def __init__(self, code: ValidationErrorCode, details: Optional[ErrorDetails] = None): |
| 23 | + super().__init__(code) |
| 24 | + self.code = code |
| 25 | + self.details = details |
| 26 | + |
| 27 | + |
| 28 | +class InMemoryEntity(BaseUnderscoreJsonPropsHandler): |
| 29 | + jsonSchema: Optional[Dict] = None |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def get_cls(cls) -> str: |
| 33 | + return cls.__name__ |
| 34 | + |
| 35 | + @property |
| 36 | + def cls(self) -> str: |
| 37 | + return self.__class__.__name__ |
| 38 | + |
| 39 | + def get_cls_name(self) -> str: |
| 40 | + return self.__class__.__name__ |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def create(cls, config: Dict[str, Any]) -> Any: |
| 44 | + return cls(config) |
| 45 | + |
| 46 | + def to_json(self, exclude: List[str] = []) -> Dict[str, Any]: |
| 47 | + return self.clean(object_utils.clone_deep(object_utils.omit(self._json, exclude))) |
| 48 | + |
| 49 | + def clone(self, extra_context: Dict[str, Any] = {}) -> Any: |
| 50 | + return self.__class__.__init__({**self.to_json(), **extra_context}) |
| 51 | + |
| 52 | + @staticmethod |
| 53 | + def validate_data(data: Dict[str, Any], clean: bool = False): |
| 54 | + if clean: |
| 55 | + print("Error: clean is not supported for InMemoryEntity.validateData") |
| 56 | + if InMemoryEntity.jsonSchema: |
| 57 | + jsonschema.validate(data, InMemoryEntity.jsonSchema) |
| 58 | + |
| 59 | + def validate(self) -> None: |
| 60 | + if self._json: |
| 61 | + self.__class__.validate_data(self._json) |
| 62 | + |
| 63 | + def clean(self, config: Dict[str, Any]) -> Dict[str, Any]: |
| 64 | + # Not implemented, consider the below for the implementation |
| 65 | + # https://stackoverflow.com/questions/44694835/remove-properties-from-json-object-not-present-in-schema |
| 66 | + return config |
| 67 | + |
| 68 | + def is_valid(self) -> bool: |
| 69 | + try: |
| 70 | + self.validate() |
| 71 | + return True |
| 72 | + except EntityError: |
| 73 | + return False |
| 74 | + |
| 75 | + # Properties |
| 76 | + @property |
| 77 | + def id(self) -> str: |
| 78 | + return self.prop("_id", "") |
| 79 | + |
| 80 | + @id.setter |
| 81 | + def id(self, id: str) -> None: |
| 82 | + self.set_prop("_id", id) |
| 83 | + |
| 84 | + @property |
| 85 | + def slug(self) -> str: |
| 86 | + return self.prop("slug", "") |
| 87 | + |
| 88 | + def get_as_entity_reference(self, by_id_only: bool = False) -> Dict[str, str]: |
| 89 | + if by_id_only: |
| 90 | + return {"_id": self.id} |
| 91 | + else: |
| 92 | + return {"_id": self.id, "slug": self.slug, "cls": self.get_cls_name()} |
| 93 | + |
| 94 | + |
| 95 | +class HasDescriptionHasMetadataNamedDefaultableInMemoryEntity( |
| 96 | + InMemoryEntity, DefaultableMixin, NamedMixin, HasMetadataMixin, HasDescriptionMixin |
| 97 | +): |
| 98 | + pass |
0 commit comments