|
1 | 1 | import uuid |
2 | 2 | from datetime import datetime |
| 3 | +from enum import Enum |
3 | 4 | from typing import Any |
4 | 5 | from typing import List |
5 | 6 | from typing import Optional |
6 | 7 | from typing import Union |
| 8 | +from typing import no_type_check |
7 | 9 |
|
8 | | -from lib.core.enums import AnnotationStatus |
9 | | -from pydantic import BaseModel |
| 10 | +from pydantic import BaseModel as PydanticBaseModel |
10 | 11 | from pydantic import Extra |
11 | 12 | from pydantic import Field |
12 | | -from pydantic import StrictBool |
13 | | -from pydantic import StrictFloat |
14 | | -from pydantic import StrictInt |
15 | | -from pydantic import StrictStr |
16 | 13 | from pydantic.datetime_parse import parse_datetime |
| 14 | +from pydantic.typing import is_namedtuple |
| 15 | +from pydantic.utils import ROOT_KEY |
| 16 | +from pydantic.utils import ValueItems |
| 17 | +from pydantic.utils import sequence_like |
| 18 | + |
| 19 | +from lib.core.enums import AnnotationStatus |
| 20 | +from lib.core.enums import BaseTitledEnum |
| 21 | + |
| 22 | +try: |
| 23 | + from pydantic import AbstractSetIntStr # noqa |
| 24 | + from pydantic import MappingIntStrAny # noqa |
| 25 | +except ImportError: |
| 26 | + pass |
| 27 | + |
| 28 | + |
| 29 | +class BaseModel(PydanticBaseModel): |
| 30 | + """ |
| 31 | +
|
| 32 | + """ |
| 33 | + @classmethod |
| 34 | + @no_type_check |
| 35 | + def _get_value( |
| 36 | + cls, |
| 37 | + v: Any, |
| 38 | + to_dict: bool, |
| 39 | + by_alias: bool, |
| 40 | + include: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']], |
| 41 | + exclude: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']], |
| 42 | + exclude_unset: bool, |
| 43 | + exclude_defaults: bool, |
| 44 | + exclude_none: bool, |
| 45 | + ) -> Any: |
| 46 | + |
| 47 | + if isinstance(v, BaseModel): |
| 48 | + if to_dict: |
| 49 | + v_dict = v.dict( |
| 50 | + by_alias=by_alias, |
| 51 | + exclude_unset=exclude_unset, |
| 52 | + exclude_defaults=exclude_defaults, |
| 53 | + include=include, |
| 54 | + exclude=exclude, |
| 55 | + exclude_none=exclude_none, |
| 56 | + ) |
| 57 | + if ROOT_KEY in v_dict: |
| 58 | + return v_dict[ROOT_KEY] |
| 59 | + return v_dict |
| 60 | + else: |
| 61 | + return v.copy(include=include, exclude=exclude) |
| 62 | + |
| 63 | + value_exclude = ValueItems(v, exclude) if exclude else None |
| 64 | + value_include = ValueItems(v, include) if include else None |
| 65 | + |
| 66 | + if isinstance(v, dict): |
| 67 | + return { |
| 68 | + k_: cls._get_value( |
| 69 | + v_, |
| 70 | + to_dict=to_dict, |
| 71 | + by_alias=by_alias, |
| 72 | + exclude_unset=exclude_unset, |
| 73 | + exclude_defaults=exclude_defaults, |
| 74 | + include=value_include and value_include.for_element(k_), |
| 75 | + exclude=value_exclude and value_exclude.for_element(k_), |
| 76 | + exclude_none=exclude_none, |
| 77 | + ) |
| 78 | + for k_, v_ in v.items() |
| 79 | + if (not value_exclude or not value_exclude.is_excluded(k_)) |
| 80 | + and (not value_include or value_include.is_included(k_)) |
| 81 | + } |
| 82 | + |
| 83 | + elif sequence_like(v): |
| 84 | + seq_args = ( |
| 85 | + cls._get_value( |
| 86 | + v_, |
| 87 | + to_dict=to_dict, |
| 88 | + by_alias=by_alias, |
| 89 | + exclude_unset=exclude_unset, |
| 90 | + exclude_defaults=exclude_defaults, |
| 91 | + include=value_include and value_include.for_element(i), |
| 92 | + exclude=value_exclude and value_exclude.for_element(i), |
| 93 | + exclude_none=exclude_none, |
| 94 | + ) |
| 95 | + for i, v_ in enumerate(v) |
| 96 | + if (not value_exclude or not value_exclude.is_excluded(i)) |
| 97 | + and (not value_include or value_include.is_included(i)) |
| 98 | + ) |
| 99 | + |
| 100 | + return v.__class__(*seq_args) if is_namedtuple(v.__class__) else v.__class__(seq_args) |
| 101 | + elif isinstance(v, BaseTitledEnum) and getattr(cls.Config, 'use_enum_names', False): |
| 102 | + return v.name |
| 103 | + elif isinstance(v, Enum) and getattr(cls.Config, 'use_enum_values', False): |
| 104 | + return v.name |
| 105 | + else: |
| 106 | + return v |
17 | 107 |
|
18 | 108 |
|
19 | 109 | class StringDate(datetime): |
@@ -70,68 +160,3 @@ def map_fields(entity: dict) -> dict: |
70 | 160 | entity["annotator_email"] = entity.get("annotator_id") |
71 | 161 | entity["qa_email"] = entity.get("qa_id") |
72 | 162 | return entity |
73 | | - |
74 | | - |
75 | | -class AttachmentEntity(BaseModel): |
76 | | - name: Optional[str] = Field(default_factory=lambda: str(uuid.uuid4())) |
77 | | - url: str |
78 | | - |
79 | | - class Config: |
80 | | - extra = Extra.ignore |
81 | | - |
82 | | - |
83 | | -class SettingEntity(BaseModel): |
84 | | - id: Optional[int] |
85 | | - project_id: Optional[int] |
86 | | - attribute: str |
87 | | - value: Union[StrictStr, StrictInt, StrictFloat, StrictBool] |
88 | | - |
89 | | - class Config: |
90 | | - extra = Extra.ignore |
91 | | - |
92 | | - def __copy__(self): |
93 | | - return SettingEntity(attribute=self.attribute, value=self.value) |
94 | | - |
95 | | - |
96 | | -class ProjectEntity(TimedBaseModel): |
97 | | - id: Optional[int] |
98 | | - team_id: Optional[int] |
99 | | - name: Optional[str] |
100 | | - type: Optional[int] |
101 | | - description: Optional[str] |
102 | | - instructions_link: Optional[str] |
103 | | - creator_id: Optional[str] |
104 | | - entropy_status: Optional[int] |
105 | | - sharing_status: Optional[int] |
106 | | - status: Optional[int] |
107 | | - folder_id: Optional[int] |
108 | | - sync_status: Optional[int] |
109 | | - upload_state: Optional[int] |
110 | | - users: Optional[List[Any]] = [] |
111 | | - unverified_users: Optional[List[Any]] = [] |
112 | | - contributors: Optional[List[Any]] = [] |
113 | | - settings: Optional[List[SettingEntity]] = [] |
114 | | - classes: Optional[List[Any]] = [] |
115 | | - workflows: Optional[List[Any]] = [] |
116 | | - completed_images_count: Optional[int] = Field(None, alias="completedImagesCount") |
117 | | - root_folder_completed_images_count: Optional[int] = Field( |
118 | | - None, alias="rootFolderCompletedImagesCount" |
119 | | - ) |
120 | | - |
121 | | - class Config: |
122 | | - extra = Extra.ignore |
123 | | - |
124 | | - def __copy__(self): |
125 | | - return ProjectEntity( |
126 | | - team_id=self.team_id, |
127 | | - name=self.name, |
128 | | - type=self.type, |
129 | | - description=self.description, |
130 | | - instructions_link=self.instructions_link |
131 | | - if self.description |
132 | | - else f"Copy of {self.name}.", |
133 | | - status=self.status, |
134 | | - folder_id=self.folder_id, |
135 | | - users=self.users, |
136 | | - upload_state=self.upload_state, |
137 | | - ) |
|
0 commit comments