Skip to content

Commit 43cb06d

Browse files
committed
initial
1 parent ed0add6 commit 43cb06d

File tree

5 files changed

+558
-1
lines changed

5 files changed

+558
-1
lines changed

METAFINDER_README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# scyjava.types Meta Path Finder Infrastructure
2+
3+
This module provides a complete meta path finder infrastructure for `scyjava.types` that:
4+
5+
## Core Features
6+
7+
1. **Intercepts imports** from `scyjava.types.XXX` using a custom meta path finder
8+
2. **Calls a user-defined function** at import time to generate the module
9+
3. **Executes the import** after the module is generated
10+
4. **Uses `__name__` for portability** - works regardless of where the module is located
11+
12+
## Key Components
13+
14+
### ScyJavaTypesMetaFinder
15+
- Implements the `find_spec()` method required by Python's import system
16+
- Only handles imports that start with `scyjava.types.`
17+
- Creates a `ModuleSpec` with our custom loader
18+
19+
### ScyJavaTypesLoader
20+
- Implements `create_module()` and `exec_module()` for the new import system
21+
- Also implements `load_module()` for backward compatibility
22+
- Calls the registered generator function to create modules
23+
24+
### Module Generator Function
25+
- User-provided function that receives the full module name
26+
- Must return a `types.ModuleType` instance
27+
- Called exactly once per unique import (modules are cached)
28+
29+
## API Functions
30+
31+
- `set_module_generator(func)` - Register your module generation function
32+
- `get_registered_generator()` - Get the currently registered function
33+
- `is_meta_finder_installed()` - Check if the meta finder is active
34+
- `clear_generated_modules()` - Remove generated modules from cache
35+
- `list_generated_modules()` - List all currently loaded generated modules
36+
37+
## Usage Pattern
38+
39+
```python
40+
import scyjava.types
41+
42+
def my_generator(module_name: str) -> types.ModuleType:
43+
# module_name will be something like "scyjava.types.ArrayList"
44+
module = types.ModuleType(module_name)
45+
46+
# Extract class name from the module path
47+
class_name = module_name.split('.')[-1] # "ArrayList"
48+
49+
# Generate your class/content here
50+
generated_class = create_java_stub(class_name)
51+
setattr(module, class_name, generated_class)
52+
53+
module.__file__ = f"<generated:{module_name}>"
54+
return module
55+
56+
# Register the generator
57+
scyjava.types.set_module_generator(my_generator)
58+
59+
# Now imports will trigger the generator
60+
import scyjava.types.ArrayList as al_module
61+
ArrayList = al_module.ArrayList
62+
63+
# Or using from-import
64+
from scyjava.types import HashMap as hm_module
65+
HashMap = hm_module.HashMap
66+
```
67+
68+
## Error Handling
69+
70+
- If no generator is registered, imports raise `ImportError`
71+
- If the generator returns `None`, imports raise `ImportError`
72+
- Generator exceptions propagate to the import statement
73+
74+
## Caching
75+
76+
- Modules are automatically cached in `sys.modules`
77+
- Subsequent imports of the same module return the cached version
78+
- Use `clear_generated_modules()` to force regeneration
79+
80+
This infrastructure provides the foundation for dynamic Java class stub generation
81+
at import time, enabling type-safe imports with lazy initialization.

src/scyjava/types/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Ignore everything in this directory
22
*
3-
# Except this file
3+
# Except this file and the __init__.py file
44
!.gitignore
5+
!__init__.py

0 commit comments

Comments
 (0)