forked from Nvizible/sg_wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsg_wrapper.py
executable file
·357 lines (291 loc) · 13.3 KB
/
sg_wrapper.py
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
#import shotgun_api3
# The Primary Text Keys are the field names to check when not defined.
# For example, calling sg.Project("my_project") will be the same as
# sg.Project(code = "my_project")
primaryTextKeys = ["code", "login"]
# For most entity types, the pluralise() function will define what the plural
# version of the entity name is.
# This dictionary defines any custom plural forms that we might want to have.
customPlural = {'Person': "People"}
# This is the base Shotgun class. Everything is created from here, and it deals
# with talking to the
# standard Shotgun API.
class Shotgun():
def __init__(self, sg, cache_enable=True):
self._sg = sg
self._cache_enable = cache_enable
self._entity_types = self.get_entity_list()
self._entity_fields = {}
self._entities = {}
self._entity_searches = []
def pluralise(self, name):
if name in customPlural:
return customPlural[name]
if name[-1] == "y" and name[-3:] != "Day":
return name[:-1] + "ies"
if name[-1] in ["s", "h"]:
return name + "es"
return name + "s"
def get_entity_list(self):
entitySchema = self._sg.schema_entity_read()
entities = []
for e in entitySchema:
newEntity = {'type': e, 'name': entitySchema[e]['name']['value'].replace(" ", ""), 'fields': []}
newEntity['type_plural'] = self.pluralise(newEntity['type'])
newEntity['name_plural'] = self.pluralise(newEntity['name'])
entities.append(newEntity)
return entities
def get_entity_field_list(self, entityType):
fields = self.get_entity_fields(entityType)
return fields.keys()
def get_entity_fields(self, entityType):
if entityType not in self._entity_fields:
self._entity_fields[entityType] = self._sg.schema_field_read(entityType)
return self._entity_fields[entityType]
def is_entity(self, entityType):
for e in self._entity_types:
if entityType in [e['type'], e['name']]:
return True
return False
def is_entity_plural(self, entityType):
for e in self._entity_types:
if entityType in [e['type_plural'], e['name_plural']]:
return True
return False
def find_entity(self, entityType, key = None, find_one = True,
fields = None, exclude_fields = None, sg_filters= None,
sg_order = [], sg_filter_operator = 'all', sg_limit = 0,
sg_retired_only = False, sg_page = 0, **kwargs):
filters = {}
thisEntityType = None
thisEntityFields = None
for e in self._entity_types:
if entityType in [e['type'], e['name'], e['type_plural'], e['name_plural']]:
thisEntityType = e['type']
if not e['fields']:
e['fields'] = self.get_entity_field_list(thisEntityType)
thisEntityFields = e['fields']
if key:
if type(key) == int:
filters['id'] = key
elif type(key) == str:
foundPrimaryKey = False
for fieldName in primaryTextKeys:
if fieldName in thisEntityFields:
filters[fieldName] = key
foundPrimaryKey = True
break
if not foundPrimaryKey:
raise Exception("Entity type '%s' does not have one of the defined primary keys(%s)." % (entityType, ", ".join(primaryTextKeys)))
for arg in kwargs:
if isinstance(kwargs[arg], Entity):
filters[arg] = {'type': kwargs[arg].entity_type(), 'id': kwargs[arg].entity_id()}
else:
filters[arg] = kwargs[arg]
if self._cache_enable and 'id' in filters:
if thisEntityType in self._entities and filters['id'] in self._entities[thisEntityType]:
return self._entities[thisEntityType][filters['id']]
if not fields:
fields = self.get_entity_field_list(thisEntityType)
if exclude_fields:
for f in exclude_fields:
if f in fields:
fields.remove(f)
if self._cache_enable:
for search in self._entity_searches:
if search['find_one'] == find_one \
and search['entity_type'] == thisEntityType \
and search['filters'] == filters \
and search['sg_filters'] == sg_filters \
and search['sg_order'] == sg_order \
and search['sg_filter_operator'] == sg_filter_operator \
and search['sg_limit'] == sg_limit \
and search['sg_retired_only'] == sg_retired_only \
and search['sg_page'] == sg_page \
and set(fields).issubset(set(search['fields'])):
return search['result']
sgFilters = []
for f in filters:
sgFilters.append([f, 'is', filters[f]])
if sg_filters:
sgFilters += sg_filters
print
result = None
if find_one:
sg_result = self.sg_find_one(thisEntityType, sgFilters, fields,
sg_order, sg_filter_operator)
if sg_result:
result = Entity(self, thisEntityType, sg_result)
else:
sg_results = self.sg_find(thisEntityType, sgFilters, fields,
sg_order, sg_filter_operator, sg_limit, sg_retired_only, sg_page)
result = []
for sg_result in sg_results:
result.append(Entity(self, thisEntityType, sg_result))
thisSearch = {}
thisSearch['find_one'] = find_one
thisSearch['entity_type'] = thisEntityType
thisSearch['filters'] = filters
thisSearch['sg_filters'] = sg_filters
thisSearch['sg_order'] = sg_order
thisSearch['sg_filter_operator'] = sg_filter_operator
thisSearch['sg_limit'] = sg_limit
thisSearch['sg_retired_only'] = sg_retired_only
thisSearch['sg_page'] = sg_page
thisSearch['fields'] = fields
thisSearch['result'] = result
self._entity_searches.append(thisSearch)
return result
def create(self, entity, fields):
data = {}
for f in fields:
if isinstance(entity[f], Entity):
ent = entity.field(f)
data[f] = {'id': ent.entity_id(), 'type': ent.entity_type()}
else:
data[f] = entity[f]
return self._sg.create(entity.entity_type(), data, entity.fields())
def delete_entity(self, entity):
self.unregister_entity(entity)
return self._sg.delete(entity._entity_type, entity._entity_id)
def sg_find_one(self, entityType, filters, fields, *args, **kwargs):
return self._sg.find_one(entityType, filters, fields, *args, **kwargs)
def sg_find(self, entityType, filters, fields, *args, **kwargs):
return self._sg.find(entityType, filters, fields, *args, **kwargs)
def update(self, entity, updateFields):
updateData = {}
for f in updateFields:
if isinstance(entity.field(f), Entity):
ent = entity.field(f)
updateData[f] = {'id': ent.entity_id(), 'type': ent.entity_type()}
else:
updateData[f] = entity.field(f)
self._sg.update(entity._entity_type, entity._entity_id, updateData)
def unregister_entity(self, entity):
if entity._entity_type in self._entities and entity._entity_id in self._entities[entity._entity_type]:
del self._entities[entity._entity_type][entity._entity_id]
def register_entity(self, entity):
if entity._entity_type not in self._entities:
self._entities[entity._entity_type] = {}
#if entity._entity_id not in self._entities[entity._entity_type]:
self._entities[entity._entity_type][entity._entity_id] = entity
def clear_cache(self):
self._entities = {}
self._entity_searches = []
def __getattr__(self, attrName):
def find_entity_wrapper(*args, **kwargs):
return self.find_entity(attrName, find_one = True, *args, **kwargs)
def find_multi_entity_wrapper(*args, **kwargs):
return self.find_entity(attrName, find_one = False, *args, **kwargs)
if self.is_entity(attrName):
return find_entity_wrapper
elif self.is_entity_plural(attrName):
return find_multi_entity_wrapper
def commit_all(self):
for entityType in self._entities:
for entityId in self._entities[entityType]:
entity = self._entities[entityType][entityId]
if entity.modified_fields():
entity.commit()
class Entity():
def __init__(self, shotgun, entity_type, fields):
self._entity_type = entity_type
self._shotgun = shotgun
self._fields = fields
self._fields_changed = {}
self._sg_filters = []
self._entity_id = self._fields['id']
if self._entity_id:
self._shotgun.register_entity(self)
def reload(self):
#self._field_names = self._shotgun.get_entity_field_list(self._entity_type)
field_names = self.fields()
self._fields = self._shotgun.sg_find_one(
self._entity_type,
[["id", "is", self._entity_id]],
fields = field_names)
def fields(self):
return self._fields.keys()
def entity_type(self):
return self._entity_type
def entity_id(self):
return self._entity_id
def field(self, fieldName):
if fieldName in self._fields:
attribute = self._fields[fieldName]
if type(attribute) == dict and 'id' in attribute and 'type' in attribute:
if 'entity' not in attribute:
attribute['entity'] = self._shotgun.find_entity(attribute['type'], id = attribute['id'])
#attribute['entity'] = Entity(self._shotgun, attribute['type'], {'id': attribute['id']})
return attribute['entity']
elif type(attribute) == list:
iterator = self.list_iterator(self._fields[fieldName])
attrResult = []
for item in iterator:
attrResult.append(item)
return attrResult
else:
return self._fields[fieldName]
raise AttributeError("Entity '%s' has no field '%s'" % (self._entity_type, fieldName))
def list_iterator(self, entities):
for entity in entities:
if type(entity) == dict and 'id' in entity and 'type' in entity:
if 'entity' not in entity:
entity['entity'] = self._shotgun.find_entity(entity['type'], id=entity['id'])
#entity['entity'] = Entity(self._shotgun, entity['type'], {'id': entity['id']})
yield entity['entity']
else:
yield entity
def modified_fields(self):
return self._fields_changed.keys()
def save(self):
fields = self._shotgun.create(self, self._fields_changed.keys())
self._fields = fields
self._entity_id = self._fields['id']
self._shotgun.register_entity(self)
def commit(self):
if not self.modified_fields():
return False
if not self.entity_id():
self.save()
else:
self._shotgun.update(self, self._fields_changed.keys())
self._fields_changed = {}
return True
def revert(self, revert_fields=None):
if revert_fields is None:
revert_fields = self.modified_fields()
elif type(revert_fields) == "str":
revert_fields = [revert_fields]
for field in self.modified_fields():
if field in revert_fields:
self._fields[field] = self._fields_changed[field]
del self._fields_changed[field]
def set_field(self, fieldName, value):
entityFields = self._shotgun.get_entity_fields(self._entity_type)
if fieldName in entityFields:
if entityFields[fieldName]['editable']['value'] is True:
oldValue = self._fields[fieldName]
if isinstance(value, Entity):
self._fields[fieldName] = {'id': value.entity_id(), 'type': value.entity_type(), 'entity':value}
else:
self._fields[fieldName] = value
if fieldName not in self._fields_changed:
self._fields_changed[fieldName] = oldValue
else:
raise AttributeError("Field '%s' in Entity '%s' is not editable" % (fieldName, self._entity_type))
else:
raise AttributeError("Entity '%s' has no field '%s'" % (self._entity_type, fieldName))
def __getattr__(self, attrName):
return self.field(attrName)
def __setattr__(self, attrName, value):
if attrName[0] == "_":
self.__dict__[attrName] = value
return
self.set_field(attrName, value)
def __getitem__(self, itemName):
return self.field(itemName)
def __setitem__(self, itemName, value):
self.set_field(itemName, value)
def __str__(self):
return "Entity %s id %s" % (self._entity_type, self._entity_id)