forked from azagoruyko/rigBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
779 lines (603 loc) · 23.3 KB
/
classes.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
import os
import sys
import re
import glob
import json
import uuid
import xml.etree.ElementTree as ET
from .utils import copyJson
ModulesAPI = {} # updated at the end
if sys.version_info.major > 2:
RigBuilderPath = os.path.dirname(__file__)
RigBuilderLocalPath = os.path.expandvars("$USERPROFILE\\rigBuilder")
else:
RigBuilderPath = os.path.dirname(__file__.decode(sys.getfilesystemencoding()))
RigBuilderLocalPath = os.path.expandvars("$USERPROFILE\\rigBuilder").decode(sys.getfilesystemencoding())
def getUidFromFile(path):
if path.endswith(".xml"):
with open(path, "r") as f:
l = f.readline() # read first line
r = re.search("uid=\"(\\w*)\"", l)
if r:
return r.group(1)
def calculateRelativePath(path, root):
path = os.path.normpath(path)
path = path.replace(os.path.normpath(root)+"\\", "")
return path
def categorizeFilesByModTime(files):
from datetime import datetime, timedelta
now = datetime.now()
categories = {
"Less 1 day ago": [],
"Less 1 week ago": [],
"Others": []
}
count = 0
for file in files:
mod_time = datetime.fromtimestamp(os.path.getmtime(file))
time_diff = now - mod_time
count += 1
if time_diff <= timedelta(days=1):
categories["Less 1 day ago"].append(file)
elif time_diff <= timedelta(weeks=1):
categories["Less 1 week ago"].append(file)
else:
categories["Others"].append(file)
count -= 1
return categories, count
class ExitModuleException(Exception):pass
class AttributeResolverError(Exception):pass
class AttributeExpressionError(Exception):pass
class ModuleNotFoundError(Exception):pass
class CopyJsonError(Exception):pass
class Attribute(object):
def __init__(self):
self._name = ""
self._category = ""
self._template = ""
self._connect = "" # attribute connection, format: /a/b/c, where c is attr, a/b is a parent relative path
self._expression = "" # python code
self._modified = False
self._module = None
self._data = {}
def copy(self):
attr = Attribute()
attr._name = self._name
attr._category = self._category
attr._template = self._template
attr._connect = self._connect
attr._expression = self._expression
attr._modified = self._modified
attr._module = self._module
attr._data = copyJson(self._data)
return attr
def name(self):
return self._name
def setName(self, name):
if name != self._name:
self._name = name
self._modified = True
def category(self):
return self._category
def setCategory(self, category):
if category != self._category:
self._category = category
self._modified = True
def template(self):
return self._template
def setTemplate(self, template):
if template != self._template:
self._template = template
self._modified = True
def connect(self):
return self._connect
def setConnect(self, connect):
if connect != self._connect:
self._connect = connect
self._modified = True
def expression(self):
return self._expression
def setExpression(self, expression):
if expression != self._expression:
self._expression = expression
self._modified = True
def modified(self):
return self._modified
def module(self):
return self._module
def _defaultValue(self):
return copyJson(self._data[self._data["default"]])
def _setDefaultValue(self, value):
newValue = copyJson(value)
if newValue != self._defaultValue():
self._data[self._data["default"]] = newValue
self._modified = True
def data(self): # return actual read-only copy of all data
self.pull()
return copyJson(self._data)
def localData(self):
return copyJson(self._data)
def setLocalData(self, data):
newData = copyJson(data)
if newData != self._data:
self._data = newData
self._modified = True
def setData(self, data):
self.setLocalData(data)
self.push()
def pull(self):
if self._connect:
srcAttr = self.findConnectionSource()
if srcAttr:
srcAttr.pull()
srcAttr.executeExpression()
self._setDefaultValue(srcAttr._defaultValue())
self.executeExpression()
def push(self):
if self._connect:
srcAttr = self.findConnectionSource()
if srcAttr:
srcAttr._setDefaultValue(self._defaultValue())
srcAttr.push()
def get(self, key=None):
self.pull()
return self._defaultValue() if not key else copyJson(self._data.get(key))
def set(self, value, key=None):
try:
valueCopy = copyJson(value)
except TypeError:
raise CopyJsonError("Cannot set non-JSON data (got {})".format(value))
if not key:
self._setDefaultValue(valueCopy)
self.push()
else:
if self._data.get(key) != valueCopy:
self._data[key] = valueCopy
self._modified = True
self.push()
def executeExpression(self):
if not self._expression:
return
localEnv = dict(self._module.getEnv())
localEnv.update({"data": self._data, "value": self._defaultValue()})
try:
exec(self._expression, localEnv)
except Exception as e:
raise AttributeExpressionError("Invalid expression: {}".format(str(e)))
else:
self._setDefaultValue(localEnv["value"])
def findConnectionSource(self):
if self._module and self._module._parent and self._connect:
srcAttr = self._module._parent.findAttributeByPath(self._connect)
return srcAttr
def listConnections(self):
def _listConnections(currentModule):
connections = []
for ch in currentModule._children:
if ch is not self._module: # not self
for attr in ch._attributes:
if attr._connect:
a = attr.findConnectionSource()
if a is self:
connections.append(attr)
connections.extend(_listConnections(ch))
return connections
return _listConnections(self._module.root())
def toXml(self, *, keepConnection=True):
attrs = [("name", self._name),
("template", self._template),
("category", self._category),
("connect", self._connect if keepConnection else "")]
attrsStr = " ".join(["{}=\"{}\"".format(k, v) for k, v in attrs])
data = dict(self._data) # here data can have additional keys for storing custom data
if self._expression:
data["_expression"] = self._expression
header = "<attr {attribs}><![CDATA[{data}]]></attr>"
return header.format(attribs=attrsStr, data=json.dumps(data))
@staticmethod
def fromXml(root):
attr = Attribute()
attr._name = root.attrib["name"]
attr._template = root.attrib["template"]
attr._category = root.attrib["category"]
attr._connect = root.attrib["connect"]
attr._data = json.loads(root.text)
# additional data
attr._expression = attr._data.pop("_expression", "")
return attr
class Dict(dict):
def __init__(self):
pass
def __getattr__(self, name):
return self.get(name)
def __setattr__(self, name, value):
self[name] = value
class AttrsWrapper(object): # attributes getter/setter
def __init__(self, module):
self._module = module
def __getattr__(self, name):
module = object.__getattribute__(self, "_module")
attr = module.findAttribute(name)
if attr:
return attr
else:
raise AttributeError("Attribute '{}' not found".format(name))
def __setattr__(self, name, value): # for code like 'module.attr.input = value'
if name == "_module":
object.__setattr__(self, "_module", value)
else:
module = object.__getattribute__(self, "_module")
attr = module.findAttribute(name)
if attr:
attr.set(value)
else:
raise AttributeError("Attribute '{}' not found".format(name))
class DataAccessor(): # for accessing data with @_data suffix inside a module's code
def __init__(self, attr):
self._attr = attr
def __getitem__(self, name):
return self._attr.get(name)
def __setitem__(self, name, value):
self._attr.set(value, name)
def __str__(self):
return json.dumps(self._attr.data())
class Module(object):
UpdateSource = "all" # all, server, local, (empty)
LocalUids = {}
ServerUids = {}
glob = Dict() # global memory
env = {}
def __init__(self, spec=None):
self._uid = "" # unique ids are assigned while saving
self._name = ""
self._runCode = ""
self._parent = None
self._children = []
self._attributes = []
self._muted = False
self._filePath = ""
self._modified = False
self.attr = AttrsWrapper(self) # attributes accessor
if spec:
self = Module.loadModule(spec)
def copy(self):
module = Module()
module._name = self._name
module._uid = self._uid
module._runCode = self._runCode
for a in self._attributes:
module.addAttribute(a.copy())
for ch in self._children:
module.addChild(ch.copy())
module._parent = self._parent
module._filePath = self._filePath
module._muted = self._muted
module._modified = self._modified
return module
def name(self):
return self._name
def setName(self, name):
self._name = name
def uid(self):
return self._uid
def parent(self):
return self._parent
def filePath(self):
return self._filePath
def modified(self):
return self._modified
def muted(self):
return self._muted
def mute(self):
self._muted = True
def unmute(self):
self._muted = False
def runCode(self):
return self._runCode
def setRunCode(self, code):
self._runCode = code
self._modified = True
def root(self):
return self._parent.root() if self._parent else self
def children(self):
return list(self._children)
def child(self, nameOrIndex):
if type(nameOrIndex) == int:
return self.children()[nameOrIndex]
elif type(nameOrIndex) == str:
return self.findChild(nameOrIndex)
def insertChild(self, idx, child):
child._parent = self
self._children.insert(idx, child)
self._modified = True
def addChild(self, child):
self.insertChild(len(self._children), child)
def removeChild(self, child):
child._parent = None
self._children.remove(child)
self._modified = True
def removeChildren(self):
for ch in self._children:
ch._parent = None
self._children = []
self._modified = True
def findChild(self, name):
for ch in self._children:
if ch._name == name:
return ch
def attributes(self):
return list(self._attributes)
def insertAttribute(self, idx, attr):
attr._module = self
self._attributes.insert(idx, attr)
self._modified = True
def addAttribute(self, attr):
self.insertAttribute(len(self._attributes), attr)
def removeAttribute(self, attr):
attr._module = None
self._attributes.remove(attr)
self._modified = True
def removeAttributes(self):
for a in self._attributes:
a._module = None
self._attributes = []
self._modified = True
def findAttribute(self, name):
for a in self._attributes:
if a._name == name:
return a
def _clearModificationFlag(self, *, modules=True, attributes=True, recursive=True):
if modules:
self._modified = False
if attributes:
for a in self._attributes:
a._modified = False
for ch in self._children:
if not ch._uid:
ch._clearModificationFlag(recursive=recursive, modules=modules, attributes=attributes)
else:
ch._clearModificationFlag(recursive=False, modules=False, attributes=attributes)
def ch(self, path, key=None):
attr = self.findAttributeByPath(path)
return attr.get(key)
def chdata(self, path):
attr = self.findAttributeByPath(path)
return attr.data() # actual read-only copy
def chset(self, path, value, key=None):
attr = self.findAttributeByPath(path)
attr.set(value, key)
def toXml(self, *, keepConnections=True):
attrs = [("name", self._name),
("muted", int(self._muted)),
("uid", self._uid)]
attrsStr = " ".join(["{}=\"{}\"".format(k,v) for k, v in attrs])
template = ["<module {}>".format(attrsStr)]
template.append("".join(["<run>",
"<![CDATA[", self._runCode, "]]>",
"</run>"]))
template.append("<attributes>")
template += [a.toXml(keepConnection=keepConnections) for a in self._attributes]
template.append("</attributes>")
template.append("<children>")
template += [ch.toXml(keepConnections=True) for ch in self._children] # keep inner connections
template.append("</children>")
template.append("</module>")
return "\n".join(template)
@staticmethod
def fromXml(root):
module = Module()
module._name = root.attrib["name"]
module._uid = root.attrib.get("uid", "")
module._muted = int(root.attrib["muted"])
module._runCode = root.findtext("run")
for ch in root.find("attributes").findall("attr"):
module.addAttribute(Attribute.fromXml(ch))
for ch in root.find("children").findall("module"):
module.addChild(Module.fromXml(ch))
module._modified = False
return module
def loadedFromServer(self):
return self._filePath.startswith(os.path.normpath(RigBuilderPath+"/modules/"))
def loadedFromLocal(self):
return self._filePath.startswith(os.path.normpath(RigBuilderLocalPath+"/modules/"))
def referenceFile(self):
local = Module.LocalUids.get(self._uid)
server = Module.ServerUids.get(self._uid)
path = {"all": local or server, "server": server, "local": local, "":self._filePath}.get(Module.UpdateSource)
return path
def relativePath(self):
if self.loadedFromServer():
return calculateRelativePath(self._filePath, RigBuilderPath+"/modules")
elif self.loadedFromLocal():
return calculateRelativePath(self._filePath, RigBuilderLocalPath+"/modules")
else:
return self._filePath
def relativePathString(self): # relative loaded path or ../folder/child/module.xml
if not self._filePath:
return ""
path = ""
if self.loadedFromServer() or self.loadedFromLocal():
path = self.relativePath()
else:
normLoadedPath = self._filePath.replace("\\", "/")
items = normLoadedPath.split("/")
MaxPathItems = 3
if len(items) > MaxPathItems: # c: folder child module.xml
path = "../"+"/".join(items[-MaxPathItems:])
else:
path = normLoadedPath
return path.replace(".xml", "")
def getSavePath(self):
if self.loadedFromServer():
relativePath = os.path.relpath(self._filePath, RigBuilderPath+"/modules")
return os.path.normpath(RigBuilderLocalPath+"/modules/"+relativePath)
else: # local or somewhere else
return self._filePath
def embed(self):
self._uid = ""
self._filePath = ""
self._modified = True
def update(self):
origPath = self.referenceFile()
if origPath:
origModule = Module.loadFromFile(origPath)
attributes = []
for origAttr in origModule._attributes:
foundAttr = self.findAttribute(origAttr._name)
if origAttr._name and foundAttr and foundAttr._template == origAttr._template: # skip empty named attrs, use first found
origAttr._setDefaultValue(foundAttr._defaultValue()) # keep attribute value
origAttr._connect = foundAttr._connect
origAttr._expression = foundAttr._expression
origAttr._modified = False # clear modification flag
attributes.append(origAttr)
self._attributes = []
for a in attributes:
self.addAttribute(a)
self._children = []
for ch in origModule._children:
self.addChild(ch)
self._runCode = origModule._runCode
self._filePath = origModule._filePath
self._modified = False
for ch in self._children:
ch.update()
def sendToServer(self): # save the module on server, remove locally
if self.loadedFromLocal():
savePath = os.path.normpath(RigBuilderPath+"/modules/"+self.relativePath())
if not os.path.exists(os.path.dirname(savePath)):
os.makedirs(os.path.dirname(savePath))
oldPath = self._filePath
self.saveToFile(savePath)
os.unlink(oldPath) # remove local file
Module.ServerUids[self._uid] = savePath
Module.LocalUids.pop(self._uid, None) # remove from local uids
return savePath
def saveToFile(self, fileName, *, newUid=False):
if not self._uid or newUid:
self._uid = uuid.uuid4().hex
with open(os.path.realpath(fileName), "w") as f: # resolve links
f.write(self.toXml(keepConnections=False)) # don't keep outer connections
self._filePath = os.path.normpath(fileName)
self._clearModificationFlag()
@staticmethod
def loadFromFile(fileName):
m = Module.fromXml(ET.parse(fileName).getroot())
m._filePath = os.path.normpath(fileName)
m._muted = False
return m
@staticmethod
def loadModule(spec): # spec can be full path, relative path or uid
modulePath = Module.LocalUids.get(spec) or Module.ServerUids.get(spec) # check local, then server uids
if not modulePath: # otherwise, find by path
specPath = os.path.expandvars(spec)
for path in [specPath, # absolute path
specPath+".xml",
RigBuilderLocalPath+"/modules/"+spec, # local path
RigBuilderLocalPath+"/modules/"+spec+".xml",
RigBuilderPath+"/modules/"+spec, # server path
RigBuilderPath+"/modules/"+spec+".xml"]:
if os.path.exists(path):
modulePath = path
break
if not modulePath:
raise ModuleNotFoundError("Module '{}' not found".format(spec))
module = Module.loadFromFile(modulePath)
module.update()
return module
@staticmethod
def listModules(path):
files = []
for f in sorted(glob.iglob(path+"/*")):
if os.path.isdir(f):
files += Module.listModules(f)
else:
if f.endswith(".xml"):
files.append(f)
return files
def path(self, inclusive=True):
if not self._parent:
return self._name
return self._parent.path() + ("/" + self._name if inclusive else "")
def findAttributeByPath(self, path):
'''
Return attribute by path, where path is /a/b/c, where c is attr, a/b is a parent relative path
'''
*moduleList, attrName = path.split("/")
currentParent = self
for module in moduleList:
if not module:
continue
if module == "..":
currentParent = currentParent._parent
continue
elif module == ".":
continue
ch = currentParent.findChild(module)
if ch:
currentParent = ch
else:
raise AttributeResolverError("Cannot resolve '{}' path".format(path))
attr = currentParent.findAttribute(attrName)
if not attr:
raise AttributeResolverError("Cannot find '{}' attribute".format(path))
return attr
def getEnv(self):
env = dict(ModulesAPI)
env.update({"module": self,
"ch": self.ch,
"chdata": self.chdata,
"chset": self.chset})
return env
def run(self, *, uiCallback=None):
localEnv = dict(Module.env or {})
localEnv.update(self.getEnv())
attrPrefix = "attr_"
for attr in self._attributes:
localEnv[attrPrefix+attr._name] = attr.get()
localEnv[attrPrefix+"set_"+attr._name] = attr.set
localEnv[attrPrefix+attr._name+"_data"] = DataAccessor(attr)
print("{} is running...".format(self.path()))
if callable(uiCallback):
uiCallback(self)
try:
exec(self._runCode.replace("@", attrPrefix), localEnv)
except ExitModuleException:
pass
for ch in self._children:
if not ch.muted():
ch.run(uiCallback=uiCallback)
return localEnv
@staticmethod
def updateUidsCache():
Module.ServerUids = Module.findUids(RigBuilderPath + "/modules")
Module.LocalUids = Module.findUids(RigBuilderLocalPath + "/modules")
@staticmethod
def findUids(path):
uids = {}
for fpath in sorted(glob.iglob(path+"/*")):
if os.path.isdir(fpath):
dirUids = Module.findUids(fpath)
for k in dirUids:
uids[k] = dirUids[k]
elif fpath.endswith(".xml"):
uid = getUidFromFile(fpath)
if uid:
uids[uid] = fpath
return uids
def printError(msg):
raise RuntimeError(msg)
def printWarning(msg):
print("Warning: "+msg)
def exitModule():
raise ExitModuleException()
ModulesAPI.update({
"module":None, # updated at runtime
"Module": Module,
"ch": None, # updated at runtime
"chdata": None, # updated at runtime
"chset": None, # updated at runtime
"copyJson": copyJson,
"exit": exitModule,
"error": printError,
"warning": printWarning})
Module.updateUidsCache()