Skip to content

Commit 2a125aa

Browse files
committed
✨ feat: plugin
1 parent eb1d9a0 commit 2a125aa

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

example/use_plugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from usepy.plugin import useLogger
2+
3+
useLogger(packages=["scrapy", "django", "usepy"])

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "usepy"
3-
version = "0.2.5"
3+
version = "0.2.6"
44
description = "usepy"
55
homepage = "https://usepy.code05.com/"
66
authors = ["miclon <jcnd@163.com>"]
@@ -17,7 +17,6 @@ typing-extensions = [
1717
]
1818

1919

20-
2120
[tool.poetry.group.test.dependencies]
2221
pytest = [
2322
{ version = "^7.0.0", python = ">=3.6,<3.7" },

src/usepy/plugin.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import functools
2+
import importlib
3+
import os
4+
import sys
5+
from glob import glob
6+
7+
8+
def find_plugin_modules():
9+
plugins = []
10+
for module in sys.path:
11+
path = os.path.join(os.path.dirname(__file__), module)
12+
for file in glob(os.path.join(path, 'usepy_plugin_*')):
13+
path_name = os.path.basename(file)
14+
try:
15+
plugins.append(importlib.import_module(path_name))
16+
except ModuleNotFoundError:
17+
pass
18+
return plugins
19+
20+
21+
plugin_modules = find_plugin_modules()
22+
23+
24+
def __getattr__(name):
25+
for pm in plugin_modules:
26+
if not hasattr(pm, name):
27+
continue
28+
return functools.reduce(getattr, [name], pm)
29+
else:
30+
raise ModuleNotFoundError(f"module `{__name__}` has no function `{name}`")

0 commit comments

Comments
 (0)