-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathannotations.py
More file actions
136 lines (119 loc) · 4.67 KB
/
annotations.py
File metadata and controls
136 lines (119 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""The required data for working with annotations in Synapse"""
from dataclasses import dataclass, field
from datetime import date, datetime
from typing import Dict, List, Optional, Union
from synapseclient import Synapse
from synapseclient.annotations import ANNO_TYPE_TO_FUNC
from synapseclient.api import set_annotations_async
from synapseclient.core.async_utils import async_to_sync
from synapseclient.models.protocols.annotations_protocol import (
AnnotationsSynchronousProtocol,
)
@dataclass()
@async_to_sync
class Annotations(AnnotationsSynchronousProtocol):
"""Annotations that can be applied to a number of Synapse resources to provide
additional information.
Attributes:
annotations: Additional metadata associated with the object. The key is the name
of your desired annotations. The value is an object containing a list of
string values (use empty list to represent no values for key) and the value
type associated with all values in the list.
id: ID of the object to which this annotation belongs. Not required if being
used as a member variable on another class.
etag: Etag of the object to which this annotation belongs. This field must match
the current etag on the object. Not required if being used as a member
variable on another class.
"""
annotations: Union[
Dict[
str,
Union[
List[str],
List[bool],
List[float],
List[int],
List[date],
List[datetime],
],
],
None,
] = field(default_factory=dict)
"""Additional metadata associated with the object. The key is the name of your
desired annotations. The value is an object containing a list of string values
(use empty list to represent no values for key) and the value type associated with
all values in the list.
"""
id: Optional[str] = None
"""ID of the object to which this annotation belongs. Not required if being used as
a member variable on another class."""
etag: Optional[str] = None
""" Etag of the object to which this annotation belongs. To update an AnnotationV2,
this field must match the current etag on the object. Not required if being used as
a member variable on another class."""
async def store_async(
self,
*,
synapse_client: Optional[Synapse] = None,
) -> "Annotations":
"""Storing annotations to synapse.
Arguments:
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.
Returns:
The stored annotations.
Raises:
ValueError: If the id or etag are not set.
"""
if self.id is None or self.etag is None:
raise ValueError("id and etag are required to store annotations.")
result = await set_annotations_async(
annotations=self,
synapse_client=synapse_client,
)
self.annotations = Annotations.from_dict(result)
self.etag = result["etag"]
Synapse.get_client(synapse_client=synapse_client).logger.debug(
f"[{self.id}]: Stored annotations"
)
return self
@classmethod
def from_dict(cls, synapse_annotations: dict) -> Union[
Dict[
str,
Union[
List[str],
List[bool],
List[float],
List[int],
List[date],
List[datetime],
],
],
None,
]:
"""Convert the annotations from the format the synapse rest API works in -
to the format used by this class.
Arguments:
synapse_annotations: The annotations from the synapse rest API.
Returns:
The annotations in python class format or None.
"""
if synapse_annotations is None:
return None
annotations = {}
dict_to_convert = (
synapse_annotations["annotations"]
if "annotations" in synapse_annotations
else synapse_annotations
)
for key in dict_to_convert:
if isinstance(dict_to_convert[key], dict):
conversion_func = ANNO_TYPE_TO_FUNC[dict_to_convert[key]["type"]]
annotations[key] = [
conversion_func(v) for v in dict_to_convert[key]["value"]
]
else:
annotations[key] = dict_to_convert[key]
return annotations