|
20 | 20 |
|
21 | 21 | import collections
|
22 | 22 |
|
| 23 | +from types import FunctionType |
| 24 | + |
| 25 | + |
| 26 | +#============================================= |
| 27 | +# Class With Stages |
| 28 | +#============================================= |
| 29 | + |
| 30 | + |
| 31 | +def rigify_stage(stage): |
| 32 | + """Decorates the method with the specified stage.""" |
| 33 | + def process(method): |
| 34 | + if not isinstance(method, FunctionType): |
| 35 | + raise ValueError("Stage decorator must be applied to a method definition") |
| 36 | + method._rigify_stage = stage |
| 37 | + return method |
| 38 | + return process |
| 39 | + |
| 40 | + |
| 41 | +class StagedMetaclass(type): |
| 42 | + """ |
| 43 | + Metaclass for rigs that manages assignment of methods to stages via @stage_* decorators. |
| 44 | +
|
| 45 | + Using 'DEFINE_STAGES = True' inside the class definition will register all non-system |
| 46 | + method names from that definition as valid stages. After that, subclasses can |
| 47 | + register methods to those stages, to be called via rigify_invoke_stage. |
| 48 | + """ |
| 49 | + def __new__(metacls, class_name, bases, namespace, **kwds): |
| 50 | + staged_bases = [base for base in bases if isinstance(base, StagedMetaclass)] |
| 51 | + |
| 52 | + # Compute the set of inherited stages |
| 53 | + stages = set().union(*[base._rigify_stages for base in staged_bases]) |
| 54 | + |
| 55 | + # Add methods from current class if requested |
| 56 | + if 'DEFINE_STAGES' in namespace: |
| 57 | + del namespace['DEFINE_STAGES'] |
| 58 | + |
| 59 | + for name, item in namespace.items(): |
| 60 | + if name[0] != '_' and isinstance(item, FunctionType): |
| 61 | + stages.add(name) |
| 62 | + |
| 63 | + # Create the class |
| 64 | + result = type.__new__(metacls, class_name, bases, dict(namespace)) |
| 65 | + |
| 66 | + # Compute the inherited stage to method mapping |
| 67 | + stage_map = collections.defaultdict(collections.OrderedDict) |
| 68 | + method_map = {} |
| 69 | + |
| 70 | + for base in staged_bases: |
| 71 | + for stage_name, methods in base._rigify_stage_map.items(): |
| 72 | + for method_name, method_class in methods.items(): |
| 73 | + if method_name in stages: |
| 74 | + raise ValueError("Stage method '%s' inherited @stage_%s in class %s (%s)" % |
| 75 | + (method_name, stage_name, class_name, result.__module__)) |
| 76 | + |
| 77 | + # Check consistency of inherited stage assignment to methods |
| 78 | + if method_name in method_map: |
| 79 | + if method_map[method_name] != stage_name: |
| 80 | + print("RIGIFY CLASS %s (%s): method '%s' has inherited both @stage_%s and @stage_%s\n" % |
| 81 | + (class_name, result.__module__, method_name, method_map[method_name], stage_name)) |
| 82 | + else: |
| 83 | + method_map[method_name] = stage_name |
| 84 | + |
| 85 | + stage_map[stage_name][method_name] = method_class |
| 86 | + |
| 87 | + # Scan newly defined methods for stage decorations |
| 88 | + for method_name, item in namespace.items(): |
| 89 | + if isinstance(item, FunctionType): |
| 90 | + stage = getattr(item, '_rigify_stage', None) |
| 91 | + |
| 92 | + if stage and method_name in stages: |
| 93 | + print("RIGIFY CLASS %s (%s): cannot use stage decorator on the stage method '%s' (@stage_%s ignored)" % |
| 94 | + (class_name, result.__module__, method_name, stage)) |
| 95 | + continue |
| 96 | + |
| 97 | + # Ensure that decorators aren't lost when redefining methods |
| 98 | + if method_name in method_map: |
| 99 | + if not stage: |
| 100 | + stage = method_map[method_name] |
| 101 | + print("RIGIFY CLASS %s (%s): missing stage decorator on method '%s' (should be @stage_%s)" % |
| 102 | + (class_name, result.__module__, method_name, stage)) |
| 103 | + # Check that the method is assigned to only one stage |
| 104 | + elif stage != method_map[method_name]: |
| 105 | + print("RIGIFY CLASS %s (%s): method '%s' has decorator @stage_%s, but inherited base has @stage_%s" % |
| 106 | + (class_name, result.__module__, method_name, stage, method_map[method_name])) |
| 107 | + |
| 108 | + # Assign the method to the stage, verifying that it's valid |
| 109 | + if stage: |
| 110 | + if stage not in stages: |
| 111 | + raise ValueError("Invalid stage name '%s' for method '%s' in class %s (%s)" % |
| 112 | + (stage, method_name, class_name, result.__module__)) |
| 113 | + else: |
| 114 | + stage_map[stage][method_name] = result |
| 115 | + |
| 116 | + result._rigify_stages = frozenset(stages) |
| 117 | + result._rigify_stage_map = stage_map |
| 118 | + |
| 119 | + return result |
| 120 | + |
| 121 | + def make_stage_decorators(self): |
| 122 | + return [('stage_'+name, rigify_stage(name)) for name in self._rigify_stages] |
| 123 | + |
| 124 | + |
| 125 | +class BaseStagedClass(object, metaclass=StagedMetaclass): |
| 126 | + def rigify_invoke_stage(self, stage): |
| 127 | + """Call all methods decorated with the given stage, followed by the callback.""" |
| 128 | + cls = self.__class__ |
| 129 | + assert(isinstance(cls, StagedMetaclass)) |
| 130 | + assert(stage in cls._rigify_stages) |
| 131 | + |
| 132 | + for method_name in cls._rigify_stage_map[stage]: |
| 133 | + getattr(self, method_name)() |
| 134 | + |
| 135 | + getattr(self, stage)() |
| 136 | + |
23 | 137 |
|
24 | 138 | #=============================================
|
25 | 139 | # Per-owner singleton class
|
26 | 140 | #=============================================
|
27 | 141 |
|
28 | 142 |
|
29 |
| -class SingletonPluginMetaclass(type): |
| 143 | +class SingletonPluginMetaclass(StagedMetaclass): |
30 | 144 | """Metaclass for maintaining one instance per owner object per constructor arg set."""
|
31 | 145 | def __call__(cls, owner, *constructor_args):
|
32 | 146 | key = (cls, *constructor_args)
|
|
0 commit comments