|
|
|
@ -2,14 +2,17 @@ package net.abhinavsarkar.jywrapper;
|
|
|
|
|
|
|
|
|
|
import static net.abhinavsarkar.jywrapper.Messages._;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import net.abhinavsarkar.jywrapper.annotation.Wraps;
|
|
|
|
|
import net.abhinavsarkar.jywrapper.exception.PythonImportNotFoundException;
|
|
|
|
|
|
|
|
|
|
import org.python.core.Py;
|
|
|
|
|
import org.python.core.PyClass;
|
|
|
|
|
import org.python.core.PyFunction;
|
|
|
|
|
import org.python.core.PyModule;
|
|
|
|
|
import org.python.core.PyObject;
|
|
|
|
|
import org.python.core.PyString;
|
|
|
|
|
import org.python.core.PySystemState;
|
|
|
|
|
import org.python.core.PyType;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -18,7 +21,7 @@ import org.python.core.PyType;
|
|
|
|
|
* @param <T> The type of the java class to wrap the Python class/module with.
|
|
|
|
|
*/
|
|
|
|
|
public final class JyWrapper {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private JyWrapper() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -27,20 +30,20 @@ public final class JyWrapper {
|
|
|
|
|
if (annotation == null) {
|
|
|
|
|
throw new PythonImportNotFoundException(_("JyWrapper.7", javaClass)); //$NON-NLS-1$
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return wrap(javaClass, annotation.value());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param pyImportName The full import name of the Python class/module
|
|
|
|
|
* to wrap.
|
|
|
|
|
* @return An instance of {@link UninitedPyObjectWrapper}, ready to be
|
|
|
|
|
* @return An instance of {@link UninitedPyObjectWrapper}, ready to be
|
|
|
|
|
* initialized.
|
|
|
|
|
* @throws IllegalStateException Thrown if the java Class to be used to
|
|
|
|
|
* wrap the Python module/class, has not been supplied by earlier
|
|
|
|
|
* wrap the Python module/class, has not been supplied by earlier
|
|
|
|
|
* calling {@link JyWrapper#with(Class)}.
|
|
|
|
|
* @throws IllegalArgumentException Thrown if the pyImportName parameter
|
|
|
|
|
* is null.
|
|
|
|
|
* is null.
|
|
|
|
|
*/
|
|
|
|
|
public static <T> T wrap(final Class<T> javaClass, final String pyImportName) {
|
|
|
|
|
if (javaClass == null) {
|
|
|
|
@ -49,24 +52,24 @@ public final class JyWrapper {
|
|
|
|
|
if (pyImportName == null) {
|
|
|
|
|
throw new IllegalArgumentException(_("JyWrapper.6", "pyImportName")); //$NON-NLS-1$ //$NON-NLS-2$
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final PyObject pyImport = PyImportLoader.loadPyImport(pyImportName);
|
|
|
|
|
if (!(pyImport instanceof PyType
|
|
|
|
|
|| pyImport instanceof PyModule
|
|
|
|
|
if (!(pyImport instanceof PyType
|
|
|
|
|
|| pyImport instanceof PyModule
|
|
|
|
|
|| pyImport instanceof PyClass)) {
|
|
|
|
|
throw new IllegalArgumentException(_("JyWrapper.5", pyImportName)); //$NON-NLS-1$
|
|
|
|
|
}
|
|
|
|
|
return Util.py2Java(pyImport, javaClass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param <T> The return type of the {@link PyCallable} instance.
|
|
|
|
|
* @param <T> The return type of the {@link PyCallable} instance.
|
|
|
|
|
* @param pyImportName The full import name of the Python function to wrap.
|
|
|
|
|
* @param returnType The class of the return type.
|
|
|
|
|
* @return An instance of {@link PyCallable} which wraps the
|
|
|
|
|
* Python function given in parameter.
|
|
|
|
|
* @throws IllegalArgumentException Thrown if the any of the parameters
|
|
|
|
|
* supplied are null or if the pyImportName parameter supplied does not
|
|
|
|
|
* @throws IllegalArgumentException Thrown if the any of the parameters
|
|
|
|
|
* supplied are null or if the pyImportName parameter supplied does not
|
|
|
|
|
* correspond to a Python function.
|
|
|
|
|
*/
|
|
|
|
|
public static <T> PyCallable<T> wrapPyFunction(
|
|
|
|
@ -77,16 +80,33 @@ public final class JyWrapper {
|
|
|
|
|
if (returnType == null) {
|
|
|
|
|
throw new IllegalArgumentException(_("JyWrapper.6", "returnType")); //$NON-NLS-1$ //$NON-NLS-2$
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final PyObject pyImport = PyImportLoader.loadPyImport(pyImportName);
|
|
|
|
|
if (!(pyImport instanceof PyFunction)) {
|
|
|
|
|
throw new IllegalArgumentException(_("JyWrapper.0", pyImportName)); //$NON-NLS-1$
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
final PyCallable<T> newInstance = PyObjectProxy.newInstance(
|
|
|
|
|
pyImport, PyCallable.class);
|
|
|
|
|
return newInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void addToPythonPath(final URI path) {
|
|
|
|
|
final PySystemState pySystemState = Py.getSystemState();
|
|
|
|
|
final PyString resolvedPath = Util.resolvePath(path);
|
|
|
|
|
if (resolvedPath != null && !pySystemState.path.contains(resolvedPath)) {
|
|
|
|
|
pySystemState.path.append(resolvedPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void removeFromPythonPath(final URI path) {
|
|
|
|
|
final PySystemState pySystemState = Py.getSystemState();
|
|
|
|
|
final PyString resolvedPath = Util.resolvePath(path);
|
|
|
|
|
if (resolvedPath != null && pySystemState.path.contains(resolvedPath)) {
|
|
|
|
|
pySystemState.path.remove(resolvedPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|