Skip to content

Commit 2cb4836

Browse files
committed
refactor: rename dynamic_import to setup_java_imports and update usage in stubs
1 parent 11649fa commit 2cb4836

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

src/scyjava/_stubs/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ._dynamic_import import dynamic_import
1+
from ._dynamic_import import setup_java_imports
22
from ._genstubs import generate_stubs
33

4-
__all__ = ["dynamic_import", "generate_stubs"]
4+
__all__ = ["setup_java_imports", "generate_stubs"]

src/scyjava/_stubs/_dynamic_import.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,51 @@
44
from typing import Any, Callable, Sequence
55

66

7-
def dynamic_import(
7+
def setup_java_imports(
88
module_name: str,
99
module_file: str,
1010
endpoints: Sequence[str] = (),
1111
base_prefix: str = "",
1212
) -> tuple[list[str], Callable[[str], Any]]:
13+
"""Setup a module to dynamically import Java class names.
14+
15+
This function creates a `__getattr__` function that, when called, will dynamically
16+
import the requested class from the Java namespace corresponding to this module.
17+
18+
:param module_name: The dotted name/identifier of the module that is calling this
19+
function (usually `__name__` in the calling module).
20+
:param module_file: The path to the module file (usually `__file__` in the calling
21+
module).
22+
:param endpoints: A list of Java endpoints to add to the scyjava configuration.
23+
:param base_prefix: The base prefix for the Java package name. This is used when
24+
determining the Java class path for the requested class. The java class path
25+
will be truncated to only the part including the base_prefix and after. This
26+
makes it possible to embed a module in a subpackage (like `scyjava.types`) and
27+
still have the correct Java class path.
28+
:return: A 2-tuple containing:
29+
- A list of all classes in the module (as defined in the stub file), to be
30+
assigned to `__all__`.
31+
- A callable that takes a class name and returns a proxy for the Java class.
32+
This callable should be assigned to `__getattr__` in the calling module.
33+
The proxy object, when called, will start the JVM, import the Java class,
34+
and return an instance of the class. The JVM will *only* be started when
35+
the object is called.
36+
37+
Example:
38+
If the module calling this function is named `scyjava.types.org.scijava.parsington`,
39+
then it should invoke this function as:
40+
41+
.. code-block:: python
42+
43+
from scyjava._stubs import setup_java_imports
44+
45+
__all__, __getattr__ = setup_java_imports(
46+
__name__,
47+
__file__,
48+
endpoints=["org.scijava:parsington:3.1.0"],
49+
base_prefix="org"
50+
)
51+
"""
1352
import scyjava
1453
import scyjava.config
1554

src/scyjava/_stubs/_genstubs.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import scyjava
1717
import scyjava.config
18-
import stubgenj
1918

2019
if TYPE_CHECKING:
2120
from collections.abc import Sequence
@@ -69,6 +68,14 @@ def generate_stubs(
6968
the `__init__.pyi` for any given module will be whatever whatever the *last*
7069
stub generator wrote to it (and therefore inaccurate).
7170
"""
71+
try:
72+
import stubgenj
73+
except ImportError as e:
74+
raise ImportError(
75+
"stubgenj is not installed, but is required to generate java stubs. "
76+
"Please install it with `pip/conda install stubgenj`."
77+
) from e
78+
7279
import jpype
7380

7481
startJVM = jpype.startJVM
@@ -145,9 +152,9 @@ def _patched_start(*args: Any, **kwargs: Any) -> None:
145152
# it creates a __getattr__ function that will dynamically import
146153
# the requested class from the Java namespace corresponding to this module.
147154
# see scyjava._stubs for implementation details.
148-
from scyjava._stubs import dynamic_import
155+
from scyjava._stubs import setup_java_imports
149156
150-
__all__, __getattr__ = dynamic_import(
157+
__all__, __getattr__ = setup_java_imports(
151158
__name__,
152159
__file__,
153160
endpoints={endpoints},

0 commit comments

Comments
 (0)