|
9 | 9 |
|
10 | 10 | from __future__ import annotations |
11 | 11 |
|
12 | | -import importlib.util |
| 12 | +import os |
13 | 13 | import sys |
14 | 14 | import threading |
15 | | -import types |
16 | | -from ast import mod |
17 | | -from importlib.abc import Loader, MetaPathFinder |
18 | | -from importlib.machinery import SourceFileLoader |
19 | 15 | from pathlib import Path |
20 | 16 | from typing import TYPE_CHECKING |
21 | 17 |
|
22 | | -import scyjava |
23 | | -from scyjava._stubs import generate_stubs |
24 | | - |
25 | 18 | if TYPE_CHECKING: |
| 19 | + import types |
26 | 20 | from collections.abc import Sequence |
27 | 21 | from importlib.machinery import ModuleSpec |
28 | 22 |
|
29 | 23 |
|
| 24 | +# where generated stubs should land (defaults to this dir: `scyjava.types`) |
| 25 | +STUBS_DIR = os.getenv("SCYJAVA_STUBS_DIR", str(Path(__file__).parent)) |
| 26 | +# namespace under which generated stubs will be placed |
| 27 | +STUBS_NAMESPACE = __name__ |
| 28 | +# module lock to prevent concurrent stub generation |
30 | 29 | _STUBS_LOCK = threading.Lock() |
31 | | -TYPES_DIR = Path(__file__).parent |
32 | 30 |
|
33 | 31 |
|
34 | | -class ScyJavaTypesMetaFinder(MetaPathFinder): |
35 | | - """Meta path finder for scyjava.types that delegates to our loader.""" |
| 32 | +class ScyJavaTypesMetaFinder: |
| 33 | + """Meta path finder for scyjava.types that generates stubs on demand.""" |
36 | 34 |
|
37 | 35 | def find_spec( |
38 | 36 | self, |
39 | 37 | fullname: str, |
40 | 38 | path: Sequence[str] | None, |
41 | 39 | target: types.ModuleType | None = None, |
42 | | - /, |
43 | 40 | ) -> ModuleSpec | None: |
44 | 41 | """Return a spec for names under scyjava.types (except the base).""" |
45 | | - base_package = __name__ |
46 | | - |
47 | | - if not fullname.startswith(base_package + ".") or fullname == base_package: |
48 | | - return None |
49 | | - |
50 | | - return importlib.util.spec_from_loader( |
51 | | - fullname, |
52 | | - ScyJavaTypesLoader(fullname), |
53 | | - origin="dynamic", |
54 | | - ) |
55 | | - |
56 | | - |
57 | | -class ScyJavaTypesLoader(Loader): |
58 | | - """Loader that lazily generates stubs and loads/synthesizes modules.""" |
59 | | - |
60 | | - def __init__(self, fullname: str) -> None: |
61 | | - self.fullname = fullname |
62 | | - |
63 | | - def create_module(self, spec: ModuleSpec) -> types.ModuleType | None: |
64 | | - """Load an existing module/package or lazily generate stubs then load.""" |
65 | | - pkg_dir, pkg_init, mod_file = _paths_for(spec.name, TYPES_DIR) |
66 | | - |
67 | | - def _load_module() -> types.ModuleType | None: |
68 | | - # Fast paths: concrete module file or package present |
69 | | - if pkg_init.exists() or mod_file.exists(): |
70 | | - return _load_generated_module(spec.name, TYPES_DIR) |
71 | | - if pkg_dir.is_dir(): |
72 | | - return _namespace_package(spec, pkg_dir) |
73 | | - return None |
74 | | - |
75 | | - if module := _load_module(): |
76 | | - return module |
77 | | - |
78 | | - # Nothing exists for this name: generate once under a lock |
79 | | - with _STUBS_LOCK: |
80 | | - # Re-check under the lock to avoid duplicate work |
81 | | - if not (pkg_init.exists() or mod_file.exists() or pkg_dir.exists()): |
82 | | - endpoints = ["org.scijava:parsington:3.1.0"] # TODO |
83 | | - generate_stubs(endpoints, output_dir=TYPES_DIR) |
84 | | - |
85 | | - # Retry after generation (or if another thread created it) |
86 | | - if module := _load_module(): |
87 | | - return module |
| 42 | + # if this is an import from this module ('scyjava.types.<name>') |
| 43 | + # check if the module exists, and if not, call generation routines |
| 44 | + if fullname.startswith(f"{__name__}."): |
| 45 | + with _STUBS_LOCK: |
| 46 | + # check if the spec already exists |
| 47 | + # under the module lock to avoid duplicate work |
| 48 | + if not _find_spec(fullname, path, target, skip=self): |
| 49 | + _generate_stubs() |
88 | 50 |
|
89 | | - raise ImportError(f"Generated module not found: {spec.name} under {pkg_dir}") |
| 51 | + return None |
90 | 52 |
|
91 | | - def exec_module(self, module: types.ModuleType) -> None: |
92 | | - pass |
93 | 53 |
|
| 54 | +def _generate_stubs() -> None: |
| 55 | + """Install stubs for all endpoints detected in `scyjava.config`. |
94 | 56 |
|
95 | | -def _paths_for(fullname: str, out_dir: Path) -> tuple[Path, Path, Path]: |
96 | | - """Return (pkg_dir, pkg_init, mod_file) for a scyjava.types.* fullname.""" |
97 | | - rel = fullname.split("scyjava.types.", 1)[1] |
98 | | - pkg_dir = out_dir / rel.replace(".", "/") |
99 | | - pkg_init = pkg_dir / "__init__.py" |
100 | | - mod_file = out_dir / (rel.replace(".", "/") + ".py") |
101 | | - return pkg_dir, pkg_init, mod_file |
102 | | - |
103 | | - |
104 | | -def _namespace_package(spec: ModuleSpec, pkg_dir: Path) -> types.ModuleType: |
105 | | - """Create a simple package module pointing at pkg_dir. |
106 | | -
|
107 | | - This fills the role of a namespace package, (a folder with no __init__.py). |
| 57 | + This could be expanded to include additional endpoints detected in, for example, |
| 58 | + python entry-points discovered in packages in the environment. |
108 | 59 | """ |
109 | | - module = types.ModuleType(spec.name) |
110 | | - module.__package__ = spec.name |
111 | | - module.__path__ = [str(pkg_dir)] |
112 | | - module.__spec__ = spec |
113 | | - return module |
114 | | - |
115 | | - |
116 | | -def _load_generated_module(fullname: str, out_dir: Path) -> types.ModuleType: |
117 | | - """Load a just-generated module/package from disk and return it.""" |
118 | | - _, pkg_init, mod_file = _paths_for(fullname, out_dir) |
119 | | - path = pkg_init if pkg_init.exists() else mod_file |
120 | | - if not path.exists(): |
121 | | - raise ImportError(f"Generated module not found: {fullname} at {path}") |
122 | | - |
123 | | - loader = SourceFileLoader(fullname, str(path)) |
124 | | - spec = importlib.util.spec_from_loader(fullname, loader, origin=str(path)) |
125 | | - if spec is None or spec.loader is None: |
126 | | - raise ImportError(f"Failed to build spec for: {fullname}") |
| 60 | + from scyjava import config |
| 61 | + from scyjava._stubs import generate_stubs |
| 62 | + |
| 63 | + generate_stubs( |
| 64 | + config.endpoints, |
| 65 | + output_dir=STUBS_DIR, |
| 66 | + add_runtime_imports=True, |
| 67 | + remove_namespace_only_stubs=True, |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def _find_spec( |
| 72 | + fullname: str, |
| 73 | + path: Sequence[str] | None, |
| 74 | + target: types.ModuleType | None = None, |
| 75 | + skip: object | None = None, |
| 76 | +) -> ModuleSpec | None: |
| 77 | + """Find a module spec, skipping finder `skip` to avoid recursion.""" |
| 78 | + # if the module is already loaded and has a spec, return it |
| 79 | + if module := sys.modules.get(fullname): |
| 80 | + try: |
| 81 | + if module.__spec__ is not None: |
| 82 | + return module.__spec__ |
| 83 | + except AttributeError: |
| 84 | + pass |
127 | 85 |
|
128 | | - spec.has_location = True # populate __file__ |
129 | | - sys.modules[fullname] = module = importlib.util.module_from_spec(spec) |
130 | | - spec.loader.exec_module(module) |
131 | | - return module |
132 | | - |
133 | | - |
134 | | -# ----------------------------------------------------------- |
| 86 | + for finder in sys.meta_path: |
| 87 | + if finder is not skip: |
| 88 | + try: |
| 89 | + spec = finder.find_spec(fullname, path, target) |
| 90 | + except AttributeError: |
| 91 | + continue |
| 92 | + else: |
| 93 | + if spec is not None: |
| 94 | + return spec |
| 95 | + return None |
135 | 96 |
|
136 | 97 |
|
137 | 98 | def _install_meta_finder() -> None: |
138 | | - for finder in sys.meta_path: |
139 | | - if isinstance(finder, ScyJavaTypesMetaFinder): |
140 | | - return |
141 | | - |
| 99 | + """Install the ScyJavaTypesMetaFinder into sys.meta_path if not already there.""" |
| 100 | + if any(isinstance(finder, ScyJavaTypesMetaFinder) for finder in sys.meta_path): |
| 101 | + return |
142 | 102 | sys.meta_path.insert(0, ScyJavaTypesMetaFinder()) |
143 | 103 |
|
144 | 104 |
|
|
0 commit comments