|
1 | 1 | import ast |
2 | 2 | from logging import warning |
3 | 3 | from pathlib import Path |
4 | | -from typing import Any, Callable |
| 4 | +from typing import Any, Callable, Sequence |
5 | 5 |
|
6 | 6 |
|
7 | 7 | def dynamic_import( |
8 | | - module_name: str, module_file: str, *endpoints: str |
| 8 | + module_name: str, |
| 9 | + module_file: str, |
| 10 | + endpoints: Sequence[str] = (), |
| 11 | + base_prefix: str = "", |
9 | 12 | ) -> tuple[list[str], Callable[[str], Any]]: |
10 | 13 | import scyjava |
11 | 14 | import scyjava.config |
@@ -35,27 +38,25 @@ def module_getattr(name: str, mod_name: str = module_name) -> Any: |
35 | 38 | if module_all and name not in module_all: |
36 | 39 | raise AttributeError(f"module {module_name!r} has no attribute {name!r}") |
37 | 40 |
|
38 | | - # this strip is important... and tricky, because it depends on the |
39 | | - # namespace that we intend to install the stubs into. |
40 | | - install_path = "scyjava.types." |
41 | | - if mod_name.startswith(install_path): |
42 | | - mod_name = mod_name[len(install_path) :] |
| 41 | + # cut the mod_name to only the part including the base_prefix and after |
| 42 | + if base_prefix in mod_name: |
| 43 | + mod_name = mod_name[mod_name.index(base_prefix) :] |
43 | 44 |
|
44 | | - full_name = f"{mod_name}.{name}" |
| 45 | + class_path = f"{mod_name}.{name}" |
45 | 46 |
|
46 | 47 | class ProxyMeta(type): |
47 | 48 | def __repr__(self) -> str: |
48 | | - return f"<scyjava class {full_name!r}>" |
| 49 | + return f"<scyjava class {class_path!r}>" |
49 | 50 |
|
50 | 51 | class Proxy(metaclass=ProxyMeta): |
51 | 52 | def __new__(_cls_, *args: Any, **kwargs: Any) -> Any: |
52 | | - cls = scyjava.jimport(full_name) |
| 53 | + cls = scyjava.jimport(class_path) |
53 | 54 | return cls(*args, **kwargs) |
54 | 55 |
|
55 | 56 | Proxy.__name__ = name |
56 | 57 | Proxy.__qualname__ = name |
57 | 58 | Proxy.__module__ = module_name |
58 | | - Proxy.__doc__ = f"Proxy for {full_name}" |
| 59 | + Proxy.__doc__ = f"Proxy for {class_path}" |
59 | 60 | return Proxy |
60 | 61 |
|
61 | 62 | return module_all, module_getattr |
0 commit comments