Skip to content
Snippets Groups Projects
Commit 8c5971b1 authored by felmer's avatar felmer
Browse files

SSDM-3745: introducing IJythonInterpreter.isNextCommand() in order to run...

SSDM-3745: introducing IJythonInterpreter.isNextCommand() in order to run master data registration for jython 2.5 or 2.7. Some JythonUtils method are inlined.

SVN: 36750
parent 8c10ce4a
No related branches found
No related tags found
No related merge requests found
Showing
with 84 additions and 116 deletions
...@@ -29,4 +29,6 @@ public interface IJythonInterpreter ...@@ -29,4 +29,6 @@ public interface IJythonInterpreter
void releaseResources(); void releaseResources();
IJythonFunction tryJythonFunction(String name); IJythonFunction tryJythonFunction(String name);
boolean isNextCommand(String lines);
} }
\ No newline at end of file
/*
* 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;
import org.python.core.CompileMode;
import org.python.core.CompilerFlags;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyObject;
/**
* @author pkupczyk
*/
public class JythonScriptCommand extends JythonScriptLines
{
public boolean isNextCommand(String line)
{
try
{
PyObject object =
Py.compile_command_flags(getLines(), "<input>", CompileMode.single,
new CompilerFlags(), true);
return object != Py.None;
} catch (PyException e)
{
return false;
}
}
}
...@@ -18,8 +18,6 @@ package ch.systemsx.cisd.common.jython; ...@@ -18,8 +18,6 @@ package ch.systemsx.cisd.common.jython;
import java.util.List; import java.util.List;
import org.python.core.PyException;
/** /**
* Splits jython code into smaller batches to overcome 64KB script size limitation. * Splits jython code into smaller batches to overcome 64KB script size limitation.
* *
...@@ -32,16 +30,23 @@ public class JythonScriptSplitter ...@@ -32,16 +30,23 @@ public class JythonScriptSplitter
private int batchSize = DEFAULT_BATCH_SIZE; private int batchSize = DEFAULT_BATCH_SIZE;
public List<String> split(String scriptToSplit) throws PyException private IJythonInterpreter interpreter;
public JythonScriptSplitter(IJythonInterpreter interpreter)
{
this.interpreter = interpreter;
}
public List<String> split(String scriptToSplit)
{ {
JythonScript script = new JythonScript(scriptToSplit); JythonScript script = new JythonScript(scriptToSplit);
JythonScriptBatches batches = new JythonScriptBatches(); JythonScriptBatches batches = new JythonScriptBatches();
JythonScriptBatch batch = new JythonScriptBatch(); JythonScriptBatch batch = new JythonScriptBatch();
JythonScriptCommand command = new JythonScriptCommand(); JythonScriptLines command = new JythonScriptLines();
for (String line : script.getLines()) for (String line : script.getLines())
{ {
if (command.getSize() > 0 && command.isNextCommand(line)) if (command.getSize() > 0 && interpreter.isNextCommand(command.getLines()))
{ {
if (batch.getSize() > 0 && batch.getSize() + command.getSize() > getBatchSize()) if (batch.getSize() > 0 && batch.getSize() + command.getSize() > getBatchSize())
{ {
...@@ -49,7 +54,7 @@ public class JythonScriptSplitter ...@@ -49,7 +54,7 @@ public class JythonScriptSplitter
batch = new JythonScriptBatch(); batch = new JythonScriptBatch();
} }
batch.addLines(command); batch.addLines(command);
command = new JythonScriptCommand(); command = new JythonScriptLines();
command.addLine(line); command.addLine(line);
} else } else
{ {
......
...@@ -18,15 +18,7 @@ package ch.systemsx.cisd.common.jython; ...@@ -18,15 +18,7 @@ package ch.systemsx.cisd.common.jython;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.python.core.Py;
import org.python.core.PyDictionary;
import org.python.core.PyFunction;
import org.python.core.PyObject;
import org.python.core.PySequenceList;
import ch.systemsx.cisd.common.filesystem.FileUtilities; import ch.systemsx.cisd.common.filesystem.FileUtilities;
import ch.systemsx.cisd.common.jython.evaluator.EvaluatorException; import ch.systemsx.cisd.common.jython.evaluator.EvaluatorException;
...@@ -39,53 +31,6 @@ import ch.systemsx.cisd.common.shared.basic.string.StringUtils; ...@@ -39,53 +31,6 @@ import ch.systemsx.cisd.common.shared.basic.string.StringUtils;
*/ */
public class JythonUtils 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(PythonInterpreter 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 * @return script string from file with given path
* @throws EvaluatorException if the file doesn't exist or is empty * @throws EvaluatorException if the file doesn't exist or is empty
......
...@@ -16,7 +16,11 @@ ...@@ -16,7 +16,11 @@
package ch.systemsx.cisd.common.jython.v25; package ch.systemsx.cisd.common.jython.v25;
import org.python.core.CompileMode;
import org.python.core.CompilerFlags;
import org.python.core.Py;
import org.python.core.PyBaseCode; import org.python.core.PyBaseCode;
import org.python.core.PyException;
import org.python.core.PyFunction; import org.python.core.PyFunction;
import org.python.core.PyInteger; import org.python.core.PyInteger;
import org.python.core.PyObject; import org.python.core.PyObject;
...@@ -25,7 +29,6 @@ import ch.systemsx.cisd.common.jython.IJythonFunction; ...@@ -25,7 +29,6 @@ import ch.systemsx.cisd.common.jython.IJythonFunction;
import ch.systemsx.cisd.common.jython.IJythonInterpreter; import ch.systemsx.cisd.common.jython.IJythonInterpreter;
import ch.systemsx.cisd.common.jython.IJythonInterpreterFactory; import ch.systemsx.cisd.common.jython.IJythonInterpreterFactory;
import ch.systemsx.cisd.common.jython.IJythonObject; import ch.systemsx.cisd.common.jython.IJythonObject;
import ch.systemsx.cisd.common.jython.JythonUtils;
import ch.systemsx.cisd.common.jython.PythonInterpreter; import ch.systemsx.cisd.common.jython.PythonInterpreter;
public class Jython25InterpreterFactory implements IJythonInterpreterFactory public class Jython25InterpreterFactory implements IJythonInterpreterFactory
...@@ -48,7 +51,12 @@ public class Jython25InterpreterFactory implements IJythonInterpreterFactory ...@@ -48,7 +51,12 @@ public class Jython25InterpreterFactory implements IJythonInterpreterFactory
@Override @Override
public IJythonObject invoke(Object... arguments) public IJythonObject invoke(Object... arguments)
{ {
PyObject result = JythonUtils.invokeFunction(function, arguments); PyObject[] pyArgs = new PyObject[arguments.length];
for (int i = 0; i < arguments.length; i++)
{
pyArgs[i] = Py.java2py(arguments[i]);
}
PyObject result = function.__call__(pyArgs);
if (result == null) if (result == null)
{ {
return null; return null;
...@@ -142,15 +150,30 @@ public class Jython25InterpreterFactory implements IJythonInterpreterFactory ...@@ -142,15 +150,30 @@ public class Jython25InterpreterFactory implements IJythonInterpreterFactory
@Override @Override
public IJythonFunction tryJythonFunction(String name) public IJythonFunction tryJythonFunction(String name)
{ {
PyFunction function = JythonUtils.tryJythonFunction(interpreter, name); try
if (function == null) {
PyFunction function = interpreter.get(name, PyFunction.class);
return new Jython25Function(function);
} catch (Exception e)
{ {
return null; return null;
} }
else }
@Override
public boolean isNextCommand(String lines)
{
try
{ {
return new Jython25Function(function); PyObject object =
Py.compile_command_flags(lines, "<input>", CompileMode.single,
new CompilerFlags(), true);
return object != Py.None;
} catch (PyException e)
{
return false;
} }
} }
} }
} }
...@@ -16,7 +16,11 @@ ...@@ -16,7 +16,11 @@
package ch.systemsx.cisd.common.jython.v27; package ch.systemsx.cisd.common.jython.v27;
import org.python27.core.CompileMode;
import org.python27.core.CompilerFlags;
import org.python27.core.Py;
import org.python27.core.PyBaseCode; import org.python27.core.PyBaseCode;
import org.python27.core.PyException;
import org.python27.core.PyFunction; import org.python27.core.PyFunction;
import org.python27.core.PyInteger; import org.python27.core.PyInteger;
import org.python27.core.PyObject; import org.python27.core.PyObject;
...@@ -153,6 +157,21 @@ public class Jython27InterpreterFactory implements IJythonInterpreterFactory ...@@ -153,6 +157,21 @@ public class Jython27InterpreterFactory implements IJythonInterpreterFactory
return new Jython27Function(function); return new Jython27Function(function);
} }
} }
@Override
public boolean isNextCommand(String lines)
{
try
{
PyObject object =
Py.compile_command_flags(lines, "<input>", CompileMode.single,
new CompilerFlags(), true);
return object != Py.None;
} catch (PyException e)
{
return false;
}
}
} }
} }
\ No newline at end of file
...@@ -25,6 +25,7 @@ import org.testng.Assert; ...@@ -25,6 +25,7 @@ import org.testng.Assert;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import ch.systemsx.cisd.common.filesystem.FileUtilities; import ch.systemsx.cisd.common.filesystem.FileUtilities;
import ch.systemsx.cisd.common.jython.v27.Jython27InterpreterFactory;
/** /**
* @author pkupczyk * @author pkupczyk
...@@ -38,7 +39,7 @@ public class JythonScriptSplitterTest ...@@ -38,7 +39,7 @@ public class JythonScriptSplitterTest
@Test @Test
public void testSplittingNullScriptShouldReturnNoBatches() public void testSplittingNullScriptShouldReturnNoBatches()
{ {
JythonScriptSplitter splitter = new JythonScriptSplitter(); JythonScriptSplitter splitter = new JythonScriptSplitter(new Jython27InterpreterFactory().createInterpreter());
List<String> batches = splitter.split(null); List<String> batches = splitter.split(null);
Assert.assertNotNull(batches); Assert.assertNotNull(batches);
Assert.assertEquals(batches, Collections.emptyList()); Assert.assertEquals(batches, Collections.emptyList());
...@@ -47,7 +48,7 @@ public class JythonScriptSplitterTest ...@@ -47,7 +48,7 @@ public class JythonScriptSplitterTest
@Test @Test
public void testSplittingEmptyScriptShouldReturnOneEmptyBatch() public void testSplittingEmptyScriptShouldReturnOneEmptyBatch()
{ {
JythonScriptSplitter splitter = new JythonScriptSplitter(); JythonScriptSplitter splitter = new JythonScriptSplitter(new Jython27InterpreterFactory().createInterpreter());
List<String> batches = splitter.split(""); List<String> batches = splitter.split("");
Assert.assertNotNull(batches); Assert.assertNotNull(batches);
Assert.assertEquals(batches.size(), 1); Assert.assertEquals(batches.size(), 1);
...@@ -89,7 +90,7 @@ public class JythonScriptSplitterTest ...@@ -89,7 +90,7 @@ public class JythonScriptSplitterTest
{ {
String originalScript = getTestScriptCode(scriptSize); String originalScript = getTestScriptCode(scriptSize);
JythonScriptSplitter splitter = new JythonScriptSplitter(); JythonScriptSplitter splitter = new JythonScriptSplitter(new Jython27InterpreterFactory().createInterpreter());
splitter.setBatchSize(batchSize); splitter.setBatchSize(batchSize);
List<String> batches = splitter.split(originalScript); List<String> batches = splitter.split(originalScript);
......
...@@ -73,6 +73,12 @@ public class DataSetRegistrationServiceV2<T extends DataSetInformation> extends ...@@ -73,6 +73,12 @@ public class DataSetRegistrationServiceV2<T extends DataSetInformation> extends
public void addToPath(String... pythonPaths) public void addToPath(String... pythonPaths)
{ {
} }
@Override
public boolean isNextCommand(String line)
{
return false;
}
}, null); }, null);
} }
......
...@@ -29,6 +29,7 @@ import org.python.core.Py; ...@@ -29,6 +29,7 @@ import org.python.core.Py;
import org.python.core.PyDictionary; import org.python.core.PyDictionary;
import org.python.core.PyFunction; import org.python.core.PyFunction;
import org.python.core.PyObject; import org.python.core.PyObject;
import org.python.core.PySequenceList;
import ch.systemsx.cisd.common.jython.JythonUtils; import ch.systemsx.cisd.common.jython.JythonUtils;
import ch.systemsx.cisd.common.jython.PythonInterpreter; import ch.systemsx.cisd.common.jython.PythonInterpreter;
...@@ -161,7 +162,7 @@ public class ValidationScriptRunner ...@@ -161,7 +162,7 @@ public class ValidationScriptRunner
Map<String, String> javaResult = null; Map<String, String> javaResult = null;
if (result instanceof PyDictionary) if (result instanceof PyDictionary)
{ {
javaResult = JythonUtils.convertPyDictToMap((PyDictionary) result); javaResult = convertPyDictToMap((PyDictionary) result);
} else } else
{ {
javaResult = (Map<String, String>) result; javaResult = (Map<String, String>) result;
...@@ -171,6 +172,17 @@ public class ValidationScriptRunner ...@@ -171,6 +172,17 @@ public class ValidationScriptRunner
return metadata; return metadata;
} }
private 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;
}
public String getScriptString() public String getScriptString()
{ {
......
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