diff --git a/common/source/java/ch/systemsx/cisd/common/jython/all/IJythonFunction.java b/common/source/java/ch/systemsx/cisd/common/jython/IJythonFunction.java similarity index 94% rename from common/source/java/ch/systemsx/cisd/common/jython/all/IJythonFunction.java rename to common/source/java/ch/systemsx/cisd/common/jython/IJythonFunction.java index 3193345d765b62032d1d13c7bcdb7726f37a7618..4a1718c5b2e9c6e3c284e932bd6a3a24583e8073 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython/all/IJythonFunction.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/IJythonFunction.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ch.systemsx.cisd.common.jython.all; +package ch.systemsx.cisd.common.jython; public interface IJythonFunction { diff --git a/common/source/java/ch/systemsx/cisd/common/jython/all/IJythonInterpreter.java b/common/source/java/ch/systemsx/cisd/common/jython/IJythonInterpreter.java similarity index 94% rename from common/source/java/ch/systemsx/cisd/common/jython/all/IJythonInterpreter.java rename to common/source/java/ch/systemsx/cisd/common/jython/IJythonInterpreter.java index 9ef639b44d459853481c1fa8c7929ce76612748b..627a2a3630e6abfce813ee71c07cf86bff583caf 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython/all/IJythonInterpreter.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/IJythonInterpreter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ch.systemsx.cisd.common.jython.all; +package ch.systemsx.cisd.common.jython; public interface IJythonInterpreter { diff --git a/common/source/java/ch/systemsx/cisd/common/jython/all/IJythonInterpreterFactory.java b/common/source/java/ch/systemsx/cisd/common/jython/IJythonInterpreterFactory.java similarity index 94% rename from common/source/java/ch/systemsx/cisd/common/jython/all/IJythonInterpreterFactory.java rename to common/source/java/ch/systemsx/cisd/common/jython/IJythonInterpreterFactory.java index b247a0e37da3becd4a4991adaa71dce8e9cdde71..6ff1afb4477e48987474a2bdda6b38ddafadc869 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython/all/IJythonInterpreterFactory.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/IJythonInterpreterFactory.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ch.systemsx.cisd.common.jython.all; +package ch.systemsx.cisd.common.jython; public interface IJythonInterpreterFactory diff --git a/common/source/java/ch/systemsx/cisd/common/jython/all/IJythonObject.java b/common/source/java/ch/systemsx/cisd/common/jython/IJythonObject.java similarity index 94% rename from common/source/java/ch/systemsx/cisd/common/jython/all/IJythonObject.java rename to common/source/java/ch/systemsx/cisd/common/jython/IJythonObject.java index cb08c5d4214ee18ac3ad99290bb5a67df236d30d..e19b752e617f9b8e7ec921727c414501840d9c78 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython/all/IJythonObject.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/IJythonObject.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ch.systemsx.cisd.common.jython.all; +package ch.systemsx.cisd.common.jython; public interface IJythonObject { diff --git a/common/source/java/ch/systemsx/cisd/common/jython/JythonScript.java b/common/source/java/ch/systemsx/cisd/common/jython/JythonScript.java index a8f0c0ee9108b99c912f925f711a023a215ae5cb..4f996c5a6914a5bfb460343c4b1395cd53ae3e66 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython/JythonScript.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/JythonScript.java @@ -19,7 +19,7 @@ package ch.systemsx.cisd.common.jython; /** * @author pkupczyk */ -class JythonScript +public class JythonScript { private String script; diff --git a/common/source/java/ch/systemsx/cisd/common/jython/JythonScriptBatch.java b/common/source/java/ch/systemsx/cisd/common/jython/JythonScriptBatch.java index ac1c238131c64a0cf8ac85bbe1676e8f5199a0e9..ae275a5e75dcd224ec4a48bbfcbb02bb46f03c6a 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython/JythonScriptBatch.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/JythonScriptBatch.java @@ -19,7 +19,7 @@ package ch.systemsx.cisd.common.jython; /** * @author pkupczyk */ -class JythonScriptBatch extends JythonScriptLines +public class JythonScriptBatch extends JythonScriptLines { } diff --git a/common/source/java/ch/systemsx/cisd/common/jython/all/Jython25InterpreterFactory.java b/common/source/java/ch/systemsx/cisd/common/jython/all/Jython25InterpreterFactory.java deleted file mode 100644 index de31a3092967348006fc0661b3a372fd8d446e1d..0000000000000000000000000000000000000000 --- a/common/source/java/ch/systemsx/cisd/common/jython/all/Jython25InterpreterFactory.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2015 ETH Zuerich, CISD - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.systemsx.cisd.common.jython.all; - -import org.python.core.PyBaseCode; -import org.python.core.PyFunction; -import org.python.core.PyInteger; -import org.python.core.PyObject; - -import ch.systemsx.cisd.common.jython.JythonUtils; -import ch.systemsx.cisd.common.jython.PythonInterpreter; - -public class Jython25InterpreterFactory implements IJythonInterpreterFactory -{ - @Override - public IJythonInterpreter createInterpreter() - { - return new Jython25Interpreter(); - } -} - -class Jython25Function implements IJythonFunction -{ - PyFunction function; - - Jython25Function(PyFunction function) - { - this.function = function; - } - - @Override - public IJythonObject invoke(Object... arguments) - { - PyObject result = JythonUtils.invokeFunction(function, arguments); - if (result == null) - { - return null; - } - return new Jython25Object(result); - } - - @Override - public int getArgumentCount() - { - if (this.function.func_code instanceof PyBaseCode) - { - return ((PyBaseCode) this.function.func_code).co_argcount; - } - return -1; - } -} - -class Jython25Object implements IJythonObject -{ - PyObject pyObject; - - public Jython25Object(PyObject object) - { - this.pyObject = object; - } - - @Override - public String getJythonType() - { - return pyObject.getClass().toString(); - } - - @Override - public boolean isInteger() - { - return pyObject instanceof PyInteger; - } - - @Override - public int asInteger() - { - if (false == isInteger()) - { - throw new IllegalStateException("Object is not integer"); - } - return ((PyInteger) pyObject).asInt(); - } - -} - -class Jython25Interpreter implements IJythonInterpreter -{ - private PythonInterpreter interpreter; - - public Jython25Interpreter() - { - interpreter = PythonInterpreter.createIsolatedPythonInterpreter(); - } - - @Override - public void exec(String scriptString, String scriptFile) - { - interpreter.exec(scriptString, scriptFile); - } - - @Override - public void set(String variableName, Object object) - { - interpreter.set(variableName, object); - } - - @Override - public void releaseResources() - { - interpreter.releaseResources(); - } - - @Override - public IJythonFunction tryJythonFunction(String name) - { - PyFunction function = JythonUtils.tryJythonFunction(interpreter, name); - if (function == null) - { - return null; - } - else - { - return new Jython25Function(function); - } - } -} diff --git a/common/source/java/ch/systemsx/cisd/common/jython/all/Jython27InterpreterFactory.java b/common/source/java/ch/systemsx/cisd/common/jython/all/Jython27InterpreterFactory.java deleted file mode 100644 index ac3bb3d7d240a645a1c4c17f81d7a344a5072144..0000000000000000000000000000000000000000 --- a/common/source/java/ch/systemsx/cisd/common/jython/all/Jython27InterpreterFactory.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright 2015 ETH Zuerich, CISD - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.systemsx.cisd.common.jython.all; - -import org.python27.core.PyBaseCode; -import org.python27.core.PyFunction; -import org.python27.core.PyInteger; -import org.python27.core.PyObject; - -import ch.systemsx.cisd.common.jython27.JythonUtils; -import ch.systemsx.cisd.common.jython27.PythonInterpreter27; - -public class Jython27InterpreterFactory implements IJythonInterpreterFactory -{ - - @Override - public IJythonInterpreter createInterpreter() - { - return new Jython27Interpreter(); - } -} - -class Jython27Function implements IJythonFunction -{ - PyFunction function; - - Jython27Function(PyFunction function) - { - this.function = function; - } - - @Override - public IJythonObject invoke(Object... arguments) - { - PyObject result = JythonUtils.invokeFunction(function, arguments); - if (result == null) - { - return null; - } - return new Jython27Object(result); - } - - @SuppressWarnings("deprecation") - @Override - public int getArgumentCount() - { - if (this.function.getFuncCode() instanceof PyBaseCode) - { - return ((PyBaseCode) this.function.getFuncCode()).co_argcount; - // return ((PyBaseCode) this.function.func_code).co_argcount; - } - return -1; - } -} - -class Jython27Object implements IJythonObject -{ - PyObject pyObject; - - public Jython27Object(PyObject object) - { - this.pyObject = object; - } - - @Override - public String getJythonType() - { - return pyObject.getClass().toString(); - } - - @Override - public boolean isInteger() - { - return pyObject instanceof PyInteger; - } - - @Override - public int asInteger() - { - if (false == isInteger()) - { - throw new IllegalStateException("Object is not integer"); - } - return ((PyInteger) pyObject).asInt(); - } - -} - -class Jython27Interpreter implements IJythonInterpreter -{ - private PythonInterpreter27 interpreter; - - public Jython27Interpreter() - { - interpreter = PythonInterpreter27.createIsolatedPythonInterpreter(); - } - - @Override - public void exec(String scriptString, String scriptFile) - { - interpreter.exec(scriptString, scriptFile); - } - - @Override - public void set(String variableName, Object object) - { - interpreter.set(variableName, object); - } - - @Override - public void releaseResources() - { - interpreter.releaseResources(); - } - - @Override - public IJythonFunction tryJythonFunction(String name) - { - PyFunction function = JythonUtils.tryJythonFunction(interpreter, name); - if (function == null) - { - return null; - } - else - { - return new Jython27Function(function); - } - } -} diff --git a/common/source/java/ch/systemsx/cisd/common/jython/v25/Jython25InterpreterFactory.java b/common/source/java/ch/systemsx/cisd/common/jython/v25/Jython25InterpreterFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..2d31ee7ed1254834edbcbf4cd74393f74d8e4a58 --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/jython/v25/Jython25InterpreterFactory.java @@ -0,0 +1,144 @@ +/* + * Copyright 2015 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.jython.v25; + +import org.python.core.PyBaseCode; +import org.python.core.PyFunction; +import org.python.core.PyInteger; +import org.python.core.PyObject; + +import ch.systemsx.cisd.common.jython.IJythonFunction; +import ch.systemsx.cisd.common.jython.IJythonInterpreter; +import ch.systemsx.cisd.common.jython.IJythonInterpreterFactory; +import ch.systemsx.cisd.common.jython.IJythonObject; +import ch.systemsx.cisd.common.jython.JythonUtils; +import ch.systemsx.cisd.common.jython.PythonInterpreter; + +public class Jython25InterpreterFactory implements IJythonInterpreterFactory +{ + @Override + public IJythonInterpreter createInterpreter() + { + return new Jython25Interpreter(); + } + + static class Jython25Function implements IJythonFunction + { + PyFunction function; + + Jython25Function(PyFunction function) + { + this.function = function; + } + + @Override + public IJythonObject invoke(Object... arguments) + { + PyObject result = JythonUtils.invokeFunction(function, arguments); + if (result == null) + { + return null; + } + return new Jython25Object(result); + } + + @Override + public int getArgumentCount() + { + if (this.function.func_code instanceof PyBaseCode) + { + return ((PyBaseCode) this.function.func_code).co_argcount; + } + return -1; + } + } + + static class Jython25Object implements IJythonObject + { + PyObject pyObject; + + public Jython25Object(PyObject object) + { + this.pyObject = object; + } + + @Override + public String getJythonType() + { + return pyObject.getClass().toString(); + } + + @Override + public boolean isInteger() + { + return pyObject instanceof PyInteger; + } + + @Override + public int asInteger() + { + if (false == isInteger()) + { + throw new IllegalStateException("Object is not integer"); + } + return ((PyInteger) pyObject).asInt(); + } + + } + + static class Jython25Interpreter implements IJythonInterpreter + { + private PythonInterpreter interpreter; + + public Jython25Interpreter() + { + interpreter = PythonInterpreter.createIsolatedPythonInterpreter(); + } + + @Override + public void exec(String scriptString, String scriptFile) + { + interpreter.exec(scriptString, scriptFile); + } + + @Override + public void set(String variableName, Object object) + { + interpreter.set(variableName, object); + } + + @Override + public void releaseResources() + { + interpreter.releaseResources(); + } + + @Override + public IJythonFunction tryJythonFunction(String name) + { + PyFunction function = JythonUtils.tryJythonFunction(interpreter, name); + if (function == null) + { + return null; + } + else + { + return new Jython25Function(function); + } + } + } +} diff --git a/common/source/java/ch/systemsx/cisd/common/jython/v27/Jython27InterpreterFactory.java b/common/source/java/ch/systemsx/cisd/common/jython/v27/Jython27InterpreterFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..2e3581ec6a1d4ac605c1bfdc063f40fe7ceaae1e --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/jython/v27/Jython27InterpreterFactory.java @@ -0,0 +1,146 @@ +/* + * Copyright 2015 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.jython.v27; + +import org.python27.core.PyBaseCode; +import org.python27.core.PyFunction; +import org.python27.core.PyInteger; +import org.python27.core.PyObject; + +import ch.systemsx.cisd.common.jython.IJythonFunction; +import ch.systemsx.cisd.common.jython.IJythonInterpreter; +import ch.systemsx.cisd.common.jython.IJythonInterpreterFactory; +import ch.systemsx.cisd.common.jython.IJythonObject; + +public class Jython27InterpreterFactory implements IJythonInterpreterFactory +{ + + @Override + public IJythonInterpreter createInterpreter() + { + return new Jython27Interpreter(); + } + + static class Jython27Function implements IJythonFunction + { + PyFunction function; + + Jython27Function(PyFunction function) + { + this.function = function; + } + + @Override + public IJythonObject invoke(Object... arguments) + { + PyObject result = JythonUtils.invokeFunction(function, arguments); + if (result == null) + { + return null; + } + return new Jython27Object(result); + } + + @SuppressWarnings("deprecation") + @Override + public int getArgumentCount() + { + if (this.function.getFuncCode() instanceof PyBaseCode) + { + return ((PyBaseCode) this.function.getFuncCode()).co_argcount; + // return ((PyBaseCode) this.function.func_code).co_argcount; + } + return -1; + } + } + + static class Jython27Object implements IJythonObject + { + PyObject pyObject; + + public Jython27Object(PyObject object) + { + this.pyObject = object; + } + + @Override + public String getJythonType() + { + return pyObject.getClass().toString(); + } + + @Override + public boolean isInteger() + { + return pyObject instanceof PyInteger; + } + + @Override + public int asInteger() + { + if (false == isInteger()) + { + throw new IllegalStateException("Object is not integer"); + } + return ((PyInteger) pyObject).asInt(); + } + + } + + static class Jython27Interpreter implements IJythonInterpreter + { + private PythonInterpreter27 interpreter; + + public Jython27Interpreter() + { + interpreter = PythonInterpreter27.createIsolatedPythonInterpreter(); + } + + @Override + public void exec(String scriptString, String scriptFile) + { + interpreter.exec(scriptString, scriptFile); + } + + @Override + public void set(String variableName, Object object) + { + interpreter.set(variableName, object); + } + + @Override + public void releaseResources() + { + interpreter.releaseResources(); + } + + @Override + public IJythonFunction tryJythonFunction(String name) + { + PyFunction function = JythonUtils.tryJythonFunction(interpreter, name); + if (function == null) + { + return null; + } + else + { + return new Jython27Function(function); + } + } + } + +} \ No newline at end of file diff --git a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptCommand.java b/common/source/java/ch/systemsx/cisd/common/jython/v27/JythonScriptCommand.java similarity index 88% rename from common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptCommand.java rename to common/source/java/ch/systemsx/cisd/common/jython/v27/JythonScriptCommand.java index 61791c95c4f2a79bc836d6a74441e68698af7cae..a531cf13d9f80d0089ed726d8950dcfec1dacec1 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptCommand.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/v27/JythonScriptCommand.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ch.systemsx.cisd.common.jython27; +package ch.systemsx.cisd.common.jython.v27; import org.python27.core.CompileMode; import org.python27.core.CompilerFlags; @@ -22,10 +22,12 @@ import org.python27.core.Py; import org.python27.core.PyException; import org.python27.core.PyObject; +import ch.systemsx.cisd.common.jython.JythonScriptLines; + /** * @author pkupczyk */ -public class JythonScriptCommand extends JythonScriptLines +class JythonScriptCommand extends JythonScriptLines { public boolean isNextCommand(String line) diff --git a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptSplitter.java b/common/source/java/ch/systemsx/cisd/common/jython/v27/JythonScriptSplitter.java similarity index 91% rename from common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptSplitter.java rename to common/source/java/ch/systemsx/cisd/common/jython/v27/JythonScriptSplitter.java index 580d563bfe4184ef00603c2b7a87d4ec6c73b67a..d113f6fb1f6cd7ac85c9e99123123ea76f57c37b 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptSplitter.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/v27/JythonScriptSplitter.java @@ -14,12 +14,16 @@ * limitations under the License. */ -package ch.systemsx.cisd.common.jython27; +package ch.systemsx.cisd.common.jython.v27; import java.util.List; import org.python27.core.PyException; +import ch.systemsx.cisd.common.jython.JythonScript; +import ch.systemsx.cisd.common.jython.JythonScriptBatch; +import ch.systemsx.cisd.common.jython.JythonScriptBatches; + /** * Splits jython code into smaller batches to overcome 64KB script size limitation. * diff --git a/common/source/java/ch/systemsx/cisd/common/jython/v27/JythonUtils.java b/common/source/java/ch/systemsx/cisd/common/jython/v27/JythonUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..19a75bf2ba9f40e2e0063abef9b7201a80b5e1ee --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/jython/v27/JythonUtils.java @@ -0,0 +1,82 @@ +/* + * Copyright 2011 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.jython.v27; + +import java.util.HashMap; +import java.util.Map; + +import org.python27.core.Py; +import org.python27.core.PyDictionary; +import org.python27.core.PyFunction; +import org.python27.core.PyObject; +import org.python27.core.PySequenceList; + +/** + * Jython utility methods. + * + * @author Kaloyan Enimanev + */ +class JythonUtils +{ + + /** + * Converts a {@link PyDictionary} to a Java map. + * + * @return a map equivalent to the given Jython dictionary. + */ + static Map<String, String> convertPyDictToMap(PyDictionary result) + { + Map<String, String> javaMap = new HashMap<String, String>(); + for (Object item : result.items()) + { + PySequenceList tuple = (PySequenceList) item; + javaMap.put(tuple.get(0).toString(), tuple.get(1).toString()); + } + return javaMap; + } + + /** + * Tries to get a function defined in jython script + * + * @return a Jython function object, or <code>null</code> if function doesn't exist. + */ + static PyFunction tryJythonFunction(PythonInterpreter27 interpreter, String functionName) + { + try + { + PyFunction function = interpreter.get(functionName, PyFunction.class); + return function; + } catch (Exception e) + { + return null; + } + } + + /** + * Turn all arguments into a python objects, and calls the specified function. + */ + static PyObject invokeFunction(PyFunction function, Object... args) + { + PyObject[] pyArgs = new PyObject[args.length]; + for (int i = 0; i < args.length; i++) + { + pyArgs[i] = Py.java2py(args[i]); + } + return function.__call__(pyArgs); + } + +} diff --git a/common/source/java/ch/systemsx/cisd/common/jython27/PythonInterpreter27.java b/common/source/java/ch/systemsx/cisd/common/jython/v27/PythonInterpreter27.java similarity index 94% rename from common/source/java/ch/systemsx/cisd/common/jython27/PythonInterpreter27.java rename to common/source/java/ch/systemsx/cisd/common/jython/v27/PythonInterpreter27.java index 6e4217180d7a682707dfcd8f7740f03d29809c9d..62f0a229873153621d36676c479507624540b9f9 100644 --- a/common/source/java/ch/systemsx/cisd/common/jython27/PythonInterpreter27.java +++ b/common/source/java/ch/systemsx/cisd/common/jython/v27/PythonInterpreter27.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package ch.systemsx.cisd.common.jython27; +package ch.systemsx.cisd.common.jython.v27; import org.apache.log4j.Logger; import org.python27.core.CompileMode; @@ -31,7 +31,7 @@ import ch.systemsx.cisd.common.resource.Resources; /** * @author pkupczyk */ -public class PythonInterpreter27 extends org.python27.util.PythonInterpreter +class PythonInterpreter27 extends org.python27.util.PythonInterpreter { private Logger log = LogFactory.getLogger(LogCategory.OPERATION, getClass()); @@ -133,7 +133,7 @@ public class PythonInterpreter27 extends org.python27.util.PythonInterpreter public void exec(String data, String filename) { - String[] pythonPath = JythonUtils.getScriptDirectoryPythonPath(filename); + String[] pythonPath = ch.systemsx.cisd.common.jython.JythonUtils.getScriptDirectoryPythonPath(filename); try { diff --git a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScript.java b/common/source/java/ch/systemsx/cisd/common/jython27/JythonScript.java deleted file mode 100644 index 301af3e6c3b7f37231c5f5e2e3bc0f5e79d2f1aa..0000000000000000000000000000000000000000 --- a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScript.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2012 ETH Zuerich, CISD - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.systemsx.cisd.common.jython27; - -/** - * @author pkupczyk - */ -class JythonScript -{ - - private String script; - - public JythonScript(String script) - { - this.script = script; - } - - public String[] getLines() - { - if (script == null) - { - return new String[] {}; - } else - { - return script.split("\n", -1); - } - } - -} diff --git a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptBatch.java b/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptBatch.java deleted file mode 100644 index 23c7eba4f54ff69cb6be4694e3bb8863a06e829e..0000000000000000000000000000000000000000 --- a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptBatch.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2012 ETH Zuerich, CISD - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.systemsx.cisd.common.jython27; - -/** - * @author pkupczyk - */ -class JythonScriptBatch extends JythonScriptLines -{ - -} diff --git a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptBatches.java b/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptBatches.java deleted file mode 100644 index a032ee0aea3c84b9ec7a52106e1d3d3b8bf831c0..0000000000000000000000000000000000000000 --- a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptBatches.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2012 ETH Zuerich, CISD - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.systemsx.cisd.common.jython27; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -/** - * @author pkupczyk - */ -public class JythonScriptBatches -{ - - private List<JythonScriptBatch> batches = new ArrayList<JythonScriptBatch>(); - - public void addBatch(JythonScriptBatch batch) - { - batches.add(batch); - } - - public List<String> getLines() - { - List<String> lines = new ArrayList<String>(); - Iterator<JythonScriptBatch> iterator = batches.iterator(); - - while (iterator.hasNext()) - { - JythonScriptBatch batch = iterator.next(); - String batchLines = batch.getLines(); - - if (iterator.hasNext()) - { - batchLines += "\n"; - } - - lines.add(batchLines); - } - - return lines; - } -} diff --git a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptLines.java b/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptLines.java deleted file mode 100644 index ffc0a439839de0a58921d6e8ac839671745f4fbf..0000000000000000000000000000000000000000 --- a/common/source/java/ch/systemsx/cisd/common/jython27/JythonScriptLines.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2012 ETH Zuerich, CISD - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.systemsx.cisd.common.jython27; - -/** - * @author pkupczyk - */ -public class JythonScriptLines -{ - - private StringBuilder lines = new StringBuilder(); - - private int size; - - public void addLine(String line) - { - if (size > 0) - { - lines.append("\n"); - } - lines.append(line); - size++; - } - - public void addLines(JythonScriptLines object) - { - if (size > 0) - { - lines.append("\n"); - } - lines.append(object.getLines()); - size += object.getSize(); - } - - public String getLines() - { - return lines.toString(); - } - - public int getSize() - { - return size; - } - -} diff --git a/common/source/java/ch/systemsx/cisd/common/jython27/JythonUtils.java b/common/source/java/ch/systemsx/cisd/common/jython27/JythonUtils.java deleted file mode 100644 index cbcf92540dcad89411a4abaeae5cbfaa3b2f929c..0000000000000000000000000000000000000000 --- a/common/source/java/ch/systemsx/cisd/common/jython27/JythonUtils.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright 2011 ETH Zuerich, CISD - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.systemsx.cisd.common.jython27; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.python27.core.Py; -import org.python27.core.PyDictionary; -import org.python27.core.PyFunction; -import org.python27.core.PyObject; -import org.python27.core.PySequenceList; - -import ch.systemsx.cisd.common.filesystem.FileUtilities; -import ch.systemsx.cisd.common.jython.evaluator.EvaluatorException; -import ch.systemsx.cisd.common.shared.basic.string.StringUtils; - -/** - * Jython utility methods. - * - * @author Kaloyan Enimanev - */ -public class JythonUtils -{ - - /** - * Converts a {@link PyDictionary} to a Java map. - * - * @return a map equivalent to the given Jython dictionary. - */ - public static Map<String, String> convertPyDictToMap(PyDictionary result) - { - Map<String, String> javaMap = new HashMap<String, String>(); - for (Object item : result.items()) - { - PySequenceList tuple = (PySequenceList) item; - javaMap.put(tuple.get(0).toString(), tuple.get(1).toString()); - } - return javaMap; - } - - /** - * Tries to get a function defined in jython script - * - * @return a Jython function object, or <code>null</code> if function doesn't exist. - */ - public static PyFunction tryJythonFunction(PythonInterpreter27 interpreter, String functionName) - { - try - { - PyFunction function = interpreter.get(functionName, PyFunction.class); - return function; - } catch (Exception e) - { - return null; - } - } - - /** - * Turn all arguments into a python objects, and calls the specified function. - */ - public static PyObject invokeFunction(PyFunction function, Object... args) - { - PyObject[] pyArgs = new PyObject[args.length]; - for (int i = 0; i < args.length; i++) - { - pyArgs[i] = Py.java2py(args[i]); - } - return function.__call__(pyArgs); - } - - /** - * @return script string from file with given path - * @throws EvaluatorException if the file doesn't exist or is empty - */ - public static String extractScriptFromPath(String scriptPath) throws EvaluatorException - { - File scriptFile = new File(scriptPath); - if (false == scriptFile.exists()) - { - throw new EvaluatorException("Plugin script [" + scriptPath - + "] specified in the configuration doesn't exist."); - } else - { - String scriptString = FileUtilities.loadToString(scriptFile); - if (StringUtils.isBlank(scriptString)) - { - throw new EvaluatorException("Plugin script [" + scriptPath - + "] specified in the configuration is empty."); - } else - { - try - { - return scriptString + "\n"; - } catch (EvaluatorException ex) - { - throw new EvaluatorException(ex.getMessage() + " [" + scriptPath + "]"); - } - } - } - } - - /** - * Returns a python path that contains folders the specified scripts are located in. Returns null if no scripts have been specified. - */ - public static String[] getScriptDirectoryPythonPath(String... scriptPaths) - { - if (scriptPaths == null || scriptPaths.length == 0) - { - return null; - } - - List<String> pythonPath = new ArrayList<String>(); - - for (String scriptPath : scriptPaths) - { - if (scriptPath != null) - { - File scriptFile = new File(scriptPath); - File scriptDirectory = scriptFile.getParentFile(); - - if (scriptDirectory != null) - { - pythonPath.add(scriptDirectory.getAbsolutePath()); - } - } - } - - if (pythonPath.isEmpty()) - { - return null; - } else - { - return pythonPath.toArray(new String[pythonPath.size()]); - } - } -} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/jython/v27/JythonScriptSplitterTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/jython/v27/JythonScriptSplitterTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2cefdc0a762e8804411c0b5d020e2ee884cbd29a --- /dev/null +++ b/common/sourceTest/java/ch/systemsx/cisd/common/jython/v27/JythonScriptSplitterTest.java @@ -0,0 +1,149 @@ +/* + * Copyright 2012 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.jython.v27; + +import java.io.File; +import java.util.Collections; +import java.util.List; + +import org.apache.commons.io.output.ByteArrayOutputStream; +import org.testng.Assert; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.common.filesystem.FileUtilities; + +/** + * @author pkupczyk + */ +// TODO: remove "broken" if jython 2.7 is working +@Test(groups = + { "slow", "broken" }) +public class JythonScriptSplitterTest +{ + + @Test + public void testSplittingNullScriptShouldReturnNoBatches() + { + JythonScriptSplitter splitter = new JythonScriptSplitter(); + List<String> batches = splitter.split(null); + Assert.assertNotNull(batches); + Assert.assertEquals(batches, Collections.emptyList()); + } + + @Test + public void testSplittingEmptyScriptShouldReturnOneEmptyBatch() + { + JythonScriptSplitter splitter = new JythonScriptSplitter(); + List<String> batches = splitter.split(""); + Assert.assertNotNull(batches); + Assert.assertEquals(batches.size(), 1); + Assert.assertEquals(batches.get(0), ""); + } + + @Test + public void testSplittingScriptSmallerThanBatchSizeShouldReturnOneBatch() + { + List<String> batches = testSplittingScript(1, 100); + Assert.assertEquals(batches.size(), 1); + } + + @Test + public void testSplittingScriptBiggerThanBatchSizeShouldReturnMultipleBatches() + { + List<String> batches = testSplittingScript(1, 10); + Assert.assertEquals(batches.size(), 3); + Assert.assertEquals(batches.get(0), getTestScriptCodeBatch(1)); + Assert.assertEquals(batches.get(1), getTestScriptCodeBatch(2)); + Assert.assertEquals(batches.get(2), getTestScriptCodeBatch(3)); + } + + @Test + public void testSplittingScriptBiggerThanJavaLimitShouldReturnMultipleBatches() + { + List<String> batches = testSplittingScript(200, 100); + Assert.assertTrue(batches.size() > 1); + } + + @Test + public void testSplittingScriptWithCommandsBiggerThanBatchSizeShouldReturnBatchesThatFitCommands() + { + List<String> batches = testSplittingScript(2, 5); + Assert.assertTrue(batches.size() > 1); + } + + private List<String> testSplittingScript(int scriptSize, int batchSize) + { + String originalScript = getTestScriptCode(scriptSize); + + JythonScriptSplitter splitter = new JythonScriptSplitter(); + splitter.setBatchSize(batchSize); + + List<String> batches = splitter.split(originalScript); + Assert.assertNotNull(batches); + + StringBuilder scriptFromBatches = new StringBuilder(); + for (String batch : batches) + { + scriptFromBatches.append(batch); + } + Assert.assertEquals(scriptFromBatches.toString(), originalScript); + + String originalScriptOutput = getTestScriptOutput(scriptSize); + + ByteArrayOutputStream scriptFromBatchesOutput = new ByteArrayOutputStream(); + PythonInterpreter27 interpreter = PythonInterpreter27.createNonIsolatedPythonInterpreter(); + interpreter.setOut(scriptFromBatchesOutput); + + for (String batch : batches) + { + interpreter.exec(batch); + } + + Assert.assertEquals(scriptFromBatchesOutput.toString(), originalScriptOutput); + + return batches; + } + + private String getTestScriptCode(int scriptSize) + { + return getTestFile("testScriptCode.py", scriptSize); + } + + private String getTestScriptCodeBatch(int index) + { + return getTestFile("testScriptCodeBatch" + index + ".py", 1); + } + + private String getTestScriptOutput(int scriptSize) + { + return getTestFile("testScriptOutput.txt", scriptSize); + } + + private String getTestFile(String fileName, int numberOfDuplications) + { + File file = new File("resource/test-data/" + getClass().getSimpleName() + "/" + fileName); + String content = FileUtilities.loadToString(file); + StringBuilder duplicatedContent = new StringBuilder(); + + for (int i = 0; i < numberOfDuplications; i++) + { + duplicatedContent.append(content); + } + return duplicatedContent.toString(); + } + +} diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/DataSetRegistrationServiceV2.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/DataSetRegistrationServiceV2.java index 414aa92245a11d40f1691169c91ff16bdea8ed2b..bd31093650d3352069b8c485c7b7b65a7ee63f6d 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/DataSetRegistrationServiceV2.java +++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/DataSetRegistrationServiceV2.java @@ -17,8 +17,8 @@ package ch.systemsx.cisd.etlserver.registrator.api.v2; import ch.systemsx.cisd.common.action.IDelegatedActionWithResult; -import ch.systemsx.cisd.common.jython.all.IJythonFunction; -import ch.systemsx.cisd.common.jython.all.IJythonInterpreter; +import ch.systemsx.cisd.common.jython.IJythonFunction; +import ch.systemsx.cisd.common.jython.IJythonInterpreter; import ch.systemsx.cisd.etlserver.ITopLevelDataSetRegistratorDelegate; import ch.systemsx.cisd.etlserver.registrator.DataSetFile; import ch.systemsx.cisd.etlserver.registrator.v2.AbstractProgrammableTopLevelDataSetHandler; diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonAsJavaDataSetRegistrationDropboxV2Wrapper.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonAsJavaDataSetRegistrationDropboxV2Wrapper.java index 55e9837959850b6e5d9aa0fe23c8b54fbb3ae647..206891a2f0b6ed1fa22793712fcc74daba9cf1e0 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonAsJavaDataSetRegistrationDropboxV2Wrapper.java +++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonAsJavaDataSetRegistrationDropboxV2Wrapper.java @@ -20,9 +20,9 @@ import org.apache.log4j.Logger; import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.common.exceptions.NotImplementedException; -import ch.systemsx.cisd.common.jython.all.IJythonFunction; -import ch.systemsx.cisd.common.jython.all.IJythonInterpreter; -import ch.systemsx.cisd.common.jython.all.IJythonObject; +import ch.systemsx.cisd.common.jython.IJythonFunction; +import ch.systemsx.cisd.common.jython.IJythonInterpreter; +import ch.systemsx.cisd.common.jython.IJythonObject; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.etlserver.registrator.DataSetRegistrationContext; diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonDataSetRegistrationServiceV2.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonDataSetRegistrationServiceV2.java index 94e9ef7758086be7fef49fad883b82e205b15289..d5f244eb4afaf57b33be90629bb5d77ef0b6c88a 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonDataSetRegistrationServiceV2.java +++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonDataSetRegistrationServiceV2.java @@ -19,7 +19,7 @@ package ch.systemsx.cisd.etlserver.registrator.api.v2; import org.apache.log4j.Logger; import ch.systemsx.cisd.common.action.IDelegatedActionWithResult; -import ch.systemsx.cisd.common.jython.all.IJythonInterpreter; +import ch.systemsx.cisd.common.jython.IJythonInterpreter; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.etlserver.ITopLevelDataSetRegistratorDelegate; diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonTopLevelDataSetHandlerV2.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonTopLevelDataSetHandlerV2.java index 2effd597b0860d0f7e90d7bc7a668de9c97b0a86..dd4f6ebe8ab24b5a9b105e29329fc15a0be8ac82 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonTopLevelDataSetHandlerV2.java +++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/api/v2/JythonTopLevelDataSetHandlerV2.java @@ -21,11 +21,11 @@ import java.io.File; import ch.systemsx.cisd.common.action.IDelegatedActionWithResult; import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; import ch.systemsx.cisd.common.filesystem.FileUtilities; -import ch.systemsx.cisd.common.jython.all.IJythonFunction; -import ch.systemsx.cisd.common.jython.all.IJythonInterpreter; -import ch.systemsx.cisd.common.jython.all.IJythonInterpreterFactory; -import ch.systemsx.cisd.common.jython.all.Jython25InterpreterFactory; -import ch.systemsx.cisd.common.jython.all.Jython27InterpreterFactory; +import ch.systemsx.cisd.common.jython.IJythonFunction; +import ch.systemsx.cisd.common.jython.IJythonInterpreter; +import ch.systemsx.cisd.common.jython.IJythonInterpreterFactory; +import ch.systemsx.cisd.common.jython.v25.Jython25InterpreterFactory; +import ch.systemsx.cisd.common.jython.v27.Jython27InterpreterFactory; import ch.systemsx.cisd.common.properties.PropertyUtils; import ch.systemsx.cisd.etlserver.DssRegistrationLogger; import ch.systemsx.cisd.etlserver.ITopLevelDataSetRegistratorDelegate; diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/v1/JythonTopLevelDataSetHandler.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/v1/JythonTopLevelDataSetHandler.java index 2d033bb5683d8c50682d1cb35d9cf53415fb5ce6..95d55b74a9d636e039beef4f87fd1e8a86bbff88 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/v1/JythonTopLevelDataSetHandler.java +++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/registrator/v1/JythonTopLevelDataSetHandler.java @@ -25,10 +25,10 @@ import ch.systemsx.cisd.common.action.IDelegatedActionWithResult; import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; import ch.systemsx.cisd.common.exceptions.NotImplementedException; import ch.systemsx.cisd.common.filesystem.FileUtilities; -import ch.systemsx.cisd.common.jython.all.IJythonFunction; -import ch.systemsx.cisd.common.jython.all.IJythonInterpreter; -import ch.systemsx.cisd.common.jython.all.IJythonInterpreterFactory; -import ch.systemsx.cisd.common.jython.all.Jython25InterpreterFactory; +import ch.systemsx.cisd.common.jython.IJythonFunction; +import ch.systemsx.cisd.common.jython.IJythonInterpreter; +import ch.systemsx.cisd.common.jython.IJythonInterpreterFactory; +import ch.systemsx.cisd.common.jython.v25.Jython25InterpreterFactory; import ch.systemsx.cisd.common.properties.PropertyUtils; import ch.systemsx.cisd.etlserver.ITopLevelDataSetRegistratorDelegate; import ch.systemsx.cisd.etlserver.TopLevelDataSetRegistratorGlobalState;