|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
This module is written for configurable workflow, not currently in use. |
|
|
""" |
|
|
|
|
|
from __future__ import annotations |
|
|
|
|
|
import importlib |
|
|
import inspect |
|
|
import sys |
|
|
import threading |
|
|
|
|
|
alias_lock = threading.RLock() |
|
|
GlobalAliases = {} |
|
|
|
|
|
__all__ = ["alias", "resolve_name"] |
|
|
|
|
|
|
|
|
def alias(*names): |
|
|
""" |
|
|
Stores the decorated function or class in the global aliases table under the given names and as the `__aliases__` |
|
|
member of the decorated object. This new member will contain all alias names declared for that object. |
|
|
""" |
|
|
|
|
|
def _outer(obj): |
|
|
for n in names: |
|
|
with alias_lock: |
|
|
GlobalAliases[n] = obj |
|
|
|
|
|
|
|
|
obj.__aliases__ = getattr(obj, "__aliases__", ()) + tuple(names) |
|
|
|
|
|
return obj |
|
|
|
|
|
return _outer |
|
|
|
|
|
|
|
|
def resolve_name(name): |
|
|
""" |
|
|
Search for the declaration (function or class) with the given name. This will first search the list of aliases to |
|
|
see if it was declared with this aliased name, then search treating `name` as a fully qualified name, then search |
|
|
the loaded modules for one having a declaration with the given name. If no declaration is found, raise ValueError. |
|
|
|
|
|
Raises: |
|
|
ValueError: When the module is not found. |
|
|
ValueError: When the module does not have the specified member. |
|
|
ValueError: When multiple modules with the declaration name are found. |
|
|
ValueError: When no module with the specified member is found. |
|
|
|
|
|
""" |
|
|
|
|
|
with alias_lock: |
|
|
obj = GlobalAliases.get(name) |
|
|
|
|
|
if name in GlobalAliases and obj is None: |
|
|
raise AssertionError |
|
|
|
|
|
|
|
|
if obj is None and "." in name: |
|
|
modname, declname = name.rsplit(".", 1) |
|
|
|
|
|
try: |
|
|
mod = importlib.import_module(modname) |
|
|
obj = getattr(mod, declname, None) |
|
|
except ModuleNotFoundError as not_found_err: |
|
|
raise ValueError(f"Module {modname!r} not found.") from not_found_err |
|
|
|
|
|
if obj is None: |
|
|
raise ValueError(f"Module {modname!r} does not have member {declname!r}.") |
|
|
|
|
|
|
|
|
if obj is None: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mods = [m for m in list(sys.modules.values()) if getattr(m, name, None)] |
|
|
|
|
|
if len(mods) > 0: |
|
|
if len(mods) > 1: |
|
|
foundmods = set(filter(None, {inspect.getmodule(getattr(m, name)) for m in mods})) |
|
|
|
|
|
if len(foundmods) > 1: |
|
|
modnames = [m.__name__ for m in foundmods] |
|
|
msg = f"Multiple modules ({modnames!r}) with declaration name {name!r} found, resolution is ambiguous." |
|
|
raise ValueError(msg) |
|
|
mods = list(foundmods) |
|
|
|
|
|
obj = getattr(mods[0], name) |
|
|
|
|
|
if obj is None: |
|
|
raise ValueError(f"No module with member {name!r} found.") |
|
|
|
|
|
return obj |
|
|
|