mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-08 06:46:02 +00:00
style: introduce ruff as linter and formatter (#1356)
* style: remove necessary imports * style: fix F841 * style: fix F401 * style: fix F811 * style: fix E402 * style: fix E721 * style: fix E722 * style: fix E722 * style: fix F541 * style: ruff format * style: all passed * style: add ruff in deps * style: more ignores in ruff.toml * style: add pre-commit
This commit is contained in:
committed by
GitHub
parent
09e70d70e9
commit
209f16af76
43
pkg/utils/importutil.py
Normal file
43
pkg/utils/importutil.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import importlib
|
||||
import importlib.util
|
||||
import os
|
||||
import typing
|
||||
|
||||
|
||||
def import_modules_in_pkg(pkg: typing.Any) -> None:
|
||||
"""
|
||||
导入一个包内的所有模块
|
||||
Args:
|
||||
pkg: 要导入的包对象
|
||||
"""
|
||||
pkg_path = os.path.dirname(pkg.__file__)
|
||||
import_dir(pkg_path)
|
||||
|
||||
|
||||
def import_modules_in_pkgs(pkgs: typing.List) -> None:
|
||||
for pkg in pkgs:
|
||||
import_modules_in_pkg(pkg)
|
||||
|
||||
|
||||
def import_dot_style_dir(dot_sep_path: str):
|
||||
sec = dot_sep_path.split('.')
|
||||
|
||||
return import_dir(os.path.join(*sec))
|
||||
|
||||
|
||||
def import_dir(path: str):
|
||||
for file in os.listdir(path):
|
||||
if file.endswith('.py') and file != '__init__.py':
|
||||
full_path = os.path.join(path, file)
|
||||
rel_path = full_path.replace(
|
||||
os.path.dirname(os.path.dirname(os.path.dirname(__file__))), ''
|
||||
)
|
||||
rel_path = rel_path[1:]
|
||||
rel_path = rel_path.replace('/', '.')[:-3]
|
||||
importlib.import_module(rel_path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from pkg.platform import types
|
||||
|
||||
import_modules_in_pkg(types)
|
||||
Reference in New Issue
Block a user