summaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
author苏向夜 <fu050409@163.com>2024-01-27 16:25:17 +0800
committer苏向夜 <fu050409@163.com>2024-01-27 16:25:17 +0800
commitaa39ef02e0bf7d4bf47349cd59ddf4d92de03df8 (patch)
tree7af5cc91f4654a42f203bb5cb9879139b9591fe8 /src
parentf3b426b6911c6925c6d0b165ed998e818b9f021b (diff)
downloadinfini-aa39ef02e0bf7d4bf47349cd59ddf4d92de03df8.tar.gz
infini-aa39ef02e0bf7d4bf47349cd59ddf4d92de03df8.zip
:sparkles: feat(internal): return a Register class instead of ModuleType in require function
Diffstat (limited to 'src')
-rw-r--r--src/infini/const.py3
-rw-r--r--src/infini/internal.py28
2 files changed, 17 insertions, 14 deletions
diff --git a/src/infini/const.py b/src/infini/const.py
deleted file mode 100644
index 2ec42488..00000000
--- a/src/infini/const.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from pathlib import Path
-
-SRC_HOME = Path.home() / ".ipm" / "src" \ No newline at end of file
diff --git a/src/infini/internal.py b/src/infini/internal.py
index 4f800297..460407a8 100644
--- a/src/infini/internal.py
+++ b/src/infini/internal.py
@@ -1,18 +1,24 @@
-from infini.typing import List, ModuleType
-from infini.const import SRC_HOME
+from infini.loader import Loader
+from infini.register import Register
+from infini.typing import List
+from pathlib import Path
-import importlib
import sys
+import inspect
-def require(name: str, paths: List | None = None) -> ModuleType:
+def require(name: str, paths: List | None = None) -> Register:
+ caller_frame = inspect.stack()[1]
+ caller_file = caller_frame[0].f_globals["__file__"]
+
+ default_paths = [Path(caller_file).resolve().parent]
paths = [
str(path)
- for path in (
- (list(paths) + [str(SRC_HOME / name)]) if paths else [str(SRC_HOME / name)]
- )
+ for path in ((list(paths) + default_paths) if paths else default_paths)
]
- sys.path.extend(paths)
- module = importlib.import_module(name)
- (sys.path.remove(path) for path in paths)
- return module
+ (sys.path.insert(0, path) for path in paths)
+
+ with Loader() as loader:
+ loader.load(name)
+ sys.path = sys.path[len(paths) - 1 :]
+ return loader.into_register()