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

6
.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
/.idea/
/tests/
poetry.lock
*/__pycache__/

1
README.md Normal file
View File

@@ -0,0 +1 @@
[nekomata is now in early-dev state](https://nekomata.kotikot.com/)

14
neko_injector/__init__.py Normal file
View File

@@ -0,0 +1,14 @@
from .neko.interfaces import (
inject_module,
inject_modules,
inject_object,
get_injected_object
)
__all__ = [
'inject_module',
'inject_modules',
'inject_object',
'get_injected_object'
]

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)

View File

View File

@@ -0,0 +1,7 @@
from neko_configparser import ConfigParserInterface
neko_config = ConfigParserInterface.parse_config()
__all__ = ['ConfigParserInterface', 'neko_config']

View File

@@ -0,0 +1,14 @@
from ..modules.variants import (
inject_module,
inject_modules,
inject_object,
get_injected_object
)
__all__ = [
'inject_module',
'inject_modules',
'inject_object',
'get_injected_object'
]

16
pyproject.toml Normal file
View File

@@ -0,0 +1,16 @@
[tool.poetry]
name = "neko-injector"
version = "0.1.0"
description = ""
authors = ["hhh"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
neko-configparser = {git="https://github.com/nekomatad/neko-configparser"}
#neko-configparser = {path = "../neko-configparser", develop = true}
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"