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 # Changelog
## [2.4.1] - 20.05.2020
### Changed
- add support for the JPype import system (`Manager.imports()`)
## [2.2.5] - 21.10.2019 ## [2.2.5] - 21.10.2019
### Changed ### Changed
- avoid downloading jars at every execution - 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 ...@@ -58,12 +58,21 @@ Finally, from your code, you can start a JVM with the complete list of jars
using: using:
```python ```python
>>> mgr = cmmnbuild_dep_manager.Manager() mgr = cmmnbuild_dep_manager.Manager()
>>> jpype = mgr.start_jpype_jvm() jpype = mgr.start_jpype_jvm()
``` ```
At this stage, JPype is ready to use. 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 ## Advanced usage
### Re-downloading jars ### Re-downloading jars
...@@ -84,8 +93,8 @@ Helper functions are provided to inspect the classes in the downloaded 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: Provides a listing of all classes contained within the jars:
```python ```python
>>> mgr = cmmnbuild_dep_manager.Manager() mgr = cmmnbuild_dep_manager.Manager()
>>> mgr.class_list() mgr.class_list()
['cern.accsoft.cals.extr.client.commandline.CommandLineException', ['cern.accsoft.cals.extr.client.commandline.CommandLineException',
'cern.accsoft.cals.extr.client.commandline.CommandLineServiceBuilder', 'cern.accsoft.cals.extr.client.commandline.CommandLineServiceBuilder',
'cern.accsoft.cals.extr.client.commandline.CommandMethod', 'cern.accsoft.cals.extr.client.commandline.CommandMethod',
...@@ -98,8 +107,8 @@ Provides a listing of all classes contained within the jars: ...@@ -98,8 +107,8 @@ Provides a listing of all classes contained within the jars:
Search for any class by name: Search for any class by name:
```python ```python
>>> mgr = cmmnbuild_dep_manager.Manager() mgr = cmmnbuild_dep_manager.Manager()
>>> mgr.class_search('ServiceBuilder') mgr.class_search('ServiceBuilder')
['cern.accsoft.cals.extr.client.commandline.CommandLineServiceBuilder', ['cern.accsoft.cals.extr.client.commandline.CommandLineServiceBuilder',
'cern.accsoft.cals.extr.client.service.ServiceBuilder', 'cern.accsoft.cals.extr.client.service.ServiceBuilder',
'cern.cmw.rda3.client.service.ClientServiceBuilder', 'cern.cmw.rda3.client.service.ClientServiceBuilder',
...@@ -111,15 +120,15 @@ Search for any class by name: ...@@ -111,15 +120,15 @@ Search for any class by name:
Provide a pasteable example of how to use a specific class from Python: Provide a pasteable example of how to use a specific class from Python:
```python ```python
>>> mgr = cmmnbuild_dep_manager.Manager() mgr = cmmnbuild_dep_manager.Manager()
>>> mgr.class_hints('cern.accsoft.cals.extr.client.service.ServiceBuilder') mgr.class_hints('cern.accsoft.cals.extr.client.service.ServiceBuilder')
cern = jpype.JPackage('cern') cern = jpype.JPackage('cern')
ServiceBuilder = cern.accsoft.cals.extr.client.service.ServiceBuilder ServiceBuilder = cern.accsoft.cals.extr.client.service.ServiceBuilder
>>> jpype = mgr.start_jpype_jvm() jpype = mgr.start_jpype_jvm()
>>> cern = jpype.JPackage('cern') cern = jpype.JPackage('cern')
>>> ServiceBuilder = cern.accsoft.cals.extr.client.service.ServiceBuilder ServiceBuilder = cern.accsoft.cals.extr.client.service.ServiceBuilder
>>> ServiceBuilder ServiceBuilder
jpype._jclass.cern.accsoft.cals.extr.client.service.ServiceBuilder jpype._jclass.cern.accsoft.cals.extr.client.service.ServiceBuilder
``` ```
...@@ -143,8 +152,8 @@ pyjapc ...@@ -143,8 +152,8 @@ pyjapc
Which is equivalent to the following Python code: Which is equivalent to the following Python code:
```python ```python
>>> mgr = cmmnbuild_dep_manager.Manager() mgr = cmmnbuild_dep_manager.Manager()
>>> mgr.register('pytimber', 'pyjapc') mgr.register('pytimber', 'pyjapc')
('pytimber', 'pyjapc') ('pytimber', 'pyjapc')
``` ```
...@@ -158,8 +167,8 @@ cmmnbuild_dep_manager manager directly. For example, manual installation of ...@@ -158,8 +167,8 @@ cmmnbuild_dep_manager manager directly. For example, manual installation of
a package can be achieved with: a package can be achieved with:
```python ```python
>>> mgr = cmmnbuild_dep_manager.Manager() mgr = cmmnbuild_dep_manager.Manager()
>>> mgr.install('pyjapc') mgr.install('pyjapc')
``` ```
The `install()` function registers the package and resolves the dependencies The `install()` function registers the package and resolves the dependencies
...@@ -193,8 +202,8 @@ class PyJapc: ...@@ -193,8 +202,8 @@ class PyJapc:
Then the user executing: Then the user executing:
```python ```python
>>> import pyjapc import pyjapc
>>> japc = pyjapc.PyJapc() japc = pyjapc.PyJapc()
``` ```
will cause the jars to be downloaded if they aren't already existing on disk will cause the jars to be downloaded if they aren't already existing on disk
......
...@@ -79,6 +79,27 @@ class Manager(object): ...@@ -79,6 +79,27 @@ class Manager(object):
yield yield
self.log.setLevel(orig_level) 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__'): def help(self, method='__init__'):
'''Print the docstring for a method''' '''Print the docstring for a method'''
print(getattr(self, method).__doc__) print(getattr(self, method).__doc__)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment