-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathutil.py
More file actions
67 lines (56 loc) · 2.19 KB
/
util.py
File metadata and controls
67 lines (56 loc) · 2.19 KB
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
#!/usr/bin/env python3
"""
Assorted utility functions.
"""
from functools import update_wrapper
from logging import warning
from traceback import extract_stack
def remapparams(**remap):
"""
Remap the specified named parameters.
Example to support an obsolete `parseAll` parameter:
@remapparams(parseAll='parse_all')
def parse(s, parse_all=True):
"""
if not remap:
raise ValueError("no parameters specified for remapping")
for old, new in remap.items():
if new in remap:
raise ValueError(f"{old}={new!r}: {new!r} is also remapped")
def remapparams_decorator(func):
"""The decorator to apply the remappings."""
# a record of callers whose parameters were remapped
remapped_callers = set()
def remapparams_wrapper(*a, **kw):
remappings = {}
for param, value in list(kw.items()):
try:
remapped = remap[param]
except KeyError:
continue
if remapped in kw:
raise ValueError(
f"remap {param}= to {remapped}=: this is already present in the keyword arguments"
)
del kw[param]
kw[remapped] = value
remappings[param] = remapped
if remappings:
caller_frame = extract_stack(limit=2)[-2]
caller_key = caller_frame.filename, caller_frame.lineno
if caller_key not in remapped_callers:
warning(
"call of %s.%s() from %s:%d: remapped the following obsolete parameters: %s",
func.__module__,
func.__name__,
caller_frame.filename,
caller_frame.lineno,
", ".join(
sorted(f"{old}->{new}" for old, new in remappings.items())
),
)
remapped_callers.add(caller_key)
return func(*a, **kw)
update_wrapper(remapparams_wrapper, func)
return remapparams_wrapper
return remapparams_decorator