/
utilitytypes.py
1345 lines (1109 loc) · 44.8 KB
/
utilitytypes.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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Defines common types and type related utilities: Singleton, etc.
These types can be shared by other utils modules and imported into util main namespace for use by other pymel modules
"""
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future.utils import PY2, with_metaclass
# 2to3: remove switch when python-3 only
if PY2:
from collections import Mapping
else:
from collections.abc import Mapping
from builtins import object
import inspect
import types
import operator
import sys
import warnings
from collections import defaultdict
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, TypeVar, Union
T = TypeVar('T')
class Singleton(type):
""" Metaclass for Singleton classes.
>>> class DictSingleton(with_metaclass(Singleton, dict)) :
... pass
...
>>> DictSingleton({'A':1})
{'A': 1}
>>> a = DictSingleton()
>>> a
{'A': 1}
>>> b = DictSingleton({'B':2})
>>> a, b, DictSingleton()
({'B': 2}, {'B': 2}, {'B': 2})
>>> a is b and a is DictSingleton()
True
>>> class StringSingleton(with_metaclass(Singleton, str)) :
... pass
...
>>> StringSingleton("first")
'first'
>>> a = StringSingleton()
>>> a
'first'
>>> b = StringSingleton("changed")
>>> a, b, StringSingleton()
('first', 'first', 'first')
>>> a is b and a is StringSingleton()
True
>>> class DictSingleton2(DictSingleton):
... pass
...
>>> DictSingleton2({'A':1})
{'A': 1}
>>> a = DictSingleton2()
>>> a
{'A': 1}
>>> b = DictSingleton2({'B':2})
>>> a, b, DictSingleton2()
({'B': 2}, {'B': 2}, {'B': 2})
>>> a is b and a is DictSingleton2()
True
"""
def __new__(mcl, classname, bases, classdict):
# redefine __new__
def __new__(cls, *p, **k):
if '_the_instance' not in cls.__dict__:
cls._the_instance = super(newcls, cls).__new__(cls, *p, **k)
self = cls._the_instance
if p:
if hasattr(self, 'clear'):
self.clear()
else:
self.__init__()
return self
newdict = {'__new__': __new__}
for k in classdict:
if k in newdict:
warnings.warn("Attribute %r is predefined in class %r of "
"type %r and can't be overriden" %
(k, classname, mcl.__name__))
else:
newdict[k] = classdict[k]
newcls = super(Singleton, mcl).__new__(mcl, classname, bases, newdict)
return newcls
class metaStatic(Singleton):
""" A static (immutable) Singleton metaclass to quickly build classes
holding predefined immutable dicts
>>> class FrozenDictSingleton(with_metaclass(metaStatic, dict)) :
... pass
...
>>> FrozenDictSingleton({'A':1})
{'A': 1}
>>> a = FrozenDictSingleton()
>>> a
{'A': 1}
>>> b = FrozenDictSingleton()
>>> a, b
({'A': 1}, {'A': 1})
>>> a is b
True
>>> b = FrozenDictSingleton({'B':2})
Traceback (most recent call last):
...
TypeError: 'FrozenDictSingleton' object does not support redefinition
>>> a['A']
1
>>> a['A'] = 2 #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: '<class '...FrozenDictSingleton'>' object does not support item assignation
>>> a.clear()
Traceback (most recent call last):
...
AttributeError: 'FrozenDictSingleton' object has no attribute 'clear'
>>> a, b, FrozenDictSingleton()
({'A': 1}, {'A': 1}, {'A': 1})
>>> a is b and a is FrozenDictSingleton()
True
>>> class StaticTest(FrozenDictSingleton):
... pass
...
>>> StaticTest({'A': 1})
{'A': 1}
>>> a = StaticTest()
>>> a
{'A': 1}
>>> b = StaticTest()
>>> a, b
({'A': 1}, {'A': 1})
>>> class StaticTest2( StaticTest ):
... pass
...
>>> StaticTest2({'B': 2})
{'B': 2}
>>> a = StaticTest2()
>>> a
{'B': 2}
>>> b = StaticTest2()
>>> a, b
({'B': 2}, {'B': 2})
"""
def __new__(mcl, classname, bases, classdict):
"""
"""
# redefine __init__
def __init__(self, *p, **k):
cls = self.__class__
# Can only create once)
if p:
# Can only init once
if not self:
return super(newcls, self).__init__(*p, **k)
else:
raise TypeError("'" + classname + "' object does not support redefinition")
newdict = {'__init__': __init__}
# hide methods with might herit from a mutable base
def __getattribute__(self, name):
if name in newcls._hide:
raise AttributeError("'" + classname + "' object has no attribute '" + name + "'")
else:
return super(newcls, self).__getattribute__(name)
newdict['__getattribute__'] = __getattribute__
_hide = ('clear', 'update', 'pop', 'popitem', '__setitem__', '__delitem__', 'append', 'extend')
newdict['_hide'] = _hide
# prevent item assignation or deletion
def __setitem__(self, key, value):
raise TypeError("'%s' object does not support item assignation" % (self.__class__))
newdict['__setitem__'] = __setitem__
def __delitem__(self, key):
raise TypeError("'%s' object does not support item deletion" % (self.__class__))
newdict['__delitem__'] = __delitem__
# Now add methods of the defined class, as long as it doesn't try to redefine
# Note: could have defined the __new__ method like it is done in Singleton but it's as easy to derive from it
for k in classdict:
if k in newdict:
warnings.warn("Attribute %r is predefined in class %r of type %r and can't be overriden" % (k, classname, mcl.__name__))
else:
newdict[k] = classdict[k]
newcls = super(metaStatic, mcl).__new__(mcl, classname, bases, newdict)
return newcls
class defaultlist(list):
def __init__(self, default_factory, *args, **kwargs):
if (default_factory is not None and
not hasattr(default_factory, '__call__')):
raise TypeError('first argument must be callable')
list.__init__(self, *args, **kwargs)
self.default_factory = default_factory
def __setitem__(self, index, item):
try:
list.__setitem__(self, index, item)
except IndexError:
diff = index - len(self) - 1
assert diff > 0
self.extend([self.default_factory()] * diff + [item])
def __getitem__(self, index):
try:
return list.__getitem__(self, index)
except IndexError:
return self.default_factory()
class ModuleInterceptor(object):
"""
This class is used to intercept an unset attribute of a module to perfrom a callback. The
callback will only be performed if the attribute does not exist on the module. Any error raised
in the callback will cause the original AttributeError to be raised.
def cb( module, attr):
if attr == 'this':
print "intercepted"
else:
raise ValueError
import sys
sys.modules[__name__] = ModuleInterceptor(__name__, cb)
intercepted
The class does not work when imported into the main namespace.
"""
def __init__(self, moduleName, callback):
self.module = __import__(moduleName, globals(), locals(), [''])
self.callback = callback
def __getattr__(self, attr):
try:
return getattr(self.module, attr)
except AttributeError as msg:
try:
self.callback(self.module, attr)
except:
raise AttributeError(msg)
# read only decorator
def readonly(f):
""" Marks a class member as protected, allowing metaProtected to prevent
re-assignation on the classes it generates
"""
f.__readonly__ = None
return f
class metaReadOnlyAttr(type):
""" A metaclass to allow to define read-only class attributes, accessible either on the class or it's instances
and protected against re-write or re-definition.
Read only attributes are stored in the class '__readonly__' dictionary.
Any attribute can be marked as read only by including its name in a tuple named '__readonly__' in the class
definition. Alternatively methods can be marked as read only with the @readonly decorator and will then get
added to the dictionary at class creation """
def __setattr__(cls, name, value): # @NoSelf
""" overload __setattr__ to forbid modification of read only class info """
readonly = {}
for c in inspect.getmro(cls):
if hasattr(c, '__readonly__'):
readonly.update(c.__readonly__)
if name in readonly:
raise AttributeError("attribute %s is a read only class attribute and cannot be modified on class %s" % (name, cls.__name__))
else:
super(metaReadOnlyAttr, cls).__setattr__(name, value)
def __new__(mcl, classname, bases, classdict): # @NoSelf
""" Create a new metaReadOnlyAttr class """
# checks for protected members, in base classes on in class to be created
readonly = {}
# check for protected members in class definition
if '__readonly__' in classdict:
readonly.update(dict((a, None) for a in classdict['__readonly__']))
for a in classdict:
if hasattr(classdict[a], '__readonly__'):
readonly[a] = None
readonly['__readonly__'] = None
classdict['__readonly__'] = readonly
# create the new class
newcls = super(metaReadOnlyAttr, mcl).__new__(mcl, classname, bases, classdict)
return newcls
NOT_PROXY_WRAPPED = ['__new__', '__getattribute__', '__getattr__', '__setattr__',
'__class__', '__weakref__', '__subclasshook__',
'__reduce_ex__', '__reduce__', '__dict__', '__sizeof__',
'__module__', '__init__', '__doc__']
def proxyClass(cls, classname, dataAttrName=None, dataFuncName=None,
remove=(), makeDefaultInit=False, sourceIsImmutable=True,
module=None):
# type: (Type[T], str, str, str, Iterable[str], bool, bool, Any) -> Type[T]
"""
This function will generate a proxy class which keeps the internal data
separate from the wrapped class. This is useful for emulating immutable
types such as str and tuple, while using mutable data. Be aware that
changing data will break hashing. not sure the best solution to this,
but a good approach would be to subclass your proxy and implement
a valid __hash__ method.
Parameters
----------
cls : Type[T]
The class to wrap
classname : str
The name to give the resulting proxy class
dataAttrName : str
The name of an attribute on which an instance of the wrapped class will
be stored.
Either dataAttrname or dataFuncName must be given, but not both.
dataFuncName : str
The name of an attribute on which reside a function, which takes no
arguments, and when called, will return an instance of the wrapped
class.
Either dataAttrname or dataFuncName must be given, but not both.
remove : Iterable[str]
An iterable of name of attributes which should NOT be wrapped.
Note that certain attributes will never be wrapped - the list of
such items is found in the NOT_PROXY_WRAPPED constant.
makeDefaultInit : bool
If True and dataAttrName is True, then a 'default' __init__ function
will be created, which creates an instance of the wrapped class, and
assigns it to the dataAttr. Defaults to False
If dataAttrName is False, does nothing
sourceIsImmutable : bool
This parameter is included only for backwards compatibility - it is
ignored.
Returns
-------
TypeT
"""
assert not (dataAttrName and dataFuncName), \
'Cannot use attribute and function for data storage. Choose one or the other.'
if dataAttrName:
class ProxyAttribute(object):
def __init__(self, name):
self.name = name
def __get__(self, proxyInst, proxyClass):
if proxyInst is None:
return getattr(cls, self.name)
else:
return getattr(getattr(proxyInst, dataAttrName),
self.name)
def _methodWrapper(method):
def wrapper(self, *args, **kwargs):
return method(getattr(self, dataAttrName), *args, **kwargs)
wrapper.__doc__ = method.__doc__
wrapper.__name__ = method.__name__
return wrapper
elif dataFuncName:
class ProxyAttribute(object):
def __init__(self, name):
self.name = name
def __get__(self, proxyInst, proxyClass):
if proxyInst is None:
return getattr(cls, self.name)
else:
return getattr(getattr(proxyInst, dataFuncName)(),
self.name)
def _methodWrapper(method):
def wrapper(self, *args, **kwargs):
return method(getattr(self, dataFuncName)(), *args, **kwargs)
wrapper.__doc__ = method.__doc__
wrapper.__name__ = method.__name__
return wrapper
else:
raise TypeError('Must specify either a dataAttrName or a dataFuncName')
class Proxy(object):
# make a default __init__ which sets the dataAttr...
# if __init__ is in remove, or dataFuncName given,
# user must supply own __init__, and set the dataAttr/dataFunc
# themselves
if makeDefaultInit and dataAttrName:
def __init__(self, *args, **kwargs):
# We may wrap __setattr__, so don't use 'our' __setattr__!
object.__setattr__(self, dataAttrName, cls(*args, **kwargs))
# For 'type' objects, you can't set the __doc__ outside of
# the class definition, so do it here:
if '__doc__' not in remove:
__doc__ = cls.__doc__
remove = set(remove)
remove.update(NOT_PROXY_WRAPPED)
for attrName, attrValue in inspect.getmembers(cls):
if attrName not in remove:
# We wrap methods using _methodWrapper, because if someone does
# unboundMethod = MyProxyClass.method
# ...they should be able to call unboundMethod with an instance
# of MyProxyClass as they expect (as opposed to an instance of
# the wrapped class, which is what you would need to do if
# we used ProxyAttribute)
# ...the stuff with the cls.__dict__ is just to check
# we don't have a classmethod - since it's a data descriptor,
# we have to go through the class dict...
# PY2: once we completely transition to PY3 only, can probably
# remove the ismethod check
if ((inspect.ismethoddescriptor(attrValue) or
inspect.isfunction(attrValue) or
inspect.ismethod(attrValue)) and
not isinstance(cls.__dict__.get(attrName, None),
(classmethod, staticmethod))):
try:
setattr(Proxy, attrName, _methodWrapper(attrValue))
except AttributeError:
print("proxyClass: error adding proxy method %s.%s" % (classname, attrName))
else:
try:
setattr(Proxy, attrName, ProxyAttribute(attrName))
except AttributeError:
print("proxyClass: error adding proxy attribute %s.%s" % (classname, attrName))
Proxy.__name__ = classname
if module is not None:
Proxy.__module__ = module
return Proxy
# Note - for backwards compatibility reasons, PyNodes still inherit from
# ProxyUnicode, even though we are now discouraging their use 'like strings',
# and ProxyUnicode itself has now had so many methods removed from it that
# it's no longer really a good proxy for unicode.
# NOTE: This may move back to core.general, depending on whether the
# __getitem__ bug was fixed in 2009, since we'll have to do a version switch there
# ProxyUnicode = proxyClass( unicode, 'ProxyUnicode', dataFuncName='name',
# remove=['__getitem__', 'translate'])
# 2009 Beta 2.1 has issues with passing classes with __getitem__
if TYPE_CHECKING:
class ProxyUnicode:
def center(self, width, fillchar=None):
# type: (int, str) -> str
pass
def endswith(self, suffix, start=None, end=None):
# type: (Union[str, Tuple[str, ...]], int, int) -> bool
pass
def find(self, sub, start=None, end=None):
# type: (str, int, int) -> int
pass
def format(self, *args, **kwargs):
# type: (object, object) -> str
pass
def isdecimal(self):
# type: () -> bool
pass
def isidentifier(self):
# type: () -> bool
pass
def islower(self):
# type: () -> bool
pass
def isnumeric(self):
# type: () -> bool
pass
def isprintable(self):
# type: () -> bool
pass
def isupper(self):
# type: () -> bool
pass
def join(self, iterable):
# type: (Iterable[str]) -> str
pass
def ljust(self, width, fillchar=None):
# type: (int, str) -> str
pass
def lower(self):
# type: () -> str
pass
def lstrip(self, chars=None):
# type: (str) -> str
pass
def partition(self, sep):
# type: (str) -> Tuple[str, str, str]
pass
def replace(self, old, new, count=None):
# type: (str, str, int) -> str
pass
def rfind(self, sub, start=None, end=None):
# type: (str, int, int) -> int
pass
def rindex(self, sub, start=None, end=None):
# type: (str, int, int) -> int
pass
def rjust(self, width, fillchar=None):
# type: (int, str) -> str
pass
def rpartition(self, sep):
# type: (str) -> Tuple[str, str, str]
pass
def rsplit(self, sep=None, maxsplit=None):
# type: (Optional[str], int) -> List[str]
pass
def rstrip(self, chars=None):
# type: (str) -> str
pass
def split(self, sep=None, maxsplit=None):
# type: (Optional[str], int) -> List[str]
pass
def startswith(self, prefix, start=None, end=None):
# type: (Union[str, Tuple[str, ...]], int, int) -> bool
pass
def strip(self, chars=None):
# type: (str) -> str
pass
def upper(self):
# type: () -> str
pass
def __add__(self, s):
# type: (str) -> str
pass
def __eq__(self, x):
# type: (object) -> bool
pass
def __ne__(self, x):
# type: (object) -> bool
pass
def __lt__(self, x):
# type: (object) -> bool
pass
def __le__(self, x):
# type: (object) -> bool
pass
def __gt__(self, x):
# type: (object) -> bool
pass
def __ge__(self, x):
# type: (object) -> bool
pass
else:
if PY2:
_proxyStrBase = unicode
else:
_proxyStrBase = str
ProxyUnicode = proxyClass(
_proxyStrBase, 'ProxyUnicode',
module=__name__, dataFuncName='name',
remove=['__doc__', '__getslice__', '__contains__', '__len__',
'__mod__', '__rmod__', '__mul__', '__rmod__', '__rmul__', # reserved for higher levels
'expandtabs', 'translate', 'decode', 'encode', 'splitlines',
'capitalize', 'swapcase', 'title',
'isalnum', 'isalpha', 'isdigit', 'isspace', 'istitle',
'zfill'])
class universalmethod(object):
# """
# a decorator which is similar to builtin classmethod, but which leaves the method unmodified when called
# as a normal instance method:
# - when the wrapped method is called as a class method, the first argument will be the class.
# - when the wrapped method is called as an instance method, the first argument will be the instance.
#
# >>> import inspect
# >>> class D(object):
# ... @universalmethod
# ... def f( obj ):
# ... if inspect.isclass(obj):
# ... print "doing something class related"
# ... else:
# ... print "doing something instance related"
# ...
# >>> D.f()
# doing something class related
# >>> d = D()
# >>> d.f()
# doing something instance related
#
# """
def __init__(self, f):
self.f = f
self.__doc__ = f.__doc__
def __get__(self, instance, cls=None):
if cls is None:
cls = type(instance)
if instance is None:
instance = cls
def newfunc(*args, **kwargs):
return self.f(instance, *args, **kwargs)
newfunc.__doc__ = self.__doc__
return newfunc
class LazyLoadModule(types.ModuleType):
"""
:param name: name of the module
:param contents: dictionary of initial module globals
This function returns a special module type with one method `_lazyModule_addAttr`. The signature
of this method is:
_lazyModule_addAttr(name, creator, *creatorArgs, **creatorKwargs)
Attributes added with this method will not be created until the first time that
they are accessed, at which point a callback function will be called to generate
the attribute's value.
:param name: name of the attribute to lazily add
:param creator: a function that create the
Example::
import sys
mod = LazyLoadModule(__name__, globals())
mod._lazyModule_addAttr( 'foo', str, 'bar' )
One caveat of this technique is that if a user imports everything from your
lazy module ( .e.g from module import * ), it will cause all lazy attributes
to be evaluated.
Also, if any module-level expression needs to reference something that only
exists in the LazyLoadModule, it will need to be stuck in after the creation of the
LazyLoadModule. Then, typically, after defining all functions/classes/etc
which rely on the LazyLoadModule attributes, you will wish to update the
LazyLoadModule with the newly-created functions - typically, this is done
with the _updateLazyModule method.
Finally, any functions which reference any LazyLoadModule-only attributes,
whether they are defined after OR before the creation of the LazyLoadModule,
will have to prefix it with a reference to the LazyLoadModule.
Example::
import sys
def myFunc():
# need to preface foo with 'lazyModule',
# even though this function is defined before
# the creation of the lazy module!
print 'foo is:', lazyModule.foo
lazyModule = LazyLoadModule(__name__, globals())
lazyModule._lazyModule_addAttr( 'foo', str, 'bar' )
# define something which relies on something in the lazy module
fooExpanded = lazyModule.foo + '... now with MORE!'
# update the lazyModule with our new additions (ie, fooExpanded)
lazyModule._updateLazyModule(globals())
"""
class LazyLoader(object):
"""
A data descriptor that delays instantiation of an object
until it is first accessed.
"""
def __init__(self, name, creator, *creatorArgs, **creatorKwargs):
self.creator = creator
self.args = creatorArgs
self.kwargs = creatorKwargs
self.name = name
def __get__(self, obj, objtype):
# if obj is None - ie, we're accessing this from the LazyLoadModule
# CLASS itself (or a subclass), and not an instance of it (ie, an
# actual module), we just return ourself - the LazyLoader object
if obj is None:
return self
# In case the LazyLoader happens to get stored on more
# than one object, cache the created object so the exact
# same one will be returned
if not hasattr(self, 'newobj'):
# use the callback to create the object that will replace us
self.newobj = self.creator(*self.args, **self.kwargs)
if isinstance(obj, types.ModuleType) and hasattr(self.newobj, '__module__'):
self.newobj.__module__ = obj.__name__
# print "Lazy-loaded object:", self.name
# delattr( obj.__class__, self.name) # should we overwrite with None?
# overwrite ourselves with the newly created object
setattr(obj, self.name, self.newobj)
return self.newobj
# because the lazy-loaded objects need to be installed on the CLASS, not on
# an instance, we need to ensure that all LazyLoadModules are subclasses, to
# ensure that they aren't polluting each other's namespaces
def __new__(cls, name, contents, autoSubClass=True):
# because the lazy-loaded objects need to be installed on the CLASS,
# not on an instance, we need to ensure that all LazyLoadModules are
# subclasses, to ensure that they aren't polluting each other's
# namespaces. So we automatically generate a new subclass...
if autoSubClass:
subclassName = name.replace('.', '_') + cls.__name__
cls = type(subclassName, (cls,), {})
return super(LazyLoadModule, cls).__new__(cls, name)
def __init__(self, name, contents, autoSubClass=True):
types.ModuleType.__init__(self, name)
self.__dict__.update(contents)
self._lazyGlobals = contents # globals of original module
# add ourselves to sys.modules, overwriting the original module
sys.modules[name] = self
# the above line assigns a None value to all entries in the original globals.
# luckily, we have a copy on this module we can use to restore it.
self._lazyGlobals.update(self.__dict__)
def __dir__(self):
# for modules, dir usually only returns what's in the dict, and does
# not inspect the class (ie, items on ModuleType aren't returned in
# a module's dir, which makes sense). However, we also want to return
# our LazyLoaded objects, to make it appear like they're there like
# a normal object...
keys = set(self.__dict__)
lazyLoaders = inspect.getmembers(
type(self), lambda x: isinstance(x, self.LazyLoader))
keys.update(name for (name, obj) in lazyLoaders)
return sorted(keys)
@property
def __all__(self):
public = [x for x in list(self.__dict__.keys()) + list(self.__class__.__dict__.keys()) if not x.startswith('_')]
return public
@classmethod
def _lazyModule_addAttr(cls, name, creator, *creatorArgs, **creatorKwargs):
"""
Add a lazily loaded attribute to this module
:param name: name of attribute
:param creator: callback function to load the object
:param creatorArgs: args passed to callback
:param creatorKwargs: kwargs passed to callback
:return: LazyLoader
"""
lazyObj = cls.LazyLoader(name, creator, *creatorArgs, **creatorKwargs)
setattr(cls, name, lazyObj)
return lazyObj
def __setitem__(self, attr, args):
"""
dynModule['attrName'] = ( callbackFunc, ( 'arg1', ), {} )
"""
# args will either be a single callable, or will be a tuple of
# ( callable, (args,), {kwargs} )
if hasattr(args, '__call__'):
callback = args
elif isinstance(args, (tuple, list)):
if len(args) >= 1:
assert hasattr(args[0], '__call__'), 'first argument must be callable'
callback = args[0]
else:
raise ValueError("must supply at least one argument")
if len(args) >= 2:
assert hasattr(args[1], '__iter__'), 'second argument must be iterable'
cb_args = args[1]
else:
cb_args = ()
cb_kwargs = {}
if len(args) == 3:
assert isinstance(args[2], Mapping), 'third argument must be a mapping type'
cb_kwargs = args[2]
else:
cb_kwargs = {}
if len(args) > 3:
raise ValueError(
"if args and kwargs are desired, they should be passed as "
"a tuple and dictionary, respectively")
else:
raise ValueError("the item must be set to a callable, or to a "
"3-tuple of (callable, (args,), {kwargs})")
self._lazyModule_addAttr(attr, callback, *cb_args, **cb_kwargs)
def __getitem__(self, attr):
"""
return a LazyLoader without initializing it, or, if a LazyLoader does not exist with this name,
a real object
"""
try:
return self.__class__.__dict__[attr]
except KeyError:
return self.__dict__[attr]
# Sort of a cumbersome name, but we want to make sure it doesn't conflict with any
# 'real' entries in the module
def _lazyModule_update(self):
"""
Used to update the contents of the LazyLoadModule with the contents of another dict.
"""
self.__dict__.update(self._lazyGlobals)
# Note - since anything referencing attributes that only exist on the lazy module
# must be prefaced with a ref to the lazy module, if we are converting a pre-existing
# module to include LazyLoaded objects, we must manually go through and edit
# any references to those objects to have a 'lazyModule' prefix (or similar).
# To aid in this process, I recommend:
# 1. Uncommenting out the final print statement in _updateLazyModule
# 2. Grabbing the output of the print statement, throw it into a text editor with
# regexp find/replace capabilities
# 3. You should have a python list of names.
# Replace the initial and final bracket and quote - [' and '] - with opening
# and closing parentheses - ( and )
# Then find / replace all occurrences of:
# ', '
# with:
# |
# ...and you should be left with a regular expression you can use to find and replace
# in your original code...
# (you may also want to put (?<=\W) / (?=\W) in front / behind the regexp...)
# Don't do the regexp find / replace on the source code blindly, though!
# ...also, when you make the call to _updateLazyModule that prints out the list of
# dynamic-module-only attributes, you should do it from a GUI maya - there are some objects
# that only exist in GUI-mode...
class LazyDocStringError(Exception):
pass
class LazyDocString(object):
"""
Set the __doc__ of an object to an instance of this class in order to have
a docstring that is dynamically generated when used.
Due to restrictions of inheriting from StringType (which is necessary,
as the 'help' function does a check to see if __doc__ is a string),
the creator can only take a single object.
Since the object initialization requires multiple parameters, the
LazyDocString should be given a sliceable-iterable on creation,
of the following form:
LazyDocString( [documentedObj, docGetter, arg1, arg2, ...] )
documentedObj should be the object on which we are placing the docstring
docGetter should be a function which is used to retrieve the 'real'
docstring - it's args will be documentedObj and any extra args
passed to the object on creation.
Example Usage:
>>> def getDocStringFromDict(obj):
... returnVal = docStringDict[obj.__name__]
... return returnVal
>>>
>>> # In order to alter the doc of a class, we need to use a metaclass
>>> class TestMetaClass(type): pass
>>>
>>> class TestClass(with_metaclass(TestMetaClass, object)):
... def aMethod(self):
... pass
...
... aMethod.__doc__ = LazyDocString( (aMethod, getDocStringFromDict, (aMethod,)) )
>>>
>>> TestClass.__doc__ = LazyDocString( (TestClass, getDocStringFromDict, (TestClass,)) )
>>>
>>>
>>> docStringDict = {'TestClass':'New Docs for PynodeClass!',
... 'aMethod':'Method docs!'}
>>>
>>> TestClass.__doc__
'New Docs for PynodeClass!'
>>> TestClass.aMethod.__doc__
'Method docs!'
Note that new-style classes (ie, instances of 'type') and instancemethods
can't have their __doc__ altered.
In the case of classes, you can get around this by using a metaclass for
the class whose docstring you wish to alter.
In the case of instancemethods, just set the __doc__ on the function
underlying the method (ie, myMethod.im_func). Note that if the __doc__
for the method is set within the class definition itself, you will
already automatically be modifying the underlying function.
"""
def __init__(self, argList):
if len(argList) < 2:
raise LazyDocStringError(
'LazyDocString must be initialized with an iterable of the '
'form: LazyDocString( [documentedObj, '
'docGetter, arg1, arg2, ...] )')
documentedObj = argList[0]
docGetter = argList[1]
if len(argList) > 2:
args = argList[2]
if len(argList) == 4:
kwargs = argList[3]
else:
kwargs = {}
else:
args = ()
kwargs = {}
try:
# put in a placeholder docstring, and check to make
# sure we can change the __doc__ of this object!
documentedObj.__doc__ = 'LazyDocString placeholder'
except AttributeError:
raise LazyDocStringError(
'cannot modify the docstring of %r objects' %
documentedObj.__class__.__name__)
self.documentedObj = documentedObj
self.docGetter = docGetter
self.args = args
self.kwargs = kwargs
def __str__(self):
# print "creating docstrings", self.docGetter, self.args, self.kwargs
self.documentedObj.__doc__ = self.docGetter(*self.args, **self.kwargs)
return self.documentedObj.__doc__
def __repr__(self):