This commit is contained in:
hhh
2024-02-02 22:36:53 +02:00
commit 9cea034fc3
10 changed files with 128 additions and 0 deletions

View File

@@ -0,0 +1 @@
pass

View File

@@ -0,0 +1,32 @@
import importlib
from typing import TypeVar
from types import ModuleType
from ..neko.integrations import neko_config
T = TypeVar('T')
def apply_replace(obj: T) -> T:
if isinstance(obj, ModuleType):
module = obj.__package__
if obj.__name__ == obj.__package__:
module = module.split('.')[0]
name = obj.__name__.removeprefix(module).removeprefix('.')
elif issubclass(obj, object):
module = obj.__module__
name = obj.__name__
else:
raise ImportError(f'Cannot apply import patches to type {type(obj)}')
try:
return importlib.import_module(
neko_config.connections[module.split('.')[0]]
+ module.removeprefix(module.split('.')[0])
).__getattribute__(name)
except (AttributeError, KeyError):
raise ImportError(
f'No standard--implementation connection for '
f'{module.split(".")[0]}'
)

View File

@@ -0,0 +1,37 @@
from .applyer import apply_replace
from typing import Iterable, Type, TypeVar
from types import ModuleType
T = TypeVar('T')
def inject_object(*objects) -> None:
for obj in objects:
new = apply_replace(obj)
for attr, value in new.__dict__.items():
try:
setattr(obj, attr, getattr(new, attr))
except AttributeError:
continue
def get_injected_object(*objects: Type[T]) -> Iterable[T]:
return [apply_replace(obj) for obj in objects]
def inject_module(mod: ModuleType) -> None:
try:
inject_object(*getattr(mod, '__replacements__'))
except AttributeError:
raise ImportError(f'Module {mod.__name__} does not define a list of '
f'replacements, it cannot be patched')
except ModuleNotFoundError:
raise ImportError(f'The implementation presented in the config does not comply '
f'with the structure of the standard')
def inject_modules(modules: list[ModuleType]) -> None:
for module in modules:
inject_module(module)