-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
80 lines (57 loc) · 1.97 KB
/
config.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
"""
Reads ``config.json`` into a ``config`` dictionary on import.
"""
from functools import wraps
import re
import inspect
import os
import json
from paya.util.functions import undefined
import maya.cmds as m
path = os.path.join(os.path.dirname(__file__), 'config.json')
with open(path, 'r') as f:
data = f.read()
config = json.loads(data)
# If useOffsetParentMatrix is undefined in config, set it to True only if
# Maya >= 2022, to avoid bugs with earlier implementations
mayaIntVersion = int(re.findall(r"[0-9]{4}", m.about(version=True))[0])
config.setdefault('useOffsetParentMatrix', mayaIntVersion >= 2022)
class Config:
"""
Context manager that takes overrides to ``config`` as keyword arguments.
"""
def __init__(self, **overrides):
self._overrides = overrides
def __enter__(self):
self._prev_config = config.copy()
config.update(self._overrides)
def __exit__(self, exc_type, exc_val, exc_tb):
config.clear()
config.update(self._prev_config)
def takeUndefinedFromConfig(f):
"""
Function decorator. Intercepts any keyword arguments that have been set
to, or left at a default of,
:class:`undefined <paya.util.functions.Undefined>` and swaps them out with
values from ``config``.
:param f: the function to wrap
:return: The wrapped function.
"""
params = inspect.signature(f).parameters
def paramQualifies(x):
kind = x.kind
return kind == x.KEYWORD_ONLY \
or (kind == x.POSITIONAL_OR_KEYWORD \
and x.default is not x.empty)
kwnames = [param.name for param \
in params.values() if paramQualifies(param)]
@wraps(f)
def wrapped(*args, **kwargs):
_kwargs = {}
for kwname in kwnames:
val = kwargs.get(kwname, params[kwname].default)
if val is undefined:
val = config[kwname]
_kwargs[kwname] = val
return f(*args, **_kwargs)
return wrapped