Skip to content

Commit

Permalink
✨ feat: plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mic1on committed Jul 28, 2023
1 parent eb1d9a0 commit 2a125aa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions example/use_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from usepy.plugin import useLogger

useLogger(packages=["scrapy", "django", "usepy"])
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <jcnd@163.com>"]
Expand All @@ -17,7 +17,6 @@ typing-extensions = [
]



[tool.poetry.group.test.dependencies]
pytest = [
{ version = "^7.0.0", python = ">=3.6,<3.7" },
Expand Down
30 changes: 30 additions & 0 deletions src/usepy/plugin.py
Original file line number Diff line number Diff line change
@@ -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}`")

0 comments on commit 2a125aa

Please sign in to comment.