diff --git a/example/use_plugin.py b/example/use_plugin.py new file mode 100644 index 0000000..1250da4 --- /dev/null +++ b/example/use_plugin.py @@ -0,0 +1,3 @@ +from usepy.plugin import useLogger + +useLogger(packages=["scrapy", "django", "usepy"]) diff --git a/pyproject.toml b/pyproject.toml index 8e65b8c..a444051 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "usepy" -version = "0.2.5" +version = "0.2.6" description = "usepy" homepage = "https://usepy.code05.com/" authors = ["miclon "] @@ -17,7 +17,6 @@ typing-extensions = [ ] - [tool.poetry.group.test.dependencies] pytest = [ { version = "^7.0.0", python = ">=3.6,<3.7" }, diff --git a/src/usepy/plugin.py b/src/usepy/plugin.py new file mode 100644 index 0000000..9b5d88a --- /dev/null +++ b/src/usepy/plugin.py @@ -0,0 +1,30 @@ +import functools +import importlib +import os +import sys +from glob import glob + + +def find_plugin_modules(): + plugins = [] + for module in sys.path: + path = os.path.join(os.path.dirname(__file__), module) + for file in glob(os.path.join(path, 'usepy_plugin_*')): + path_name = os.path.basename(file) + try: + plugins.append(importlib.import_module(path_name)) + except ModuleNotFoundError: + pass + return plugins + + +plugin_modules = find_plugin_modules() + + +def __getattr__(name): + for pm in plugin_modules: + if not hasattr(pm, name): + continue + return functools.reduce(getattr, [name], pm) + else: + raise ModuleNotFoundError(f"module `{__name__}` has no function `{name}`")