-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlists.py
More file actions
456 lines (395 loc) · 14.8 KB
/
lists.py
File metadata and controls
456 lines (395 loc) · 14.8 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
from __future__ import annotations
import json
import typing
from typing import Optional, Iterable, Any, Generator
from ayon_api.utils import create_entity_id
from ayon_api.graphql_queries import entity_lists_graphql_query
from .base import BaseServerAPI
if typing.TYPE_CHECKING:
from ayon_api.typing import (
EntityListEntityType,
EntityListAttributeDefinitionDict,
EntityListItemMode,
)
class ListsAPI(BaseServerAPI):
def get_entity_lists(
self,
project_name: str,
*,
list_ids: Optional[Iterable[str]] = None,
active: Optional[bool] = None,
fields: Optional[Iterable[str]] = None,
) -> Generator[dict[str, Any], None, None]:
"""Fetch entity lists from AYON server.
Warnings:
You can't get list items for lists with different 'entityType' in
one call.
Notes:
To get list items, you have to pass 'items' field or
'items.{sub-fields you want}' to 'fields' argument.
Args:
project_name (str): Project name where entity lists are.
list_ids (Optional[Iterable[str]]): List of entity list ids to
fetch.
active (Optional[bool]): Filter by active state of entity lists.
fields (Optional[Iterable[str]]): Fields to fetch from server.
Returns:
Generator[dict[str, Any], None, None]: Entity list entities
matching defined filters.
"""
if fields is None:
fields = self.get_default_fields_for_type("entityList")
# List does not have 'attrib' field but has 'allAttrib' field
# which is json string and contains only values that are set
o_fields = tuple(fields)
fields = set()
requires_attrib = False
for field in o_fields:
if field == "attrib" or field.startswith("attrib."):
requires_attrib = True
field = "allAttrib"
fields.add(field)
if "items" in fields:
fields.discard("items")
fields |= {
"items.id",
"items.entityId",
"items.entityType",
"items.position",
}
available_attribs = []
if requires_attrib:
available_attribs = self.get_attributes_for_type("list")
if active is not None:
fields.add("active")
filters: dict[str, Any] = {"projectName": project_name}
if list_ids is not None:
if not list_ids:
return
filters["listIds"] = list(set(list_ids))
query = entity_lists_graphql_query(fields)
for attr, filter_value in filters.items():
query.set_variable_value(attr, filter_value)
for parsed_data in query.continuous_query(self):
for entity_list in parsed_data["project"]["entityLists"]:
if active is not None and entity_list["active"] != active:
continue
attributes = entity_list.get("attributes")
if isinstance(attributes, str):
entity_list["attributes"] = json.loads(attributes)
if requires_attrib:
all_attrib = json.loads(
entity_list.get("allAttrib") or "{}"
)
entity_list["attrib"] = {
attrib_name: all_attrib.get(attrib_name)
for attrib_name in available_attribs
}
self._convert_entity_data(entity_list)
yield entity_list
def get_entity_list_rest(
self, project_name: str, list_id: str
) -> Optional[dict[str, Any]]:
"""Get entity list by id using REST API.
Args:
project_name (str): Project name.
list_id (str): Entity list id.
Returns:
Optional[dict[str, Any]]: Entity list data or None if not found.
"""
response = self.get(f"projects/{project_name}/lists/{list_id}")
response.raise_for_status()
return response.data
def get_entity_list_by_id(
self,
project_name: str,
list_id: str,
fields: Optional[Iterable[str]] = None,
) -> Optional[dict[str, Any]]:
"""Get entity list by id using GraphQl.
Args:
project_name (str): Project name.
list_id (str): Entity list id.
fields (Optional[Iterable[str]]): Fields to fetch from server.
Returns:
Optional[dict[str, Any]]: Entity list data or None if not found.
"""
for entity_list in self.get_entity_lists(
project_name, list_ids=[list_id], active=None, fields=fields
):
return entity_list
return None
def create_entity_list(
self,
project_name: str,
entity_type: EntityListEntityType,
label: str,
*,
list_type: Optional[str] = None,
access: Optional[dict[str, Any]] = None,
attrib: Optional[list[dict[str, Any]]] = None,
data: Optional[list[dict[str, Any]]] = None,
tags: Optional[list[str]] = None,
template: Optional[dict[str, Any]] = None,
owner: Optional[str] = None,
active: Optional[bool] = None,
items: Optional[list[dict[str, Any]]] = None,
list_id: Optional[str] = None,
) -> str:
"""Create entity list.
Args:
project_name (str): Project name where entity list lives.
entity_type (EntityListEntityType): Which entity types can be
used in list.
label (str): Entity list label.
list_type (Optional[str]): Entity list type.
access (Optional[dict[str, Any]]): Access control for entity list.
attrib (Optional[dict[str, Any]]): Attribute values of
entity list.
data (Optional[dict[str, Any]]): Custom data of entity list.
tags (Optional[list[str]]): Entity list tags.
template (Optional[dict[str, Any]]): Dynamic list template.
owner (Optional[str]): New owner of the list.
active (Optional[bool]): Change active state of entity list.
items (Optional[list[dict[str, Any]]]): Initial items in
entity list.
list_id (Optional[str]): Entity list id.
"""
if list_id is None:
list_id = create_entity_id()
kwargs = {
"id": list_id,
"entityType": entity_type,
"label": label,
}
for key, value in (
("entityListType", list_type),
("access", access),
("attrib", attrib),
("template", template),
("tags", tags),
("owner", owner),
("data", data),
("active", active),
("items", items),
):
if value is not None:
kwargs[key] = value
response = self.post(
f"projects/{project_name}/lists",
**kwargs
)
response.raise_for_status()
return list_id
def update_entity_list(
self,
project_name: str,
list_id: str,
*,
label: Optional[str] = None,
access: Optional[dict[str, Any]] = None,
attrib: Optional[list[dict[str, Any]]] = None,
data: Optional[list[dict[str, Any]]] = None,
tags: Optional[list[str]] = None,
owner: Optional[str] = None,
active: Optional[bool] = None,
) -> None:
"""Update entity list.
Args:
project_name (str): Project name where entity list lives.
list_id (str): Entity list id that will be updated.
label (Optional[str]): New label of entity list.
access (Optional[dict[str, Any]]): Access control for entity list.
attrib (Optional[dict[str, Any]]): Attribute values of
entity list.
data (Optional[dict[str, Any]]): Custom data of entity list.
tags (Optional[list[str]]): Entity list tags.
owner (Optional[str]): New owner of the list.
active (Optional[bool]): Change active state of entity list.
"""
kwargs = {
key: value
for key, value in (
("label", label),
("access", access),
("attrib", attrib),
("data", data),
("tags", tags),
("owner", owner),
("active", active),
)
if value is not None
}
response = self.patch(
f"projects/{project_name}/lists/{list_id}",
**kwargs
)
response.raise_for_status()
def delete_entity_list(self, project_name: str, list_id: str) -> None:
"""Delete entity list from project.
Args:
project_name (str): Project name.
list_id (str): Entity list id that will be removed.
"""
response = self.delete(f"projects/{project_name}/lists/{list_id}")
response.raise_for_status()
def get_entity_list_attribute_definitions(
self, project_name: str, list_id: str
) -> list[EntityListAttributeDefinitionDict]:
"""Get attribute definitioins on entity list.
Args:
project_name (str): Project name.
list_id (str): Entity list id.
Returns:
list[EntityListAttributeDefinitionDict]: List of attribute
definitions.
"""
response = self.get(
f"projects/{project_name}/lists/{list_id}/attributes"
)
response.raise_for_status()
return response.data
def set_entity_list_attribute_definitions(
self,
project_name: str,
list_id: str,
attribute_definitions: list[EntityListAttributeDefinitionDict],
) -> None:
"""Set attribute definitioins on entity list.
Args:
project_name (str): Project name.
list_id (str): Entity list id.
attribute_definitions (list[EntityListAttributeDefinitionDict]):
List of attribute definitions.
"""
response = self.raw_put(
f"projects/{project_name}/lists/{list_id}/attributes",
json=attribute_definitions,
)
response.raise_for_status()
def create_entity_list_item(
self,
project_name: str,
list_id: str,
*,
position: Optional[int] = None,
label: Optional[str] = None,
attrib: Optional[dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
tags: Optional[list[str]] = None,
item_id: Optional[str] = None,
) -> str:
"""Create entity list item.
Args:
project_name (str): Project name where entity list lives.
list_id (str): Entity list id where item will be added.
position (Optional[int]): Position of item in entity list.
label (Optional[str]): Label of item in entity list.
attrib (Optional[dict[str, Any]]): Item attribute values.
data (Optional[dict[str, Any]]): Item data.
tags (Optional[list[str]]): Tags of item in entity list.
item_id (Optional[str]): Id of item that will be created.
Returns:
str: Item id.
"""
if item_id is None:
item_id = create_entity_id()
kwargs = {
"id": item_id,
"entityId": list_id,
}
for key, value in (
("position", position),
("label", label),
("attrib", attrib),
("data", data),
("tags", tags),
):
if value is not None:
kwargs[key] = value
response = self.post(
f"projects/{project_name}/lists/{list_id}/items",
**kwargs
)
response.raise_for_status()
return item_id
def update_entity_list_items(
self,
project_name: str,
list_id: str,
items: list[dict[str, Any]],
mode: EntityListItemMode,
) -> None:
"""Update items in entity list.
Args:
project_name (str): Project name where entity list live.
list_id (str): Entity list id.
items (list[dict[str, Any]]): Entity list items.
mode (EntityListItemMode): Mode of items update.
"""
response = self.patch(
f"projects/{project_name}/lists/{list_id}/items",
items=items,
mode=mode,
)
response.raise_for_status()
def update_entity_list_item(
self,
project_name: str,
list_id: str,
item_id: str,
*,
new_list_id: Optional[str],
position: Optional[int] = None,
label: Optional[str] = None,
attrib: Optional[dict[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
tags: Optional[list[str]] = None,
) -> None:
"""Update item in entity list.
Args:
project_name (str): Project name where entity list live.
list_id (str): Entity list id where item lives.
item_id (str): Item id that will be removed from entity list.
new_list_id (Optional[str]): New entity list id where item will be
added.
position (Optional[int]): Position of item in entity list.
label (Optional[str]): Label of item in entity list.
attrib (Optional[dict[str, Any]]): Attributes of item in entity
list.
data (Optional[dict[str, Any]]): Custom data of item in
entity list.
tags (Optional[list[str]]): Tags of item in entity list.
"""
kwargs = {}
for key, value in (
("entityId", new_list_id),
("position", position),
("label", label),
("attrib", attrib),
("data", data),
("tags", tags),
):
if value is not None:
kwargs[key] = value
response = self.patch(
f"projects/{project_name}/lists/{list_id}/items/{item_id}",
**kwargs,
)
response.raise_for_status()
def delete_entity_list_item(
self,
project_name: str,
list_id: str,
item_id: str,
) -> None:
"""Delete item from entity list.
Args:
project_name (str): Project name where entity list live.
list_id (str): Entity list id from which item will be removed.
item_id (str): Item id that will be removed from entity list.
"""
response = self.delete(
f"projects/{project_name}/lists/{list_id}/items/{item_id}",
)
response.raise_for_status()