|
| 1 | +from dataclasses import asdict, dataclass, fields |
| 2 | +from typing import Iterable |
| 3 | + |
| 4 | +from requests import Session |
| 5 | + |
| 6 | +from ..models.category_type import LABEL_COLOR_CYCLE, CategoryType |
| 7 | +from ..utils.response import verbose_raise_for_status |
| 8 | + |
| 9 | +COLOR_CYCLE_RANGE = len(LABEL_COLOR_CYCLE) |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class CategoryTypeController: |
| 14 | + """Wraps a CategoryType with fields used for interacting directly with Doccano client""" |
| 15 | + |
| 16 | + category_type: CategoryType |
| 17 | + id: int |
| 18 | + category_types_url: str |
| 19 | + client_session: Session |
| 20 | + |
| 21 | + @property |
| 22 | + def category_type_url(self) -> str: |
| 23 | + """Return an api url for this category_type""" |
| 24 | + return f"{self.category_types_url}/{self.id}" |
| 25 | + |
| 26 | + |
| 27 | +class CategoryTypesController: |
| 28 | + """Controls the creation and retrieval of individual CategoryTypeControllers for an assigned project""" |
| 29 | + |
| 30 | + def __init__(self, project_url: str, client_session: Session) -> None: |
| 31 | + """Initializes a CategoryTypeController instance""" |
| 32 | + self._project_url = project_url |
| 33 | + self.client_session = client_session |
| 34 | + |
| 35 | + @property |
| 36 | + def category_types_url(self) -> str: |
| 37 | + """Return an api url for category_types list""" |
| 38 | + return f"{self._project_url}/category-types" |
| 39 | + |
| 40 | + def all(self) -> Iterable[CategoryTypeController]: |
| 41 | + """Return a sequence of all category-types for a given controller, which maps to a project |
| 42 | +
|
| 43 | + Yields: |
| 44 | + CategoryTypeController: The next category type controller. |
| 45 | + """ |
| 46 | + response = self.client_session.get(self.category_types_url) |
| 47 | + verbose_raise_for_status(response) |
| 48 | + category_type_dicts = response.json() |
| 49 | + category_type_object_fields = set(category_type_field.name for category_type_field in fields(CategoryType)) |
| 50 | + |
| 51 | + for category_type_dict in category_type_dicts: |
| 52 | + # Sanitize category_type_dict before converting to CategoryType |
| 53 | + sanitized_category_type_dict = { |
| 54 | + category_type_key: category_type_dict[category_type_key] |
| 55 | + for category_type_key in category_type_object_fields |
| 56 | + } |
| 57 | + |
| 58 | + yield CategoryTypeController( |
| 59 | + category_type=CategoryType(**sanitized_category_type_dict), |
| 60 | + id=category_type_dict["id"], |
| 61 | + category_types_url=self.category_types_url, |
| 62 | + client_session=self.client_session, |
| 63 | + ) |
| 64 | + |
| 65 | + def create(self, category_type: CategoryType) -> CategoryTypeController: |
| 66 | + """Create new category_type for Doccano project, assign session variables to category_type, return the id""" |
| 67 | + category_type_json = asdict(category_type) |
| 68 | + |
| 69 | + response = self.client_session.post(self.category_types_url, json=category_type_json) |
| 70 | + verbose_raise_for_status(response) |
| 71 | + response_id = response.json()["id"] |
| 72 | + |
| 73 | + return CategoryTypeController( |
| 74 | + category_type=category_type, |
| 75 | + id=response_id, |
| 76 | + category_types_url=self.category_types_url, |
| 77 | + client_session=self.client_session, |
| 78 | + ) |
| 79 | + |
| 80 | + def update(self, category_type_controllers: Iterable[CategoryTypeController]) -> None: |
| 81 | + """Updates the given category_types in the remote project""" |
| 82 | + for category_type_controller in category_type_controllers: |
| 83 | + category_type_json = asdict(category_type_controller.category_type) |
| 84 | + category_type_json = { |
| 85 | + category_type_key: category_type_value |
| 86 | + for category_type_key, category_type_value in category_type_json.items() |
| 87 | + if category_type_value is not None |
| 88 | + } |
| 89 | + category_type_json["id"] = category_type_controller.id |
| 90 | + |
| 91 | + response = self.client_session.put(category_type_controller.category_type_url, json=category_type_json) |
| 92 | + verbose_raise_for_status(response) |
0 commit comments