Skip to content

Commit

Permalink
实现了 import_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
carefree0910 committed Dec 25, 2023
1 parent 2c1dbe9 commit 6d48fc2
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cftool/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import hashlib
import zipfile
import operator
import importlib
import threading
import unicodedata

Expand All @@ -37,6 +38,7 @@
from typing import Protocol
from typing import Coroutine
from typing import NamedTuple
from pathlib import Path
from argparse import Namespace
from datetime import datetime
from datetime import timedelta
Expand Down Expand Up @@ -495,6 +497,21 @@ def get_err_msg(err: Exception) -> str:
return " | ".join(map(repr, sys.exc_info()[:2] + (str(err),)))


def import_modules(path: Union[str, Path], prefix: str) -> None:
if isinstance(path, str):
path = Path(path)
_globals = inspect.stack()[1][0].f_globals
for path in path.parent.glob("*.py"):
if path.stem == "__init__":
continue
module = importlib.import_module(f"{prefix}.{path.stem}")
if "__all__" in module.__dict__:
names = module.__dict__["__all__"]
else:
names = [x for x in module.__dict__ if not x.startswith("_")]
_globals.update({k: getattr(module, k) for k in names})


async def retry(
fn: Callable[[], Coroutine[None, None, TRetryResponse]],
num_retry: Optional[int] = None,
Expand Down

0 comments on commit 6d48fc2

Please sign in to comment.