|
| 1 | +from contextlib import contextmanager |
| 2 | +from typing import ( |
| 3 | + Any, |
| 4 | + Dict, |
| 5 | + Generator, |
| 6 | + Iterator, |
| 7 | + List, |
| 8 | + MutableMapping, |
| 9 | + Sequence, |
| 10 | + TypeVar, |
| 11 | + Union, |
| 12 | +) |
| 13 | + |
| 14 | +from webknossos.client.api_client.models import ApiDataset, ApiFolder, ApiMetadata |
| 15 | +from webknossos.utils import infer_metadata_type, parse_metadata_value |
| 16 | + |
| 17 | +_T = TypeVar("_T", bound="Metadata") |
| 18 | + |
| 19 | + |
| 20 | +class Metadata(MutableMapping): |
| 21 | + __slots__ = () |
| 22 | + _api_path: str |
| 23 | + _api_type: Any |
| 24 | + |
| 25 | + def __init__(self, _id: str, *args: Any, **kwargs: Dict[str, Any]) -> None: |
| 26 | + if not self._api_path or not self._api_type: |
| 27 | + raise NotImplementedError( |
| 28 | + "This class is not meant to be used directly. Please use FolderMetadata or DatasetMetadata." |
| 29 | + ) |
| 30 | + super().__init__(*args, **kwargs) |
| 31 | + self._id: str = _id |
| 32 | + self._has_changed: bool = False |
| 33 | + self._mapping: Dict = {} |
| 34 | + |
| 35 | + @contextmanager |
| 36 | + def _recent_metadata(self: _T) -> Generator[_T, None, None]: |
| 37 | + from ..client.context import _get_api_client |
| 38 | + |
| 39 | + try: |
| 40 | + client = _get_api_client() |
| 41 | + full_object = client._get_json( |
| 42 | + f"{self._api_path}{self._id}", |
| 43 | + self._api_type, # type: ignore |
| 44 | + ) |
| 45 | + metadata: List[ApiMetadata] = full_object.metadata |
| 46 | + if metadata is not None: |
| 47 | + self._mapping = { |
| 48 | + element.key: parse_metadata_value(element.value, element.type) |
| 49 | + for element in metadata |
| 50 | + } |
| 51 | + else: |
| 52 | + self._mapping = {} |
| 53 | + yield self |
| 54 | + finally: |
| 55 | + if self._has_changed: |
| 56 | + api_metadata = [ |
| 57 | + ApiMetadata(key=k, type=infer_metadata_type(v), value=v) |
| 58 | + for k, v in self._mapping.items() |
| 59 | + ] |
| 60 | + |
| 61 | + full_object.metadata = api_metadata |
| 62 | + if self._api_type == ApiDataset: |
| 63 | + client._patch_json(f"{self._api_path}{self._id}", full_object) |
| 64 | + else: |
| 65 | + client._put_json(f"{self._api_path}{self._id}", full_object) |
| 66 | + self._has_changed = False |
| 67 | + |
| 68 | + def __setitem__( |
| 69 | + self, key: str, value: Union[str, int, float, Sequence[str]] |
| 70 | + ) -> None: |
| 71 | + with self._recent_metadata() as metadata: |
| 72 | + metadata._has_changed = True |
| 73 | + metadata._mapping[key] = value |
| 74 | + |
| 75 | + def __getitem__(self, key: str) -> Union[str, int, float, Sequence[str]]: |
| 76 | + with self._recent_metadata() as metadata: |
| 77 | + return metadata._mapping[key] |
| 78 | + |
| 79 | + def __delitem__(self, key: str) -> None: |
| 80 | + with self._recent_metadata() as metadata: |
| 81 | + metadata._has_changed = True |
| 82 | + del metadata._mapping[key] |
| 83 | + |
| 84 | + def __contains__(self, key: object) -> bool: |
| 85 | + with self._recent_metadata() as metadata: |
| 86 | + return key in metadata._mapping |
| 87 | + |
| 88 | + def __eq__(self, other: object) -> bool: |
| 89 | + if not isinstance(other, Metadata): |
| 90 | + raise NotImplementedError( |
| 91 | + f"Cannot compare {self.__class__.__name__} with {other.__class__.__name__}" |
| 92 | + ) |
| 93 | + with self._recent_metadata() as metadata: |
| 94 | + return metadata._mapping == other._mapping |
| 95 | + |
| 96 | + def __ne__(self, other: object) -> bool: |
| 97 | + return not self == other |
| 98 | + |
| 99 | + def __iter__(self) -> Iterator[Any]: |
| 100 | + with self._recent_metadata() as metadata: |
| 101 | + return iter(metadata._mapping) |
| 102 | + |
| 103 | + def __len__(self) -> int: |
| 104 | + with self._recent_metadata() as metadata: |
| 105 | + return len(metadata._mapping) |
| 106 | + |
| 107 | + def __repr__(self) -> str: |
| 108 | + with self._recent_metadata() as metadata: |
| 109 | + return f"{self.__class__.__name__}({repr(metadata._mapping)})" |
| 110 | + |
| 111 | + |
| 112 | +class FolderMetadata(Metadata): |
| 113 | + _api_path = "/folders/" |
| 114 | + _api_type = ApiFolder |
| 115 | + |
| 116 | + |
| 117 | +class DatasetMetadata(Metadata): |
| 118 | + _api_path = "/datasets/" |
| 119 | + _api_type = ApiDataset |
0 commit comments