Skip to content
Snippets Groups Projects
Commit c1866938 authored by Michi Hostettler's avatar Michi Hostettler :coffee:
Browse files

Merge branch 'jpype-imports-context-manager' into 'master'

provide import system access through context manager

See merge request !32
parents 470f8138 0b92ed73
No related branches found
No related tags found
1 merge request!32provide import system access through context manager
Pipeline #1704654 passed
# Changelog
## [2.4.1] - 20.05.2020
### Changed
- add support for the JPype import system (`Manager.imports()`)
## [2.2.5] - 21.10.2019
### Changed
- avoid downloading jars at every execution
\ No newline at end of file
......@@ -58,12 +58,21 @@ Finally, from your code, you can start a JVM with the complete list of jars
using:
```python
>>> mgr = cmmnbuild_dep_manager.Manager()
>>> jpype = mgr.start_jpype_jvm()
mgr = cmmnbuild_dep_manager.Manager()
jpype = mgr.start_jpype_jvm()
```
At this stage, JPype is ready to use.
To enable the JPype (>= 0.7) import system, you can use the `imports()` context manager:
```python
mgr = cmmnbuild_dep_manager.Manager()
with mgr.imports():
from cern.some.service import Service
# ...
```
## Advanced usage
### Re-downloading jars
......@@ -84,8 +93,8 @@ Helper functions are provided to inspect the classes in the downloaded jars.
Provides a listing of all classes contained within the jars:
```python
>>> mgr = cmmnbuild_dep_manager.Manager()
>>> mgr.class_list()
mgr = cmmnbuild_dep_manager.Manager()
mgr.class_list()
['cern.accsoft.cals.extr.client.commandline.CommandLineException',
'cern.accsoft.cals.extr.client.commandline.CommandLineServiceBuilder',
'cern.accsoft.cals.extr.client.commandline.CommandMethod',
......@@ -98,8 +107,8 @@ Provides a listing of all classes contained within the jars:
Search for any class by name:
```python
>>> mgr = cmmnbuild_dep_manager.Manager()
>>> mgr.class_search('ServiceBuilder')
mgr = cmmnbuild_dep_manager.Manager()
mgr.class_search('ServiceBuilder')
['cern.accsoft.cals.extr.client.commandline.CommandLineServiceBuilder',
'cern.accsoft.cals.extr.client.service.ServiceBuilder',
'cern.cmw.rda3.client.service.ClientServiceBuilder',
......@@ -111,15 +120,15 @@ Search for any class by name:
Provide a pasteable example of how to use a specific class from Python:
```python
>>> mgr = cmmnbuild_dep_manager.Manager()
>>> mgr.class_hints('cern.accsoft.cals.extr.client.service.ServiceBuilder')
mgr = cmmnbuild_dep_manager.Manager()
mgr.class_hints('cern.accsoft.cals.extr.client.service.ServiceBuilder')
cern = jpype.JPackage('cern')
ServiceBuilder = cern.accsoft.cals.extr.client.service.ServiceBuilder
>>> jpype = mgr.start_jpype_jvm()
>>> cern = jpype.JPackage('cern')
>>> ServiceBuilder = cern.accsoft.cals.extr.client.service.ServiceBuilder
>>> ServiceBuilder
jpype = mgr.start_jpype_jvm()
cern = jpype.JPackage('cern')
ServiceBuilder = cern.accsoft.cals.extr.client.service.ServiceBuilder
ServiceBuilder
jpype._jclass.cern.accsoft.cals.extr.client.service.ServiceBuilder
```
......@@ -143,8 +152,8 @@ pyjapc
Which is equivalent to the following Python code:
```python
>>> mgr = cmmnbuild_dep_manager.Manager()
>>> mgr.register('pytimber', 'pyjapc')
mgr = cmmnbuild_dep_manager.Manager()
mgr.register('pytimber', 'pyjapc')
('pytimber', 'pyjapc')
```
......@@ -158,8 +167,8 @@ cmmnbuild_dep_manager manager directly. For example, manual installation of
a package can be achieved with:
```python
>>> mgr = cmmnbuild_dep_manager.Manager()
>>> mgr.install('pyjapc')
mgr = cmmnbuild_dep_manager.Manager()
mgr.install('pyjapc')
```
The `install()` function registers the package and resolves the dependencies
......@@ -193,8 +202,8 @@ class PyJapc:
Then the user executing:
```python
>>> import pyjapc
>>> japc = pyjapc.PyJapc()
import pyjapc
japc = pyjapc.PyJapc()
```
will cause the jars to be downloaded if they aren't already existing on disk
......
......@@ -79,6 +79,27 @@ class Manager(object):
yield
self.log.setLevel(orig_level)
@contextmanager
def imports(self):
'''Run a piece of code with the access to the Java universe through JPype imports.
When the context ends, the original python import behavior is restored.
'''
import sys
import importlib
old_sys_meta_path = list(sys.meta_path)
old_modules = set(sys.modules.keys())
import jpype
if not jpype.isJVMStarted():
self.start_jpype_jvm()
import jpype.imports
jpype.imports.registerDomain("cern")
yield jpype
sys.meta_path = old_sys_meta_path
added_modules = set(sys.modules.keys()) - old_modules
for mod in added_modules:
del sys.modules[mod]
importlib.invalidate_caches()
def help(self, method='__init__'):
'''Print the docstring for a method'''
print(getattr(self, method).__doc__)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment