| 1 |
""" |
| 2 |
Resolve a name to an object. |
| 3 |
|
| 4 |
It is expected that `name` will be a string in one of the following |
| 5 |
formats, where W is shorthand for a valid Python identifier and dot stands |
| 6 |
for a literal period in these pseudo-regexes: |
| 7 |
|
| 8 |
W(.W)* |
| 9 |
W(.W)*:(W(.W)*)? |
| 10 |
|
| 11 |
The first form is intended for backward compatibility only. It assumes that |
| 12 |
some part of the dotted name is a package, and the rest is an object |
| 13 |
somewhere within that package, possibly nested inside other objects. |
| 14 |
Because the place where the package stops and the object hierarchy starts |
| 15 |
can't be inferred by inspection, repeated attempts to import must be done |
| 16 |
with this form. |
| 17 |
|
| 18 |
In the second form, the caller makes the division point clear through the |
| 19 |
provision of a single colon: the dotted name to the left of the colon is a |
| 20 |
package to be imported, and the dotted name to the right is the object |
| 21 |
hierarchy within that package. Only one import is needed in this form. If |
| 22 |
it ends with the colon, then a module object is returned. |
| 23 |
|
| 24 |
The function will return an object (which might be a module), or raise one |
| 25 |
of the following exceptions: |
| 26 |
|
| 27 |
ValueError - if `name` isn't in a recognised format |
| 28 |
ImportError - if an import failed when it shouldn't have |
| 29 |
AttributeError - if a failure occurred when traversing the object hierarchy |
| 30 |
within the imported package to get to the desired object) |
| 31 |
""" |
| 32 |
|
| 33 |
import importlib |
| 34 |
import re |
| 35 |
|
| 36 |
__version__ = "1.3.10" |
| 37 |
|
| 38 |
|
| 39 |
_NAME_PATTERN = None |
| 40 |
|
| 41 |
def resolve_name(name): |
| 42 |
""" |
| 43 |
Resolve a name to an object. |
| 44 |
|
| 45 |
It is expected that `name` will be a string in one of the following |
| 46 |
formats, where W is shorthand for a valid Python identifier and dot stands |
| 47 |
for a literal period in these pseudo-regexes: |
| 48 |
|
| 49 |
W(.W)* |
| 50 |
W(.W)*:(W(.W)*)? |
| 51 |
|
| 52 |
The first form is intended for backward compatibility only. It assumes that |
| 53 |
some part of the dotted name is a package, and the rest is an object |
| 54 |
somewhere within that package, possibly nested inside other objects. |
| 55 |
Because the place where the package stops and the object hierarchy starts |
| 56 |
can't be inferred by inspection, repeated attempts to import must be done |
| 57 |
with this form. |
| 58 |
|
| 59 |
In the second form, the caller makes the division point clear through the |
| 60 |
provision of a single colon: the dotted name to the left of the colon is a |
| 61 |
package to be imported, and the dotted name to the right is the object |
| 62 |
hierarchy within that package. Only one import is needed in this form. If |
| 63 |
it ends with the colon, then a module object is returned. |
| 64 |
|
| 65 |
The function will return an object (which might be a module), or raise one |
| 66 |
of the following exceptions: |
| 67 |
|
| 68 |
ValueError - if `name` isn't in a recognised format |
| 69 |
ImportError - if an import failed when it shouldn't have |
| 70 |
AttributeError - if a failure occurred when traversing the object hierarchy |
| 71 |
within the imported package to get to the desired object) |
| 72 |
""" |
| 73 |
global _NAME_PATTERN |
| 74 |
if _NAME_PATTERN is None: |
| 75 |
# Lazy import to speedup Python startup time |
| 76 |
import re |
| 77 |
dotted_words = r'(?!\d)(\w+)(\.(?!\d)(\w+))*' |
| 78 |
_NAME_PATTERN = re.compile(f'^(?P<pkg>{dotted_words})' |
| 79 |
f'(?P<cln>:(?P<obj>{dotted_words})?)?$', |
| 80 |
re.UNICODE) |
| 81 |
|
| 82 |
m = _NAME_PATTERN.match(name) |
| 83 |
if not m: |
| 84 |
raise ValueError(f'invalid format: {name!r}') |
| 85 |
gd = m.groupdict() |
| 86 |
if gd.get('cln'): |
| 87 |
# there is a colon - a one-step import is all that's needed |
| 88 |
mod = importlib.import_module(gd['pkg']) |
| 89 |
parts = gd.get('obj') |
| 90 |
parts = parts.split('.') if parts else [] |
| 91 |
else: |
| 92 |
# no colon - have to iterate to find the package boundary |
| 93 |
parts = name.split('.') |
| 94 |
modname = parts.pop(0) |
| 95 |
# first part *must* be a module/package. |
| 96 |
mod = importlib.import_module(modname) |
| 97 |
while parts: |
| 98 |
p = parts[0] |
| 99 |
s = f'{modname}.{p}' |
| 100 |
try: |
| 101 |
mod = importlib.import_module(s) |
| 102 |
parts.pop(0) |
| 103 |
modname = s |
| 104 |
except ImportError: |
| 105 |
break |
| 106 |
# if we reach this point, mod is the module, already imported, and |
| 107 |
# parts is the list of parts in the object hierarchy to be traversed, or |
| 108 |
# an empty list if just the module is wanted. |
| 109 |
result = mod |
| 110 |
for p in parts: |
| 111 |
result = getattr(result, p) |
| 112 |
return result |