chore: fix imports

This commit is contained in:
Junyan Qin
2025-11-16 17:44:18 +08:00
parent 75edeb7a01
commit 418cddd657
38 changed files with 110 additions and 248 deletions

View File

@@ -1,5 +1,5 @@
import importlib
import importlib.util
import importlib.resources
import os
import typing
@@ -25,7 +25,7 @@ def import_dot_style_dir(dot_sep_path: str):
return import_dir(os.path.join(*sec))
def import_dir(path: str):
def import_dir(path: str, path_prefix: str = 'langbot.'):
for file in os.listdir(path):
if file.endswith('.py') and file != '__init__.py':
full_path = os.path.join(path, file)
@@ -33,10 +33,13 @@ def import_dir(path: str):
rel_path = rel_path[1:]
rel_path = rel_path.replace('/', '.')[:-3]
rel_path = rel_path.replace('\\', '.')
importlib.import_module(rel_path)
importlib.import_module(f'{path_prefix}{rel_path}')
if __name__ == '__main__':
from pkg.platform import types
def read_resource_file(resource_path: str) -> str:
with importlib.resources.files('langbot').joinpath(resource_path).open('r', encoding='utf-8') as f:
return f.read()
import_modules_in_pkg(types)
def list_resource_files(resource_path: str) -> list[str]:
return [f.name for f in importlib.resources.files('langbot').joinpath(resource_path).iterdir()]