Java Examples for org.python.util.PythonInterpreter
The following java examples will help you to understand the usage of org.python.util.PythonInterpreter. These source code samples are taken from different open source projects.
Example 1
| Project: jiac-examples-master File: SimpleEmbedded.java View source code |
public static void main(String[] args) throws PyException {
PythonInterpreter interp = new PythonInterpreter();
System.out.println("Hello, brave new world");
interp.exec("import sys");
interp.exec("print sys");
interp.set("a", new PyInteger(42));
interp.exec("print a");
interp.exec("x = 2+2");
PyObject x = interp.get("x");
System.out.println("x: " + x);
System.out.println("Goodbye, cruel world");
}Example 2
| Project: ringmud-master File: Compiler.java View source code |
private void convertPython() {
try {
PythonInterpreter interp = Interpreter.INSTANCE.getInterpreter();
//Provide the DocumentInfo class to all python-defined data.
InputStream documentInfoStream = Interpreter.INSTANCE.getInternalScript("compiler.py");
interp.execfile(documentInfoStream);
List<FileEntry> entriesToRemove = new ArrayList<FileEntry>();
List<FileEntry> entriesToAdd = new ArrayList<FileEntry>();
for (FileEntry entry : mudFile.getEntries("data")) {
if (entry.getEntryName().endsWith(".py")) {
//Add document info object to the interpreter.
interp.exec("__document__ = DocumentInfo()");
//Execute script
interp.execfile(new FileInputStream(entry.getFile()));
//Then convert to XML
List<Persistable> persistables = XMLConverter.getPersistables();
//More efficient to have one string, rather than create it
//in each iteration below. This will be the file name of the python file,
//minus the .py extension.
String fileRoot = entry.getEntryName();
fileRoot = fileRoot.substring(fileRoot.lastIndexOf("/"), fileRoot.length());
File tmp = File.createTempFile(fileRoot, ".xml");
FileOutputStream fileOut = new FileOutputStream(tmp);
BufferedOutputStream buffer = new BufferedOutputStream(fileOut);
PrintStream out = new PrintStream(buffer);
//Retrieve codebehind attribute from document info, if it is present.
PyObject documentInfo = interp.get("__document__");
String codebehind = (String) documentInfo.__findattr__("codebehind").__tojava__(String.class);
//Write beginning of XML document.
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
if (codebehind != null) {
out.print("<ring codebehind=\"" + codebehind + "\">");
} else {
out.println("<ring>");
}
//Write out each persistable to the XML document.
for (Persistable persistable : persistables) {
String xml = persistable.toXML();
out.println(xml);
}
//Write end of XML document.
out.println("</ring>");
XMLConverter.clear();
out.close();
buffer.close();
fileOut.close();
FileEntry fe = new FileEntry();
fe.setFile(tmp);
//Prefix should start with a /
fe.setEntryName("data" + fileRoot + ".xml");
entriesToAdd.add(fe);
entriesToRemove.add(entry);
//Cleanup!
interp.cleanup();
}
}
//Modify the mudfile to remove the python data files and replace them with the
//generated xml files.
mudFile.getEntries().removeAll(entriesToRemove);
mudFile.getEntries().addAll(entriesToAdd);
} catch (IOException e) {
e.printStackTrace();
}
}Example 3
| Project: netention-old1-master File: RunTwitterBotPython.java View source code |
// http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html
public static void main(String[] args) throws Exception {
System.out.println("Free memory: " + Runtime.getRuntime().freeMemory());
System.out.println("Total memory: " + Runtime.getRuntime().totalMemory());
System.out.println("Max memory: " + Runtime.getRuntime().maxMemory());
for (final String scriptFile : args) {
new Thread(new Runnable() {
@Override
public void run() {
final PythonInterpreter interp = new PythonInterpreter();
System.out.println("Running " + scriptFile);
interp.execfile(scriptFile);
}
}).start();
}
}Example 4
| Project: SpringBlog-master File: PygmentsService.java View source code |
@Override
public String highlight(String content) {
PythonInterpreter interpreter = new PythonInterpreter();
// Set a variable with the content you want to work with
interpreter.set("code", content);
// Simple use Pygments as you would in Python
interpreter.exec("from pygments import highlight\n" + "from pygments.lexers import PythonLexer\n" + "from pygments.formatters import HtmlFormatter\n" + "\nresult = highlight(code, PythonLexer(), HtmlFormatter())");
return interpreter.get("result", String.class);
}Example 5
| Project: ToriTools-master File: JythonFactory.java View source code |
@SuppressWarnings("rawtypes")
public static Object getJythonObject(String interfaceName, String pathToJythonModule) throws ClassNotFoundException {
Object javaInt = null;
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile(pathToJythonModule);
String tempName = pathToJythonModule.substring(pathToJythonModule.lastIndexOf("/") + 1);
tempName = tempName.substring(0, tempName.indexOf("."));
String instanceName = tempName.toLowerCase();
String javaClassName = tempName.substring(0, 1).toUpperCase() + tempName.substring(1);
String objectDef = "=" + javaClassName + "()";
interpreter.exec(instanceName + objectDef);
Class JavaInterface = Class.forName(interfaceName);
javaInt = interpreter.get(instanceName).__tojava__(JavaInterface);
return javaInt;
}Example 6
| Project: FreeVoteBot-master File: ScriptModuleLoader.java View source code |
/**
* Loads a module from a Python file that contains a class with the <b>SAME</b> name as the file.
* The class must extend {@link ExternalModule}.
*
* @param f the file to load the {@link org.freecode.irc.votebot.api.ExternalModule} from
* @return the {@link ExternalModule} loaded, or <tt>null</tt>
* @throws IOException if the file was not found
* @throws ScriptException if the script failed to load
*/
public ExternalModule loadFromFile(final File f) throws IOException, ScriptException {
if (f == null || !f.exists()) {
throw new IOException("Invalid file");
}
if (f.getName().endsWith(".py")) {
PythonInterpreter interpreter = new PythonInterpreter();
String clzName = f.getName().replace(".py", "");
interpreter.exec(String.format("from %s import %s", clzName, clzName));
PyObject pyClass = interpreter.get(clzName);
PyObject buildObject = pyClass.__call__();
ExternalModule ext = (ExternalModule) buildObject.__tojava__(ExternalModule.class);
ext.setFvb(fvb);
ext.setExtName(clzName);
interpreter.cleanup();
return ext;
}
return null;
}Example 7
| Project: codehaus-mojo-master File: Main.java View source code |
public static void main(String[] args) throws IOException {
ScriptRecorder rec = new PythonScriptRecorder();
args = rec.processForLearningMode(args);
String filePath = args[0];
PythonInterpreter interp = new PythonInterpreter();
StreamConnection conn = new DefaultStreamConnection();
// deprecated
interp.set("ssh", conn);
interp.set("conn", conn);
interp.set("args", args);
interp.execfile(filePath);
}Example 8
| Project: dawn-workflow-master File: PythonScript.java View source code |
private DataMessageComponent getJythonMessage(final List<DataMessageComponent> cache) throws Exception {
PythonInterpreter interpreter;
if (// (Takes a while)
isNewInterpreter) {
interpreter = JythonInterpreterUtils.getInterpreter();
} else {
interpreter = ActorInterpreterUtils.getInterpreterForRun(this);
}
// We save the names of the abstract data sets passed in
// these are read back and passed on sometimes.
List<String> inputs = new ArrayList<String>(7);
for (DataMessageComponent dataMessageComponent : cache) {
if (dataMessageComponent.getList() == null)
continue;
for (String name : dataMessageComponent.getList().keySet()) {
final Object ob = dataMessageComponent.getList().get(name);
interpreter.set(name, ob);
if (ob instanceof Dataset) {
interpreter.exec(name + " = dnp.Sciwrap(" + name + ")");
if (isPassInputs) {
inputs.add(name);
}
}
}
}
final IResource file = getResource();
if (file.exists())
interpreter.execfile(new FileInputStream(file.getLocation().toOSString()));
final DataMessageComponent ret = new DataMessageComponent();
final Map<String, Serializable> data = new HashMap<String, Serializable>(7);
if (outputs == null)
outputs = Collections.emptyList();
final List<String> out = new ArrayList<String>(outputs.size() + inputs.size());
out.addAll(outputs);
out.addAll(inputs);
for (String name : out) {
PyObject ob = interpreter.get(name);
Dataset ds = (Dataset) ob.__tojava__(Dataset.class);
if (ds instanceof PyProxy)
ds = ds.getView(true);
ds.setName(name);
data.put(name, ds);
}
ret.setList(data);
ret.setMeta(MessageUtils.getMeta(cache));
ret.addScalar(MessageUtils.getScalar(cache));
ret.putScalar("Jython Script", getResource().getName());
return ret;
}Example 9
| Project: floodlight-qos-beta-master File: JythonServer.java View source code |
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
p.exec("run_server(" + this.port + ", '0.0.0.0', locals())");
}Example 10
| Project: geni-openflow-vertical-handover-master File: JythonServer.java View source code |
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
p.exec("run_server(" + this.port + ", '0.0.0.0', locals())");
}Example 11
| Project: geoserver-old-master File: PythonAppResource.java View source code |
@Override
public void handleGet() {
PythonInterpreter pi = python.interpreter();
pi.execfile(appFile.getAbsolutePath());
PyObject app = pi.get("app");
if (app == null) {
throw new RestletException("'app' function not found", Status.SERVER_ERROR_INTERNAL);
}
if (!(app instanceof PyFunction)) {
throw new RestletException("'app' must be a function", Status.SERVER_ERROR_INTERNAL);
}
PyFunction appf = (PyFunction) app;
PyFunction start_response = createStartResponse();
WSGIResponse wr = new WSGIResponse();
response.set(wr);
PyObject ret = appf.__call__(new PyObject[] { createEnviron(getRequest()), start_response });
if (ret != null) {
String contentType = wr.headers.get("Content-type");
if (contentType == null) {
contentType = "text/plain";
}
MediaType mediaType = new MediaType(contentType);
if (ret instanceof PyString) {
getResponse().setEntity(ret.toString(), mediaType);
} else if (ret instanceof PyList) {
final PyList list = (PyList) ret;
getResponse().setEntity(new OutputRepresentation(mediaType) {
@Override
public void write(OutputStream outputStream) throws IOException {
for (Iterator i = list.iterator(); i.hasNext(); ) {
outputStream.write(i.next().toString().getBytes());
outputStream.write('\n');
}
}
});
} else if (ret instanceof PyIterator) {
final PyIterator iter = (PyIterator) ret;
getResponse().setEntity(new OutputRepresentation(mediaType) {
@Override
public void write(OutputStream outputStream) throws IOException {
for (Iterator i = iter.iterator(); i.hasNext(); ) {
outputStream.write(i.next().toString().getBytes());
outputStream.write('\n');
}
}
});
} else {
LOGGER.warning("Unsure how to handle " + ret + ". Resorting to outputing string " + "representation.");
getResponse().setEntity(ret.toString(), mediaType);
}
}
response.remove();
}Example 12
| Project: geoserver_trunk-master File: PythonAppResource.java View source code |
@Override
public void handleGet() {
PythonInterpreter pi = python.interpreter();
pi.execfile(appFile.getAbsolutePath());
PyObject app = pi.get("app");
if (app == null) {
throw new RestletException("'app' function not found", Status.SERVER_ERROR_INTERNAL);
}
if (!(app instanceof PyFunction)) {
throw new RestletException("'app' must be a function", Status.SERVER_ERROR_INTERNAL);
}
PyFunction appf = (PyFunction) app;
PyFunction start_response = createStartResponse();
WSGIResponse wr = new WSGIResponse();
response.set(wr);
PyObject ret = appf.__call__(new PyObject[] { createEnviron(getRequest()), start_response });
if (ret != null) {
String contentType = wr.headers.get("Content-type");
if (contentType == null) {
contentType = "text/plain";
}
MediaType mediaType = new MediaType(contentType);
if (ret instanceof PyString) {
getResponse().setEntity(ret.toString(), mediaType);
} else if (ret instanceof PyList) {
final PyList list = (PyList) ret;
getResponse().setEntity(new OutputRepresentation(mediaType) {
@Override
public void write(OutputStream outputStream) throws IOException {
for (Iterator i = list.iterator(); i.hasNext(); ) {
outputStream.write(i.next().toString().getBytes());
outputStream.write('\n');
}
}
});
} else if (ret instanceof PyIterator) {
final PyIterator iter = (PyIterator) ret;
getResponse().setEntity(new OutputRepresentation(mediaType) {
@Override
public void write(OutputStream outputStream) throws IOException {
for (Iterator i = iter.iterator(); i.hasNext(); ) {
outputStream.write(i.next().toString().getBytes());
outputStream.write('\n');
}
}
});
} else {
LOGGER.warning("Unsure how to handle " + ret + ". Resorting to outputing string " + "representation.");
getResponse().setEntity(ret.toString(), mediaType);
}
}
response.remove();
}Example 13
| Project: HederaInFloodlight-master File: JythonServer.java View source code |
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
p.exec("run_server(" + this.port + ", '0.0.0.0', locals())");
}Example 14
| Project: hudson_plugins-master File: Jython.java View source code |
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
PySystemState sys = new PySystemState();
sys.setCurrentWorkingDir(build.getWorkspace().getRemote());
PythonInterpreter interp = new PythonInterpreter(null, sys);
interp.setOut(listener.getLogger());
interp.setErr(listener.getLogger());
interp.exec(this.getCommand());
interp.cleanup();
build.setResult(Result.SUCCESS);
return true;
}Example 15
| Project: ubuntu-packaging-floodlight-master File: JythonServer.java View source code |
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
p.exec("run_server(" + this.port + ", '0.0.0.0', locals())");
}Example 16
| Project: airavata-master File: JythonOneTimeRunnerImpl.java View source code |
public Void run() {
ClassLoader loader = this.getClass().getClassLoader();
PySystemState.initialize(System.getProperties(), null, arguments, loader);
if (loader instanceof JythonClassLoader) {
logger.debug("jythonClassLoader");
JythonClassLoader jythonLoader = (JythonClassLoader) loader;
JarFile xbayaJarFile = jythonLoader.getXBayaJarFile();
if (xbayaJarFile != null) {
String jarPath = xbayaJarFile.getName();
logger.debug("jarPath: " + jarPath);
// String jarDir = jarPath.substring(0,
// jarPath.lastIndexOf());
File jarFile = new File(jarPath);
String jarDir = jarFile.getParent();
logger.debug("jarDir: " + jarDir);
// This is for the Jython interpreter to
// solve import statements.
PySystemState.add_extdir(jarDir);
}
}
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec(script);
return null;
}Example 17
| Project: archived-net-virt-platform-master File: JythonServer.java View source code |
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
p.exec("run_server(" + this.port + ", '0.0.0.0', locals())");
}Example 18
| Project: development_apps_spareparts-master File: ScriptRunner.java View source code |
/** Initialize the python interpreter. */
private static void initPython() {
Properties props = new Properties();
// Default is 'message' which displays sys-package-mgr bloat
// Choose one of error,warning,message,comment,debug
props.setProperty("python.verbose", "error");
props.setProperty("python.path", System.getProperty("java.class.path"));
PythonInterpreter.initialize(System.getProperties(), props, new String[] { "" });
}Example 19
| Project: FL_HAND-master File: JythonServer.java View source code |
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
p.exec("run_server(" + this.port + ", '0.0.0.0', locals())");
}Example 20
| Project: gutenberg-master File: PygmentsTest.java View source code |
@Test
public void raw_usecase() {
PythonInterpreter interpreter = new PyGateway().getInterpreter();
// Set a variable with the content you want to work with
interpreter.set("code", "" + "(defn year-end-evaluation\n" + " []\n" + " (if (> (rand) 0.5)\n" + " \"You get a raise!\"\n" + " \"Better luck next year!\"))");
// Simple use Pygments as you would in Python
interpreter.exec("from pygments import highlight\n" + "from pygments.lexers.jvm import ClojureLexer\n" + "from pygments.formatters import RawTokenFormatter\n" + "\n" + "result = highlight(code, ClojureLexer(), RawTokenFormatter())");
// Get the result that has been set in a variable
PyObject result = interpreter.get("result");
PyString string = (PyString) result;
assertThat(string.getString()).isEqualTo("" + "Token.Punctuation\tu'('\n" + "Token.Keyword.Declaration\tu'defn '\n" + "Token.Name.Variable\tu'year-end-evaluation'\n" + "Token.Text\tu'\\n '\n" + "Token.Punctuation\tu'['\n" + "Token.Punctuation\tu']'\n" + "Token.Text\tu'\\n '\n" + "Token.Punctuation\tu'('\n" + "Token.Keyword\tu'if '\n" + "Token.Punctuation\tu'('\n" + "Token.Name.Builtin\tu'> '\n" + "Token.Punctuation\tu'('\n" + "Token.Name.Function\tu'rand'\n" + "Token.Punctuation\tu')'\n" + "Token.Text\tu' '\n" + "Token.Literal.Number.Float\tu'0.5'\n" + "Token.Punctuation\tu')'\n" + "Token.Text\tu'\\n '\n" + "Token.Literal.String\tu'\"You get a raise!\"'\n" + "Token.Text\tu'\\n '\n" + "Token.Literal.String\tu'\"Better luck next year!\"'\n" + "Token.Punctuation\tu')'\n" + "Token.Punctuation\tu')'\n" + "Token.Text\tu'\\n'\n");
}Example 21
| Project: jsystem-master File: JythonScriptExecutor.java View source code |
@Override
public void run() {
PythonInterpreter interp = new JythonTestInterpreter();
PyObject result = null;
try {
// get the test
interp.exec(String.format("test = getTestByTag(r'%s', r'%s')", getJythonFile().getAbsolutePath(), tag));
// set up parameters
for (Parameter param : parameters.values()) {
if (!param.getName().contains(".")) {
String cmd = String.format("test.%s = '%s'", param.getName(), param.getValue().toString().replace("\\", "\\\\"));
interp.exec(cmd);
}
}
// run it
interp.exec("result = runTest(test)");
// get the result
result = interp.get("result");
} catch (Throwable e) {
exception = e;
}
int wasSuccesful = ((PyInteger) result.__getattr__("wasSuccessful").__call__()).getValue();
if (exception == null && wasSuccesful != 1) {
try {
interp.exec("errors = result.getErrors()");
String errors = interp.get("errors").toString();
exception = new JythonFailedException("Jython test failed!", errors);
} catch (Throwable e) {
exception = e;
}
}
}Example 22
| Project: misc_hookons_avec_javasnoop-master File: ScriptingView.java View source code |
@Action
public void executeScript() {
try {
String lang = (String) lstLanguage.getSelectedItem();
String code = txtScript.getText();
if ("Jython".equals(lang)) {
StringWriter swOut = new StringWriter();
StringWriter swErr = new StringWriter();
if (jython == null || !chkRememberState.isSelected())
jython = new org.python.util.PythonInterpreter();
((org.python.util.PythonInterpreter) jython).setOut(swOut);
((org.python.util.PythonInterpreter) jython).setErr(swErr);
try {
((org.python.util.PythonInterpreter) jython).exec(code);
swOut.flush();
swErr.flush();
String out = swOut.toString();
String err = swErr.toString();
showOut(out);
showErr(err);
} catch (Exception ex) {
AgentLogger.error("Error evaluating expression: " + ex.getMessage());
showErr(StringUtil.exception2string(ex));
}
} else if ("BeanShell".equals(lang)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream psOut = new PrintStream(baos);
if (bsh == null || !chkRememberState.isSelected())
bsh = new bsh.Interpreter(new StringReader(code), psOut, null, false);
try {
((bsh.Interpreter) bsh).eval(code);
} catch (Exception ex) {
AgentLogger.error("Error evaluating expression: " + ex.getMessage());
showErr(StringUtil.exception2string(ex));
ex.printStackTrace();
} finally {
psOut.flush();
String out = baos.toString();
showOut(out);
}
}
showOut("");
showErr("");
showPrompt();
} catch (Exception ex) {
UIUtil.showErrorMessage(this, "Problem with script execution: " + ex.getMessage());
AgentLogger.error(ex);
}
}Example 23
| Project: scisoft-core-master File: JythonInterpreterUtils.java View source code |
/** * Create Jython interpreter with extra java libraries * * @param extraPaths set of paths to extra libraries to load * @return a new Jython Interpreter * @throws Exception from getJythonInterpreterDirectory in case of missing JYTHON_BUNDLE_LOC when no Jython bundle found */ public static PythonInterpreter getBasicInterpreter(Set<String> extraPaths, ClassLoader classLoader, boolean isRunningInEclipse) throws Exception { final long start = System.currentTimeMillis(); Properties preProperties = System.getProperties(); Properties postProperties = new Properties(); //Set some useful parameters for the jython environment File jyRoot = JythonPath.getInterpreterDirectory(isRunningInEclipse); postProperties.setProperty("python.home", jyRoot.getAbsolutePath()); postProperties.setProperty("python.executable", new File(jyRoot, JythonPath.getJythonExecutableName()).getAbsolutePath()); //Set the cache for java classes loaded by Jython String cacheDir = System.getProperty("python.cachedir"); if (cacheDir == null) { cacheDir = "/scratch/.jython_consumer_cachedir"; postProperties.setProperty("python.cachedir", cacheDir); } File cacheDirPath = new File(cacheDir); if (!cacheDirPath.exists()) { try { logger.debug("Creating jython cachedir", cacheDir); cacheDirPath.mkdirs(); } catch (Exception e) { logger.warn("Could not create python.cachedir. Resetting to cachedir"); postProperties.setProperty("python.cachedir", "cachedir"); } } //Set up the path environmental variable & send it to properties StringBuilder allPaths = new StringBuilder(); allPaths.append(new File(jyRoot, "jython.jar").getAbsolutePath() + File.pathSeparatorChar); File jyLib = new File(jyRoot, "Lib"); allPaths.append(jyLib.getAbsolutePath() + File.pathSeparatorChar); allPaths.append(new File(jyLib, "distutils").getAbsolutePath() + File.pathSeparatorChar); allPaths.append(new File(jyLib, "site-packages").getAbsolutePath() + File.pathSeparatorChar); //If there's anything else to add to the path, add it. if (extraPaths != null) { for (String path : extraPaths) { allPaths.append(path); allPaths.append(File.pathSeparatorChar); } } String pythonPath = allPaths.toString(); postProperties.setProperty("python.path", pythonPath); logger.debug("Starting new Jython Interpreter."); PythonInterpreter.initialize(preProperties, postProperties, null); //Create object to give access to python system PySystemState state = new PySystemState(); // prevent problem in warnings.py where it expects len(sys.argv) > 0 state.argv = new PyList(); state.argv.add(new PyString(postProperties.getProperty("python.executable"))); //This adds an external classloader & reports classpath if (classLoader != null) state.setClassLoader(classLoader); logger.info("Class loader is {}", classLoader); if (classLoader instanceof URLClassLoader) { logger.debug("URL classpath:"); for (URL u : ((URLClassLoader) classLoader).getURLs()) { logger.debug("\t{}", u.getPath()); } } logger.debug("Classpath:"); for (String p : System.getProperty("java.class.path").split(File.pathSeparator)) { logger.debug("\t{}", p); } //All set? Create the interpreter! PythonInterpreter interpreter = new PythonInterpreter(new PyStringMap(), state); final long end = System.currentTimeMillis(); logger.debug("Created new Jython Interpreter in {}ms.", end - start); return interpreter; }
Example 24
| Project: SikuliX-IDE-master File: EditorPane.java View source code |
private void cleanBundle(String bundle) {
if (!PreferencesUser.getInstance().getAtSaveCleanBundle()) {
return;
}
//TODO implement in Java
PythonInterpreter py = SikuliScriptRunner.getPythonInterpreter();
Debug.log(2, "Clear source bundle " + bundle);
py.set("bundle_path", bundle);
py.exec(pyBundleCleaner);
}Example 25
| Project: spring-security-master File: PythonInterpreterPreInvocationAdvice.java View source code |
public boolean before(Authentication authentication, MethodInvocation mi, PreInvocationAttribute preAttr) {
PythonInterpreterPreInvocationAttribute pythonAttr = (PythonInterpreterPreInvocationAttribute) preAttr;
String script = pythonAttr.getScript();
PythonInterpreter python = new PythonInterpreter();
python.set("authentication", authentication);
python.set("args", createArgumentMap(mi));
python.set("method", mi.getMethod().getName());
Resource scriptResource = new PathMatchingResourcePatternResolver().getResource(script);
try {
python.execfile(scriptResource.getInputStream());
} catch (IOException e) {
throw new IllegalArgumentException("Couldn't run python script, " + script, e);
}
PyObject allowed = python.get("allow");
if (allowed == null) {
throw new IllegalStateException("Python script did not set the permit flag");
}
return (Boolean) Py.tojava(allowed, Boolean.class);
}Example 26
| Project: zk-master File: JythonInterpreter.java View source code |
//super//
public void init(Page owner, String zslang) {
super.init(owner, zslang);
if (System.getProperty("python.home") == null)
System.setProperty("python.home", System.getProperty("java.io.tmpdir"));
PySystemState.initialize();
PySystemState.add_extdir(owner.getDesktop().getWebApp().getRealPath("/WEB-INF/lib"), true);
PySystemState.add_classdir(owner.getDesktop().getWebApp().getRealPath("/WEB-INF/classes"));
_ip = new PythonInterpreter(new Variables());
}Example 27
| Project: darks-orm-master File: PythonParser.java View source code |
/**
* Parse python script data
*
* @param aspectData Aspect data
* @param simpleWrapper {@linkplain darks.orm.core.data.xml.SimpleAspectWrapper SimpleAspectWrapper}
* @param queryEnumType {@linkplain darks.orm.app.QueryEnumType QueryEnumType}
* @param methodType ½âÎö·½·¨ÀàÐÍ JY_ASPECT_BEFORE or JY_ASPECT_AFTER
* @return ÊÇ·ñ¼ÌÐøÖ´ÐÐ
*/
public boolean parse(AspectData aspectData, SimpleAspectWrapper simpleWrapper, QueryEnumType queryEnumType, String methodType) throws JythonAspectException {
if (aspectData == null)
return true;
PythonInterpreter pyInter = initPythonInterpreter(simpleWrapper);
if (aspectData.getAspectType() == AspectType.JYTHON) {
String content = PythonBuilder.buildJython(aspectData, aspectData.getClassName(), aspectData.getContent());
try {
pyInter.exec(content);
} catch (Exception e) {
e.printStackTrace();
throw new JythonAspectException(e);
}
} else if (aspectData.getAspectType() == AspectType.PYFILE) {
pyInter.exec(PythonBuilder.getPythonHeader().toString());
pyInter.execfile(getFileInputStream(aspectData.getContent()));
pyInter.exec(PythonBuilder.buildJythonTail(aspectData.getClassName()));
} else {
return true;
}
PyObject aspectObj = pyInter.get(PythonBuilder.JY_ASPECT_CLASS);
PyObject retObj = aspectObj.invoke(methodType);
if (retObj == null || retObj instanceof PyNone) {
return false;
} else {
Integer ret = (Integer) JythonFactory.getInstance().pyObjectToObject(retObj, Integer.class);
if (ret == 0)
return false;
else
return true;
}
}Example 28
| Project: floodlight-master File: JythonServer.java View source code |
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
if (this.host == null) {
p.exec("run_server(port=" + this.port + ", locals=locals())");
} else {
p.exec("run_server(port=" + this.port + ", host='" + this.host + "', locals=locals())");
}
}Example 29
| Project: floodlight-nfv-master File: JythonServer.java View source code |
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
if (this.host == null) {
p.exec("run_server(port=" + this.port + ", locals=locals())");
} else {
p.exec("run_server(port=" + this.port + ", host='" + this.host + "', locals=locals())");
}
}Example 30
| Project: floodlightUI-master File: JythonServer.java View source code |
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
if (this.host == null) {
p.exec("run_server(port=" + this.port + ", locals=locals())");
} else {
p.exec("run_server(port=" + this.port + ", host='" + this.host + "', locals=locals())");
}
}Example 31
| Project: floodlight_with_topoguard-master File: JythonServer.java View source code |
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
if (this.host == null) {
p.exec("run_server(port=" + this.port + ", locals=locals())");
} else {
p.exec("run_server(port=" + this.port + ", host='" + this.host + "', locals=locals())");
}
}Example 32
| Project: geoserver-master File: PythonPlugin.java View source code |
@Override
public void init(ScriptManager scriptMgr) throws Exception {
//add lib to python.path
Properties props = new Properties();
props.put("python.path", scriptMgr.script("lib/" + "py").dir().getAbsolutePath());
PythonInterpreter.initialize(null, props, null);
// codecs.register(new PyObject() {
// @Override
// public PyObject __call__(PyObject arg0) {
// if ("idna".equals(arg0.toString())) {
// return new PyTuple(
// new PyObject() {
// public PyObject __call__(PyObject v) {
// return new PyTuple(new PyString(IDN.toUnicode(v.toString())), new PyInteger(0));
// }
// },
// new PyObject() {
// public PyObject __call__(PyObject v) {
// return new PyTuple(new PyString(IDN.toASCII(v.toString())), new PyInteger(0));
// }
// }
// );
// }
// return Py.None;
// }
// });
}Example 33
| Project: giraph-gora-master File: HiveJythonUtils.java View source code |
/** * Parse set of Jython scripts from local files * * @param interpreter PythonInterpreter to use * @param paths Jython files to parse * @return JythonJob * @throws IOException */ public static JythonJob parseJythonFiles(PythonInterpreter interpreter, List<String> paths) throws IOException { InputStream[] streams = new InputStream[paths.size()]; for (int i = 0; i < paths.size(); ++i) { LOG.info("Reading jython file " + paths.get(i)); streams[i] = new FileInputStream(paths.get(i)); } JythonJob jythonJob; try { jythonJob = parseJythonStreams(interpreter, streams); } finally { for (InputStream stream : streams) { Closeables.closeQuietly(stream); } } return jythonJob; }
Example 34
| Project: giraph-master File: TestJythonWritableWrapper.java View source code |
@Test
public void testWrap() throws IOException {
String jython = "class Foo:\n" + " def __init__(self):\n" + " self.val = 17\n" + "\n" + " def do_add(self, x):\n" + " self.val += x\n" + "\n" + " def do_add_squared(self, x):\n" + " self.do_add(x * x)\n" + "\n" + " def new_other(self):\n" + " self.other_val = 3\n" + "\n" + "def outside_add_squared(foo, x):\n" + " foo.do_add_squared(x)\n" + "\n";
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec(jython);
PyObject fooClass = interpreter.get("Foo");
assertTrue(fooClass instanceof PyClass);
PyObject foo = fooClass.__call__();
PyObject fooVal = foo.__getattr__("val");
assertTrue(fooVal instanceof PyInteger);
PyInteger val = (PyInteger) fooVal;
assertEquals(17, val.getValue());
PyObject function = interpreter.get("outside_add_squared");
assertTrue("method class: " + function.getClass(), function instanceof PyFunction);
function.__call__(foo, new PyInteger(3));
fooVal = foo.__getattr__("val");
assertTrue(fooVal instanceof PyInteger);
val = (PyInteger) fooVal;
assertEquals(26, val.getValue());
JythonWritableWrapper wrappedFoo = new JythonWritableWrapper(foo);
PyObject newOtherMethod = wrappedFoo.__getattr__("new_other");
assertTrue(newOtherMethod instanceof PyMethod);
newOtherMethod.__call__();
function.__call__(wrappedFoo, new PyInteger(2));
fooVal = foo.__getattr__("val");
assertTrue(fooVal instanceof PyInteger);
val = (PyInteger) fooVal;
assertEquals(30, val.getValue());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
wrappedFoo.write(dos);
byte[] data = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
PyObject foo2 = fooClass.__call__();
PyObject foo2Val = foo2.__getattr__("val");
assertTrue(foo2Val instanceof PyInteger);
PyInteger val2 = (PyInteger) foo2Val;
assertEquals(17, val2.getValue());
JythonWritableWrapper wrappedFoo2 = new JythonWritableWrapper(foo2);
foo2Val = wrappedFoo2.getPyObject().__getattr__("val");
assertTrue(foo2Val instanceof PyInteger);
val2 = (PyInteger) foo2Val;
assertEquals(17, val2.getValue());
wrappedFoo2.readFields(dis);
foo2Val = wrappedFoo2.getPyObject().__getattr__("val");
assertTrue(foo2Val instanceof PyInteger);
val2 = (PyInteger) foo2Val;
assertEquals(30, val2.getValue());
}Example 35
| Project: golo-jmh-benchmarks-master File: MethodDispatchMicroBenchmark.java View source code |
@Setup(Level.Trial)
public void prepare() {
PythonInterpreter interpreter = new CodeLoader().jython("dispatch");
dispatcher = (PyFunction) interpreter.get("dispatch");
MonomorphicState monomorphicState = new MonomorphicState();
monomorphicState.prepare();
monomorphicArray = new PyArray(Object.class, monomorphicState.data);
TriMorphicState triMorphicState = new TriMorphicState();
triMorphicState.prepare();
trimorphicArray = new PyArray(Object.class, triMorphicState.data);
PolyMorphicState polyMorphicState = new PolyMorphicState();
polyMorphicState.prepare();
polymorphicArray = new PyArray(Object.class, polyMorphicState.data);
}Example 36
| Project: interactivespaces-master File: PythonActivityScriptWrapper.java View source code |
@Override
public Activity newInstance() {
try {
PythonInterpreter interp = PythonInterpreter.threadLocalStateInterpreter(null);
// If script dir in the activity install, include at beginning of
// path.
File scriptDir = new File(activityFilesystem.getInstallDirectory(), "lib/python");
if (scriptDir.exists()) {
if (scriptDir.canRead()) {
PySystemState systemState = interp.getSystemState();
systemState.path.add(0, Py.newString(scriptDir.getAbsolutePath()));
}
}
String script = scriptSource.getScriptContents();
interp.exec(script);
PyObject cls = interp.get(objectName);
if (cls == null)
throw new InteractiveSpacesException(String.format("No callable class named %s in %s", objectName, script));
PyObject processingUnit = cls.__call__();
Object o = processingUnit.__tojava__(Activity.class);
if (o == Py.NoConversion)
throw new InteractiveSpacesException(String.format("The value with name %s must implement %s", objectName, Activity.class.getName()));
return (Activity) o;
} catch (PyException e) {
e.printStackTrace();
throw new InteractiveSpacesException("Could not create Jython Interactive Spaces activity ", e);
}
}Example 37
| Project: lzusdn-master File: JythonServer.java View source code |
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
if (this.host == null) {
p.exec("run_server(port=" + this.port + ", locals=locals())");
} else {
p.exec("run_server(port=" + this.port + ", host='" + this.host + "', locals=locals())");
}
}Example 38
| Project: cloud-slang-master File: ScriptEvaluatorTest.java View source code |
@Bean
public PythonExecutionEngine pythonExecutionEngine() {
return new PythonExecutionCachedEngine() {
protected PythonExecutor createNewExecutor(Set<String> filePaths) {
return new PythonExecutor(filePaths) {
protected PythonInterpreter initInterpreter(Set<String> dependencies) {
return pythonInterpreter();
}
};
}
};
}Example 39
| Project: cocoon-master File: PythonGenerator.java View source code |
public void initialize() throws Exception {
try {
Properties properties = new Properties();
File workDir = (File) avalonContext.get(Constants.CONTEXT_WORK_DIR);
properties.setProperty("python.home", workDir.toString());
properties.setProperty("python.packages.fakepath", (String) avalonContext.get(Constants.CONTEXT_CLASSPATH));
PythonInterpreter.initialize(System.getProperties(), properties, new String[] {});
python = new PythonInterpreter();
python.set("page", this);
python.set("logger", getLogger());
python.set("xspAttr", new AttributesImpl());
if (getLogger().isDebugEnabled()) {
getLogger().debug("Compiling script " + file);
}
this.code = Py.compile(new FileInputStream(this.file), this.file.toString(), "exec");
} catch (Exception e) {
this.compileError = e;
}
}Example 40
| Project: ggp-base-master File: PythonGamer.java View source code |
// Gamer stubs are lazily loaded because the Python interface takes
// time to initialize, so we only want to load it when necessary, and
// not for light-weight things like returning the player name.
private void lazilyLoadGamerStub() {
if (thePythonGamer == null) {
try {
// Load in the Python gamer, using a Jython intepreter.
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("from " + getPythonGamerModule() + " import " + getPythonGamerName());
PyObject thePyClass = interpreter.get(getPythonGamerName());
PyObject PyGamerObject = thePyClass.__call__();
thePythonGamer = (Gamer) PyGamerObject.__tojava__(Gamer.class);
} catch (Exception e) {
GamerLogger.logError("GamePlayer", "Caught exception in Python initialization:");
GamerLogger.logStackTrace("GamePlayer", e);
}
}
}Example 41
| Project: hadoop-pig-master File: JythonScriptEngine.java View source code |
@Override
public void registerFunctions(String path, String namespace, PigContext pigContext) throws IOException {
Interpreter.init(path);
pigContext.scriptJars.add(getJarPath(PythonInterpreter.class));
PythonInterpreter pi = Interpreter.interpreter;
@SuppressWarnings("unchecked") List<PyTuple> locals = (List<PyTuple>) ((PyStringMap) pi.getLocals()).items();
if (namespace == null) {
namespace = "";
} else {
namespace = namespace + namespaceSeparator;
}
try {
for (PyTuple item : locals) {
String key = (String) item.get(0);
Object value = item.get(1);
FuncSpec funcspec = null;
if (!key.startsWith("__") && !key.equals("schemaFunction") && !key.equals("outputSchema") && !key.equals("outputSchemaFunction") && (value instanceof PyFunction) && (((PyFunction) value).__findattr__("schemaFunction".intern()) == null)) {
PyObject obj = ((PyFunction) value).__findattr__("outputSchema".intern());
if (obj != null) {
Utils.getSchemaFromString(obj.toString());
}
funcspec = new FuncSpec(JythonFunction.class.getCanonicalName() + "('" + path + "','" + key + "')");
pigContext.registerFunction(namespace + key, funcspec);
}
}
} catch (ParseException pe) {
throw new IOException("Error parsing schema for script function from the decorator " + pe);
}
}Example 42
| Project: Hakd-master File: MenuTerminal.java View source code |
@Override
public void initializeTerminal() throws Exception {
Skin skin = Hakd.assets.get(("skins/uiskin.json"), Skin.class);
display = new Label("", skin.get("console", Label.LabelStyle.class));
scroll = new ScrollPane(display, skin);
// display.setFontScale(.6f);
// for now, this is broken for labels, but I will leave it in so I can easily tell when it gets fixed
display.getStyle().font.setMarkupEnabled(true);
display.setWrap(false);
display.setAlignment(10, Align.left);
String terminalInfo = "[#3C91BF]Terminal [[Version 0." + ((int) (Math.random() * 100)) / 10 + "]";
terminalInfo += "\n[#38FF4C]" + System.getProperty("user.name") + "[] @ [#FFC123]127.0.0.1[]";
terminalInfo += "\nStorage:";
// makes hakd start a bit slower, but lets the first jython program start a bit sooner
// it is a trade off that lets the game feel more responsive
new PythonInterpreter().exec("import sys");
// I could just search for /dev/sd* if on linux(mac too?) and read the size of them some how, not worth the work
for (int i = 0; i < File.listRoots().length; i++) {
terminalInfo += "\n Drive[[" + i + "] " + (-File.listRoots()[i].getFreeSpace() + File.listRoots()[i].getTotalSpace()) / 1000000000 + "GB Used, " + File.listRoots()[i].getTotalSpace() / 1000000000 + "GB Total";
}
display.setText(terminalInfo + "\n-----------------------------------------------------\n" + " Type \"help\" to get started.[]\n\n");
}Example 43
| Project: jvmnotebook-master File: JythonUtilPlugin.java View source code |
///==============================================================
///
/// ** Methods **
///
///==============================================================
public void reset() {
interp = new PythonInterpreter(null, new PySystemState());
cache.clear();
PySystemState sys = Py.getSystemState();
sys.path.append(new PyString(rootPath));
String modulesDir = rootPath + "WEB-INF" + File.separator + "jython";
sys.path.append(new PyString(modulesDir));
// Needed in order python to recognize classes in the class directory
String classDir = rootPath + "WEB-INF" + File.separator + "classes";
sys.path.append(new PyString(classDir));
}Example 44
| Project: red5-master File: JythonScriptFactory.java View source code |
/** {@inheritDoc} */
@SuppressWarnings({ "rawtypes" })
public Object getScriptedObject(ScriptSource scriptSourceLocator, Class[] scriptInterfaces) throws IOException, ScriptCompilationException {
String basePath = "";
/* TODO: how to do this when running under Tomcat?
ContextHandler handler = WebAppContext.getCurrentWebAppContext();
if (handler != null) {
File root = handler.getBaseResource().getFile();
if (root != null && root.exists()) {
basePath = root.getAbsolutePath() + File.separator + "WEB-INF" + File.separator;
}
}
*/
String strScript = scriptSourceLocator.getScriptAsString();
if (scriptInterfaces.length > 0) {
try {
PySystemState state = new PySystemState();
if (!"".equals(basePath)) {
// Add webapp paths that can contain classes and .jar files to python search path
state.path.insert(0, Py.newString(basePath + "classes"));
File jarRoot = new File(basePath + "lib");
if (jarRoot.exists()) {
for (String filename : jarRoot.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return (name.endsWith(".jar"));
}
})) {
state.path.insert(1, Py.newString(basePath + "lib" + File.separator + filename));
}
}
}
PythonInterpreter interp = new PythonInterpreter(null, state);
interp.exec(strScript);
PyObject getInstance = interp.get("getInstance");
if (!(getInstance instanceof PyFunction)) {
throw new ScriptCompilationException("\"getInstance\" is not a function.");
}
PyObject _this;
if (arguments == null) {
_this = ((PyFunction) getInstance).__call__();
} else {
PyObject[] args = new PyObject[arguments.length];
for (int i = 0; i < arguments.length; i++) {
args[i] = PyJavaType.wrapJavaObject(arguments[i]);
}
_this = ((PyFunction) getInstance).__call__(args);
}
return _this.__tojava__(scriptInterfaces[0]);
} catch (Exception ex) {
logger.error("Error while loading script.", ex);
if (ex instanceof IOException) {
throw (IOException) ex;
} else if (ex instanceof ScriptCompilationException) {
throw (ScriptCompilationException) ex;
}
throw new ScriptCompilationException(ex.getMessage());
}
}
logger.error("No scriptInterfaces provided.");
return null;
}Example 45
| Project: red5-mobileconsole-master File: JythonScriptFactory.java View source code |
/** {@inheritDoc} */
@SuppressWarnings({ "rawtypes" })
public Object getScriptedObject(ScriptSource scriptSourceLocator, Class[] scriptInterfaces) throws IOException, ScriptCompilationException {
String basePath = "";
/* TODO: how to do this when running under Tomcat?
ContextHandler handler = WebAppContext.getCurrentWebAppContext();
if (handler != null) {
File root = handler.getBaseResource().getFile();
if (root != null && root.exists()) {
basePath = root.getAbsolutePath() + File.separator + "WEB-INF" + File.separator;
}
}
*/
String strScript = scriptSourceLocator.getScriptAsString();
if (scriptInterfaces.length > 0) {
try {
PySystemState state = new PySystemState();
if (!"".equals(basePath)) {
// Add webapp paths that can contain classes and .jar files to python search path
state.path.insert(0, Py.newString(basePath + "classes"));
File jarRoot = new File(basePath + "lib");
if (jarRoot.exists()) {
for (String filename : jarRoot.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return (name.endsWith(".jar"));
}
})) {
state.path.insert(1, Py.newString(basePath + "lib" + File.separator + filename));
}
}
}
PythonInterpreter interp = new PythonInterpreter(null, state);
interp.exec(strScript);
PyObject getInstance = interp.get("getInstance");
if (!(getInstance instanceof PyFunction)) {
throw new ScriptCompilationException("\"getInstance\" is not a function.");
}
PyObject _this;
if (arguments == null) {
_this = ((PyFunction) getInstance).__call__();
} else {
PyObject[] args = new PyObject[arguments.length];
for (int i = 0; i < arguments.length; i++) {
args[i] = PyJavaType.wrapJavaObject(arguments[i]);
}
_this = ((PyFunction) getInstance).__call__(args);
}
return _this.__tojava__(scriptInterfaces[0]);
} catch (Exception ex) {
logger.error("Error while loading script.", ex);
if (ex instanceof IOException) {
throw (IOException) ex;
} else if (ex instanceof ScriptCompilationException) {
throw (ScriptCompilationException) ex;
}
throw new ScriptCompilationException(ex.getMessage());
}
}
logger.error("No scriptInterfaces provided.");
return null;
}Example 46
| Project: red5-plugins-master File: JythonScriptFactory.java View source code |
/** {@inheritDoc} */
public Object getScriptedObject(ScriptSource scriptSourceLocator, Class[] scriptInterfaces) throws IOException, ScriptCompilationException {
// TODO: how to do this when running under Tomcat?
ContextHandler handler = WebAppContext.getCurrentWebAppContext();
String basePath = "";
if (handler != null) {
File root = handler.getBaseResource().getFile();
if (root != null && root.exists()) {
basePath = root.getAbsolutePath() + File.separator + "WEB-INF" + File.separator;
}
}
String strScript = scriptSourceLocator.getScriptAsString();
if (scriptInterfaces.length > 0) {
try {
PySystemState state = new PySystemState();
if (!"".equals(basePath)) {
// Add webapp paths that can contain classes and .jar files to python search path
state.path.insert(0, Py.newString(basePath + "classes"));
File jarRoot = new File(basePath + "lib");
if (jarRoot.exists()) {
for (String filename : jarRoot.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return (name.endsWith(".jar"));
}
})) {
state.path.insert(1, Py.newString(basePath + "lib" + File.separator + filename));
}
}
}
PythonInterpreter interp = new PythonInterpreter(null, state);
interp.exec(strScript);
PyObject getInstance = interp.get("getInstance");
if (!(getInstance instanceof PyFunction)) {
throw new ScriptCompilationException("\"getInstance\" is not a function.");
}
PyObject _this;
if (arguments == null) {
_this = ((PyFunction) getInstance).__call__();
} else {
PyObject[] args = new PyObject[arguments.length];
for (int i = 0; i < arguments.length; i++) {
args[i] = PyJavaType.wrapJavaObject(arguments[i]);
}
_this = ((PyFunction) getInstance).__call__(args);
}
return _this.__tojava__(scriptInterfaces[0]);
} catch (Exception ex) {
logger.error("Error while loading script.", ex);
if (ex instanceof IOException) {
throw (IOException) ex;
} else if (ex instanceof ScriptCompilationException) {
throw (ScriptCompilationException) ex;
}
throw new ScriptCompilationException(ex.getMessage());
}
}
logger.error("No scriptInterfaces provided.");
return null;
}Example 47
| Project: SonicFieldRepo-master File: Sython.java View source code |
@SuppressWarnings("nls")
public static void main(String[] args) {
try {
// Checks for deadlocks IFF the property SFConstants.FIND_DEADLOCKS is set.
if (SFConstants.FIND_DEADLOCKS) {
findDeadlocks();
}
try (InputStream sis = Sython.class.getClassLoader().getResourceAsStream("com/nerdscentral/sython/sython.py");
InputStreamReader sir = new InputStreamReader(sis);
BufferedReader bsir = new BufferedReader(sir)) {
{
String lin = null;
while ((lin = bsir.readLine()) != null) {
if (lin.trim().length() > 0) {
init(lin);
}
}
}
}
// ==============
try (PythonInterpreter interp = new PythonInterpreter()) {
try (InputStream pis = Sython.class.getClassLoader().getResourceAsStream("com/nerdscentral/sython/processors.txt");
InputStreamReader pir = new InputStreamReader(pis);
BufferedReader bpir = new BufferedReader(pir)) {
String lin = null;
HashMap<String, SFPL_Operator> processors = new HashMap<>();
interp.exec("import __builtin__");
while ((lin = bpir.readLine()) != null) {
if (lin.trim().length() > 0) {
SFPL_Operator op = (SFPL_Operator) Class.forName(lin).newInstance();
String word = op.Word();
processors.put(word, op);
init(" def " + word + "(self, input, *args):");
init(" return self.run(\"" + word + "\",input,args)");
init(" __builtin__." + word + "=" + word);
}
}
List<SFPL_Operator> vols = new ArrayList<>(404);
vols.addAll(SFP_DBs.getAll());
vols.addAll(SFP_Pcnt.getAll());
for (SFPL_Operator op : vols) {
String word = op.Word();
processors.put(word, op);
init(" def " + word + "(self, input):");
init(" return self.run(\"" + word + "\",input,[])");
init(" __builtin__." + word + "=" + word);
}
init("");
System.out.println("Python about to interpret:");
// System.out.println(initCode);
interp.exec(initCode.toString());
PyObject pyo = interp.get("SonicField");
PyDictionary pid = new PyDictionary();
pid.putAll(processors);
PyObject sf = pyo.__call__(pid);
interp.exec("print \"Installing sf object\"");
interp.set("sf", sf);
interp.exec("__builtin__.sf=sf");
}
interp.exec("import __builtin__");
interp.exec("__builtin__.sf=sf");
interp.exec("import sython.concurrent");
interp.exec("print \"Switching To Python Mode\"");
interp.exec("print \"========================\"");
long t0 = System.currentTimeMillis();
for (String f : args) {
interp.exec(f);
}
interp.exec("sython.concurrent.sf_shutdown()");
interp.exec("print \"========================\"");
interp.exec("print \"------- All DONE -------\"");
long t1 = System.currentTimeMillis();
System.out.println("Total Processing Took: " + ((t1 - t0) / 1000) + " seconds");
}
} catch (Throwable t) {
while (t != null) {
t.printStackTrace();
t = t.getCause();
}
System.exit(1);
}
System.exit(0);
}Example 48
| Project: tuscany-sca-2.x-master File: PythonImplementationProvider.java View source code |
public void start() {
final PySystemState pss = new PySystemState();
pss.path.insert(0, new PyString(implementation.getLocation()));
pss.path.insert(0, new PyString(getClass().getProtectionDomain().getCodeSource().getLocation().getFile()));
python = new PythonInterpreter(null, pss);
python.exec("from invoker import *");
final List<PyObject> px = new ArrayList<PyObject>();
for (final ComponentReference r : component.getReferences()) {
final PythonEval pe = pxFactory.createProxy(PythonEval.class, (RuntimeEndpointReference) r.getEndpointReferences().get(0));
px.add(Py.java2py(new PythonEval() {
@Override
public String eval(final String args) throws Exception {
final String v = pe.eval(args);
return v;
}
}));
}
final List<PyObject> pr = new ArrayList<PyObject>();
for (final ComponentProperty p : component.getProperties()) {
final String v = String.valueOf(p.getValue());
pr.add(Py.java2py(new PythonProperty() {
@Override
public String eval() {
return v;
}
}));
}
PyObject mkc = python.get("mkcomponent");
callable = mkc.__call__(new PyString(component.getName()), new PyString(implementation.getScript()), new PyTuple(px.toArray(new PyObject[0])), new PyTuple(pr.toArray(new PyObject[0])));
}Example 49
| Project: Web-Karma-master File: PythonTransformationCommand.java View source code |
protected void generateTransformedValues(Workspace workspace, Worksheet worksheet, RepFactory f, HNode hNode, JSONArray transformedRows, JSONArray errorValues, Integer limit) throws JSONException, IOException {
final ServletContextParameterMap contextParameters = ContextParametersRegistry.getInstance().getContextParameters(workspace.getContextId());
SuperSelection selection = getSuperSelection(worksheet);
String trimmedTransformationCode = transformationCode.trim();
// exceptions.
if (trimmedTransformationCode.isEmpty()) {
trimmedTransformationCode = "return \"\"";
logger.info("Empty PyTransform statement in " + hNode.getColumnName());
}
String transformMethodStmt = PythonTransformationHelper.getPythonTransformMethodDefinitionState(worksheet, trimmedTransformationCode, "");
logger.debug("Executing PyTransform {}\n", transformMethodStmt);
// Prepare the Python interpreter
PythonRepository repo = PythonRepositoryRegistry.getInstance().getPythonRepository(contextParameters.getParameterValue(ContextParameter.USER_PYTHON_SCRIPTS_DIRECTORY));
PythonInterpreter interpreter = repo.getInterpreter();
repo.initializeInterpreter(interpreter);
Collection<Node> nodes = new ArrayList<>(Math.max(1000, worksheet.getDataTable().getNumRows()));
worksheet.getDataTable().collectNodes(hNode.getHNodePath(f), nodes, selection);
Map<String, String> rowToValueMap = new HashMap<>();
int counter = 0;
long starttime = System.currentTimeMillis();
// Go through all nodes collected for the column with given hNodeId;
PyObject locals = interpreter.getLocals();
locals.__setitem__("workspaceid", new PyString(workspace.getId()));
locals.__setitem__("command", Py.java2py(this));
locals.__setitem__("worksheetId", new PyString(worksheet.getId()));
locals.__setitem__("selectionName", new PyString(selection.getName()));
repo.compileAndAddToRepositoryAndExec(interpreter, transformMethodStmt);
PyCode py = repo.getTransformCode();
int numRowsWithErrors = 0;
for (Node node : nodes) {
Row row = node.getBelongsToRow();
locals.__setitem__("nodeid", new PyString(node.getId()));
try {
PyObject output = interpreter.eval(py);
String transformedValue = PythonTransformationHelper.getPyObjectValueAsString(output);
addTransformedValue(transformedRows, row, transformedValue);
} catch (PyException p) {
logger.debug("error in evaluation python, skipping one row");
numRowsWithErrors++;
addTransformedValue(transformedRows, row, errorDefaultValue);
addError(errorValues, row, counter, p.value);
} catch (Exception t) {
logger.debug("Error occured while transforming, using default value.", t);
numRowsWithErrors++;
rowToValueMap.put(row.getId(), errorDefaultValue);
}
if (limit != null && ++counter >= limit) {
break;
}
}
if (numRowsWithErrors > 0) {
logger.debug("PyTransform errors in " + numRowsWithErrors + " rows. This could be normal when rows have unexpected values.");
}
logger.debug("transform time " + (System.currentTimeMillis() - starttime));
}Example 50
| Project: android-platform_sdk-master File: ScriptRunner.java View source code |
/**
* Runs the specified Jython script. First runs the initialization script to
* preload the appropriate client library version.
*
* @param scriptfilename the name of the file to run.
* @param args the arguments passed in (excluding the filename).
* @param plugins a list of plugins to load.
* @return the error code from running the script.
*/
public static int run(String executablePath, String scriptfilename, Collection<String> args, Map<String, Predicate<PythonInterpreter>> plugins) {
// Add the current directory of the script to the python.path search path.
File f = new File(scriptfilename);
// Adjust the classpath so jython can access the classes in the specified classpath.
Collection<String> classpath = Lists.newArrayList(f.getParent());
classpath.addAll(plugins.keySet());
String[] argv = new String[args.size() + 1];
argv[0] = f.getAbsolutePath();
int x = 1;
for (String arg : args) {
argv[x++] = arg;
}
initPython(executablePath, classpath, argv);
PythonInterpreter python = new PythonInterpreter();
// Now let the mains run.
for (Map.Entry<String, Predicate<PythonInterpreter>> entry : plugins.entrySet()) {
boolean success;
try {
success = entry.getValue().apply(python);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Plugin Main through an exception.", e);
continue;
}
if (!success) {
LOG.severe("Plugin Main returned error for: " + entry.getKey());
}
}
// Bind __name__ to __main__ so mains will run
python.set("__name__", "__main__");
// Also find __file__
python.set("__file__", scriptfilename);
try {
python.execfile(scriptfilename);
} catch (PyException e) {
if (Py.SystemExit.equals(e.type)) {
return (Integer) e.value.__tojava__(Integer.class);
}
LOG.log(Level.SEVERE, "Script terminated due to an exception", e);
return 1;
}
return 0;
}Example 51
| Project: autopsy-master File: JythonModuleLoader.java View source code |
private static <T> List<T> getInterfaceImplementations(LineFilter filter, Class<T> interfaceClass) {
List<T> objects = new ArrayList<>();
Set<File> pythonModuleDirs = new HashSet<>();
PythonInterpreter interpreter = new PythonInterpreter();
// which are copied from 'autopsy/*/release/InternalPythonModules' folders.
for (File f : InstalledFileLocator.getDefault().locateAll("InternalPythonModules", "org.sleuthkit.autopsy.core", false)) {
//NON-NLS
Collections.addAll(pythonModuleDirs, f.listFiles());
}
// add python modules from 'testuserdir/python_modules' folder
Collections.addAll(pythonModuleDirs, new File(PlatformUtil.getUserPythonModulesPath()).listFiles());
for (File file : pythonModuleDirs) {
if (file.isDirectory()) {
File[] pythonScripts = file.listFiles(new PythonScriptFileFilter());
for (File script : pythonScripts) {
try {
Scanner fileScanner = new Scanner(script);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
if (line.startsWith("class ") && filter.accept(line)) {
//NON-NLS
String className = line.substring(6, line.indexOf("("));
try {
objects.add(createObjectFromScript(interpreter, script, className, interfaceClass));
} catch (Exception ex) {
logger.log(Level.SEVERE, String.format("Failed to load %s from %s", className, script.getAbsolutePath()), ex);
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(NbBundle.getMessage(JythonModuleLoader.class, "JythonModuleLoader.errorMessages.failedToLoadModule", className, ex.toString()), NotifyDescriptor.ERROR_MESSAGE));
}
}
}
} catch (FileNotFoundException ex) {
logger.log(Level.SEVERE, String.format("Failed to open %s", script.getAbsolutePath()), ex);
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(NbBundle.getMessage(JythonModuleLoader.class, "JythonModuleLoader.errorMessages.failedToOpenModule", script.getAbsolutePath()), NotifyDescriptor.ERROR_MESSAGE));
}
}
}
}
return objects;
}Example 52
| Project: elassandra-master File: PythonScriptEngineService.java View source code |
@Override public PythonInterpreter run() { // snapshot our context here for checks, as the script has no permissions final AccessControlContext engineContext = AccessController.getContext(); PythonInterpreter interp = PythonInterpreter.threadLocalStateInterpreter(null); if (sm != null) { interp.getSystemState().setClassLoader(new ClassLoader(getClass().getClassLoader()) { @Override protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { try { engineContext.checkPermission(new ClassPermission(name)); } catch (SecurityException e) { throw new ClassNotFoundException(name, e); } return super.loadClass(name, resolve); } }); } return interp; }
Example 53
| Project: Electric8-master File: EvalJython.java View source code |
public static boolean hasJython() {
if (!jythonChecked) {
jythonChecked = true;
// find the Jython class
try {
jythonClass = Class.forName("org.python.util.PythonInterpreter");
} catch (Exception e) {
jythonClass = null;
}
}
// if already initialized, return state
return jythonClass != null;
}Example 54
| Project: eoulsan-master File: CheetahInterpreter.java View source code |
/**
* Execute script by Python interpreter and replace variable name by value.
* @return final command line
* @throws EoulsanException if an error throws by interpretation.
*/
public String execute() throws EoulsanException {
try (final PythonInterpreter interpreter = new PythonInterpreter()) {
final PyObject nameSpace = createNameSpace(this.variables);
final String template = this.cheetahScript;
interpreter.set("template", template);
interpreter.set("nameSpace", nameSpace);
final String pythonScript = "from Cheetah.Template import Template\n" + "result = str(Template(template, searchList=[nameSpace]))";
interpreter.exec(pythonScript);
// Retrieve standard output
final PyObject cmd = interpreter.get("result");
return cmd.asString().replace('\n', ' ').trim();
}
}Example 55
| Project: Ganymede-master File: JythonTask.java View source code |
/**
* <p>This method is the one invoked when the scheduler runs this
* task. It creates a new Jython interpreter, runs the bootstrapping
* code, and then executes the external Jython task.</p>
*/
public final void run() {
String uri;
PythonInterpreter interp;
uri = getURI();
if (uri == null) {
throw new RuntimeException("Unable to get URI of the the Jython code for this task.");
}
/* Initialize the interpreter */
interp = new PythonInterpreter(null, new PySystemState());
try {
/* Import the additional Jython library routines */
interp.exec("import sys");
interp.exec("sys.path.append( sys.prefix + '" + System.getProperty("file.separator") + "' + 'jython-lib.jar' )");
/* Launch the actual task */
interp.exec("import JythonTaskBootstrapper");
interp.set("uri", uri);
interp.exec("JythonTaskBootstrapper.run(uri)");
} catch (PyException pex) {
throw new RuntimeException(pex.toString());
}
interp = null;
}Example 56
| Project: jython-on-android-master File: JythonOnAndroidRunScript.java View source code |
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
copyResourcesToLocal();
Properties props = new Properties();
props.setProperty("python.path", this.getFilesDir().getAbsolutePath());
PythonInterpreter.initialize(System.getProperties(), props, new String[] { "" });
PythonInterpreter interp = new PythonInterpreter();
String main_file = this.getFilesDir().getAbsolutePath() + "/main.jython";
Log.d("Jython-For-Android", "main_file = " + main_file);
interp.execfile(main_file);
}Example 57
| Project: kraken-master File: JythonLogScriptRegistryImpl.java View source code |
private PyObject eval(String name, String script) {
PythonInterpreter interpreter = jython.newInterpreter();
interpreter.exec("from org.krakenapps.logdb import LogScript");
interpreter.exec("from org.krakenapps.logdb import BaseLogScript");
interpreter.exec(script);
PyObject clazz = interpreter.get(name);
if (clazz == null)
throw new IllegalStateException("cannot eval jython script " + name);
return clazz;
}Example 58
| Project: logdb-master File: JythonTransformerScriptRegistryImpl.java View source code |
private PyObject eval(String name, String script) {
PythonInterpreter interpreter = jython.newInterpreter();
interpreter.exec("from org.araqne.log.api import SimpleLog");
interpreter.exec("from org.araqne.log.api import LogTransformer");
interpreter.exec("from java.util import Date");
interpreter.exec(script);
PyObject clazz = interpreter.get(name);
if (clazz == null)
throw new IllegalStateException("cannot eval jython logger script " + name);
return clazz;
}Example 59
| Project: nodebox-master File: PythonLibrary.java View source code |
public ImmutableMap<String, Function> call() throws Exception {
// This creates a dependency between function and the client.
// However, we need to know the load paths before we can do anything, so this is necessary.
PythonUtils.initializePython();
Py.getSystemState().path.append(new PyString(file.getParentFile().getCanonicalPath()));
PythonInterpreter interpreter = new PythonInterpreter();
try {
interpreter.execfile(file.getCanonicalPath());
} catch (IOException e) {
throw new LoadException(file, e);
} catch (PyException e) {
throw new LoadException(file, e);
}
PyStringMap map = (PyStringMap) interpreter.getLocals();
ImmutableMap.Builder<String, Function> builder = ImmutableMap.builder();
for (Object key : map.keys()) {
Object o = map.get(Py.java2py(key));
if (o instanceof PyFunction) {
String name = (String) key;
Function f = new PythonFunction(name, (PyFunction) o);
builder.put(name, f);
}
}
return builder.build();
}Example 60
| Project: opennms_dashboard-master File: TcpRrdStrategyTest.java View source code |
public void run() {
this.setName("fail");
try {
ServerSocket ssocket = new ServerSocket(8999);
ssocket.setSoTimeout(500);
while (true) {
try {
/*
* This python code is not working properly under Jython. My
* hunch is that it would be better under the new Jython 2.5.1
* but that version is not easy to use under Maven, see:
*
* http://bugs.jython.org/issue1512
* http://bugs.jython.org/issue1513
*
PythonInterpreter python = new PythonInterpreter();
python.execfile(
// Load the python path parser script from the classpath
Thread.currentThread().getContextClassLoader().getResourceAsStream(
"rrdPathParser.py"
)
);
python.eval("configureRrdPaths('" + m_tempDir + "')");
*/
Socket socket = ssocket.accept();
PerformanceDataProtos.PerformanceDataReadings messages = PerformanceDataProtos.PerformanceDataReadings.parseFrom(socket.getInputStream());
System.out.println("Number of messages in current packet: " + messages.getMessageCount());
for (PerformanceDataProtos.PerformanceDataReading message : messages.getMessageList()) {
StringBuffer values = new StringBuffer();
values.append("{ ");
for (int i = 0; i < message.getValueCount(); i++) {
if (i != 0) {
values.append(", ");
}
values.append(message.getValue(i));
}
values.append(" }");
System.out.println("Message received: { " + "path: \"" + message.getPath() + "\", " + "owner: \"" + message.getOwner() + "\", " + "timestamp: \"" + message.getTimestamp() + "\", " + "values: " + values.toString() + " }");
/*
* See comments above re: Jython
PyDictionary attributes = (PyDictionary)python.eval("parseRrdPath('" + message.getPath() + "')");
System.out.println(attributes.getClass().getName());
*/
}
} catch (SocketTimeoutException e) {
if (this.isInterrupted()) {
this.setName("notfailed");
return;
}
} catch (IOException e) {
ThreadCategory.getInstance(this.getClass()).error(e.getMessage(), e);
}
}
} catch (IOException e) {
ThreadCategory.getInstance(this.getClass()).error(e.getMessage(), e);
} catch (Throwable e) {
ThreadCategory.getInstance(this.getClass()).error(e.getMessage(), e);
}
}Example 61
| Project: platform_sdk-master File: ScriptRunner.java View source code |
/**
* Runs the specified Jython script. First runs the initialization script to
* preload the appropriate client library version.
*
* @param scriptfilename the name of the file to run.
* @param args the arguments passed in (excluding the filename).
* @param plugins a list of plugins to load.
* @return the error code from running the script.
*/
public static int run(String executablePath, String scriptfilename, Collection<String> args, Map<String, Predicate<PythonInterpreter>> plugins) {
// Add the current directory of the script to the python.path search path.
File f = new File(scriptfilename);
// Adjust the classpath so jython can access the classes in the specified classpath.
Collection<String> classpath = Lists.newArrayList(f.getParent());
classpath.addAll(plugins.keySet());
String[] argv = new String[args.size() + 1];
argv[0] = f.getAbsolutePath();
int x = 1;
for (String arg : args) {
argv[x++] = arg;
}
initPython(executablePath, classpath, argv);
PythonInterpreter python = new PythonInterpreter();
// Now let the mains run.
for (Map.Entry<String, Predicate<PythonInterpreter>> entry : plugins.entrySet()) {
boolean success;
try {
success = entry.getValue().apply(python);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Plugin Main through an exception.", e);
continue;
}
if (!success) {
LOG.severe("Plugin Main returned error for: " + entry.getKey());
}
}
// Bind __name__ to __main__ so mains will run
python.set("__name__", "__main__");
// Also find __file__
python.set("__file__", scriptfilename);
try {
python.execfile(scriptfilename);
} catch (PyException e) {
if (Py.SystemExit.equals(e.type)) {
return (Integer) e.value.__tojava__(Integer.class);
}
LOG.log(Level.SEVERE, "Script terminated due to an exception", e);
return 1;
}
return 0;
}Example 62
| Project: ProbCog-master File: genDB.java View source code |
/**
* @param args
*/
public static void main(String[] args) {
try {
boolean writeBasicBLND = false, writeBasicMLN = false, writeBLOGDB = false, writeMLNDB = false, writeProxDB = false;
boolean printDB = false;
int numRuns = 1;
int i = 0;
boolean abort = false;
if (args.length < 3) {
abort = true;
} else {
// read args
int tasks = 0;
for (; i < args.length; i++) {
if (args[i].equals("-m")) {
writeMLNDB = true;
++tasks;
} else if (args[i].equals("-b")) {
writeBLOGDB = true;
++tasks;
} else if (args[i].equals("-p")) {
writeProxDB = true;
++tasks;
} else if (args[i].equals("-s")) {
printDB = true;
++tasks;
} else if (args[i].equals("-bm")) {
writeBasicMLN = true;
++tasks;
} else if (args[i].equals("-bb")) {
writeBasicBLND = true;
++tasks;
} else if (args[i].equals("-multi")) {
numRuns = Integer.parseInt(args[++i]);
} else
break;
}
if (tasks == 0) {
abort = true;
System.err.println("\nError: No tasks were specified. Add at least one option.\n");
}
}
if (abort) {
System.out.println("\ngenDB - a database generator");
System.out.println("\n usage: genDB [options] <Jython generator script> <output base filename> [parameters to pass on to generator]\n" + " -m output MLN database (.db)\n" + " -b output BLOG database (.blogdb)\n" + " -p output Proximity database (.proxdb.xml)\n" + " -s print read database structure to stdout\n" + " -bm output basic MLN model (.basic.mln)\n" + " -bb output basic BLN model declarations (.basic.blnd)\n" + " -multi N create N databases with the given parameters (appending numbers to the base filename)\n\n" + " The Jython script must create a Database object named 'db' in the global scope.\n");
return;
}
String jythonscript = args[i++];
String baseOutfilename = args[i++];
int firstScriptArgIndex = i;
Properties props = new Properties();
//props.put("python.path", "C:\\Progra~2\\jython-2.1\\Lib;datagen");
//props.put("python.path", "/usr/wiss/jain/work/code/SRLDB/bin:/usr/wiss/jain/work/code/SRLDB/python");
String jythonpath = System.getenv("JYTHONPATH");
if (jythonpath == null) {
System.err.println("Warning: JYTHONPATH environment variable not set. If modules such as 'datagen' cannot be imported, either manually set sys.path in your generator scripts to include the appropriate directories or set this variable to include ProbCog's 'python' directory.");
jythonpath = "";
} else
jythonpath += File.pathSeparator;
jythonpath += System.getProperty("java.class.path");
props.put("python.path", jythonpath);
Properties sysprops = System.getProperties();
PythonInterpreter.initialize(sysprops, props, null);
int numDigits = (int) Math.ceil(Math.log10((double) numRuns));
String multiOutfileFormat = String.format("%s%%0%dd", baseOutfilename, numDigits);
for (int run = 1; run <= numRuns; run++) {
if (numRuns > 1)
System.out.printf("gerating database %d/%d...", run, numRuns);
PythonInterpreter jython = new PythonInterpreter();
jython.exec("import sys");
jython.exec("sys.argv = ['" + jythonscript + "']");
for (int j = firstScriptArgIndex; j < args.length; j++) {
jython.exec("sys.argv.append('" + args[j] + "')");
}
jython.execfile(jythonscript);
PyObject dbObj = jython.get("db");
if (dbObj == null) {
throw new Exception("\nGenerator script does not define 'db' object!");
}
Database db = (Database) dbObj.__tojava__(Database.class);
db.check();
if (printDB)
db.printData();
String outfilename;
if (numRuns == 1)
outfilename = baseOutfilename;
else
outfilename = String.format(multiOutfileFormat, run);
if (writeMLNDB) {
File file = new java.io.File(outfilename + ".db");
System.out.println("Writing MLN database to " + file);
db.writeMLNDatabase(new PrintStream(file));
}
if (writeBLOGDB) {
File file = new java.io.File(outfilename + ".blogdb");
System.out.println("Writing BLN database to " + file);
db.writeBLOGDatabase(new PrintStream(file));
}
if (writeProxDB) {
File file = new java.io.File(outfilename + ".proxdb.xml");
System.out.println("Writing Proximity database to " + file);
db.writeProximityDatabase(new PrintStream(file));
}
if (writeBasicMLN) {
File file = new File(outfilename + ".basic.mln");
System.out.println("Writing basic MLN declarations to " + file);
db.writeBasicMLN(new PrintStream(file));
}
if (writeBasicBLND) {
File file = new File(outfilename + ".basic.blnd");
System.out.println("Writing basic BLN declarations to " + file);
db.getDataDictionary().writeBasicBLOGModel(new PrintStream(file));
}
}
System.out.println("done!");
} catch (Exception e) {
e.printStackTrace();
}
}Example 63
| Project: score-master File: PythonExecutor.java View source code |
protected PythonInterpreter initInterpreter(Set<String> dependencies) {
logger.info("Creating python interpreter with [" + dependencies.size() + "] dependencies [" + dependencies + "]");
if (!dependencies.isEmpty()) {
PySystemState systemState = new PySystemState();
for (String dependency : dependencies) {
systemState.path.append(new PyString(dependency));
}
return new ThreadSafePythonInterpreter(systemState);
}
return GLOBAL_INTERPRETER;
}Example 64
| Project: szeke-master File: SubmitPythonTransformationCommand.java View source code |
@Override
public UpdateContainer doIt(VWorkspace vWorkspace) throws CommandException {
Worksheet worksheet = vWorkspace.getViewFactory().getVWorksheet(vWorksheetId).getWorksheet();
RepFactory f = vWorkspace.getRepFactory();
HNode hNode = f.getHNode(hNodeId);
ExecutionController ctrl = WorkspaceRegistry.getInstance().getExecutionController(vWorkspace.getWorkspace().getId());
List<HNode> accessibleHNodes = f.getHNode(hNodeId).getHNodesAccessibleList(f);
List<HNode> nodesWithNestedTable = new ArrayList<HNode>();
Map<String, String> hNodeIdtoColumnNameMap = new HashMap<String, String>();
for (HNode accessibleHNode : accessibleHNodes) {
if (accessibleHNode.hasNestedTable()) {
nodesWithNestedTable.add(accessibleHNode);
} else {
hNodeIdtoColumnNameMap.put(accessibleHNode.getId(), accessibleHNode.getColumnName());
}
}
accessibleHNodes.removeAll(nodesWithNestedTable);
PythonTransformationHelper pyHelper = new PythonTransformationHelper();
Map<String, String> normalizedColumnNameMap = new HashMap<String, String>();
String clsStmt = pyHelper.getPythonClassCreationStatement(worksheet, normalizedColumnNameMap, accessibleHNodes);
String transformMethodStmt = pyHelper.getPythonTransformMethodDefinitionState(worksheet, transformationCode);
String columnNameDictStmt = pyHelper.getColumnNameDictionaryStatement(normalizedColumnNameMap);
String defGetValueStmt = pyHelper.getGetValueDefStatement(normalizedColumnNameMap);
// Create a map from hNodeId to normalized column name
Map<String, String> hNodeIdToNormalizedColumnName = new HashMap<String, String>();
for (HNode accHNode : accessibleHNodes) {
hNodeIdToNormalizedColumnName.put(accHNode.getId(), normalizedColumnNameMap.get(accHNode.getColumnName()));
}
try {
// Prepare the Python interpreter
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec(pyHelper.getImportStatements());
interpreter.exec(clsStmt);
interpreter.exec(columnNameDictStmt);
interpreter.exec(defGetValueStmt);
interpreter.exec(transformMethodStmt);
Collection<Node> nodes = new ArrayList<Node>();
worksheet.getDataTable().collectNodes(hNode.getHNodePath(f), nodes);
Map<String, String> rowToValueMap = new HashMap<String, String>();
// Go through all nodes collected for the column with given hNodeId
for (Node node : nodes) {
Row row = node.getBelongsToRow();
Map<String, String> values = node.getColumnValues();
StringBuilder objectCreationStmt = new StringBuilder();
objectCreationStmt.append("r = " + pyHelper.normalizeString(worksheet.getTitle()) + "(");
int keyCounter = 0;
for (String valHNodeId : values.keySet()) {
String nodeId = values.get(valHNodeId);
String nodeVal = f.getNode(nodeId).getValue().asString();
if (keyCounter++ != 0)
objectCreationStmt.append(",");
if (nodeVal == null || nodeVal.isEmpty()) {
objectCreationStmt.append(hNodeIdToNormalizedColumnName.get(valHNodeId) + "=\"\"");
} else {
nodeVal = nodeVal.replaceAll("\"", "\\\\\"");
nodeVal = nodeVal.replaceAll("\n", " ");
objectCreationStmt.append(hNodeIdToNormalizedColumnName.get(valHNodeId) + "=\"" + nodeVal + "\"");
}
}
objectCreationStmt.append(")");
interpreter.exec(objectCreationStmt.toString());
try {
PyObject output = interpreter.eval("transform(r)");
// Execution ran successfully, put the value returned in the HashMap
rowToValueMap.put(row.getId(), pyHelper.getPyObjectValueAsString(output));
} catch (PyException p) {
p.printStackTrace();
rowToValueMap.put(row.getId(), errorDefaultValue);
} catch (Exception t) {
t.printStackTrace();
logger.debug("Error occured while transforming.", t);
rowToValueMap.put(row.getId(), errorDefaultValue);
}
}
// Invoke the add column command
JSONArray addColumnInput = getAddColumnCommandInputJSON();
AddColumnCommandFactory addColumnFac = (AddColumnCommandFactory) ctrl.getCommandFactoryMap().get(AddColumnCommand.class.getSimpleName());
addColCmd = (AddColumnCommand) addColumnFac.createCommand(addColumnInput, vWorkspace);
addColCmd.saveInHistory(false);
addColCmd.doIt(vWorkspace);
// Invoke the MultipleValueEditColumnCommand
JSONArray multiCellEditInput = getMultiCellValueEditInputJSON(rowToValueMap, addColCmd.getNewHNodeId());
MultipleValueEditColumnCommandFactory mfc = (MultipleValueEditColumnCommandFactory) ctrl.getCommandFactoryMap().get(MultipleValueEditColumnCommand.class.getSimpleName());
MultipleValueEditColumnCommand mvecc = (MultipleValueEditColumnCommand) mfc.createCommand(multiCellEditInput, vWorkspace);
mvecc.doIt(vWorkspace);
} catch (Exception e) {
e.printStackTrace();
logger.error("Error occured during python transformation.", e);
return new UpdateContainer(new ErrorUpdate("Error occured while applying Python transformation to the column."));
}
// Prepare the output container
UpdateContainer c = new UpdateContainer();
vWorkspace.getViewFactory().updateWorksheet(vWorksheetId, worksheet, worksheet.getHeaders().getAllPaths(), vWorkspace);
vWorkspace.getViewFactory().getVWorksheet(this.vWorksheetId).update(c);
/** Add the alignment update **/
addAlignmentUpdate(c, vWorkspace, worksheet);
c.add(new InfoUpdate("Transformation complete"));
return c;
}Example 65
| Project: SikuliX-2014-master File: JythonScriptRunner.java View source code |
private boolean doRedirect(PipedInputStream[] pin) {
PythonInterpreter py = getInterpreter();
Debug.saveRedirected(System.out, System.err);
try {
PipedOutputStream pout = new PipedOutputStream(pin[0]);
PrintStream ps = new PrintStream(pout, true);
if (!ScriptingSupport.systemRedirected) {
System.setOut(ps);
}
py.setOut(ps);
} catch (Exception e) {
log(-1, "%s: redirect STDOUT: %s", getName(), e.getMessage());
return false;
}
try {
PipedOutputStream eout = new PipedOutputStream(pin[1]);
PrintStream eps = new PrintStream(eout, true);
if (!ScriptingSupport.systemRedirected) {
System.setErr(eps);
}
py.setErr(eps);
} catch (Exception e) {
log(-1, "%s: redirect STDERR: %s", getName(), e.getMessage());
return false;
}
return true;
}Example 66
| Project: javasec-master File: RmPyServlet.java View source code |
protected static PythonInterpreter createInterpreter(ServletContext servletContext) { String rootPath = getRootPath(servletContext); PySystemState sys = new PySystemState(); //qb-rm sys.setdefaultencoding(RmConfig.getSingleton().getDefaultEncode()); PythonInterpreter interp = new PythonInterpreter(Py.newStringMap(), sys); sys.path.append(new PyString(rootPath)); String modulesDir = rootPath + "WEB-INF" + File.separator + "jython"; sys.path.append(new PyString(modulesDir)); return interp; }
Example 67
| Project: jCAE-master File: AlgoAction.java View source code |
private void runInSameVM(List<String> args, File pyFile, final InputOutput io) {
final ProgressHandle ph = ProgressHandleFactory.createHandle(getName());
Logger root = Logger.getLogger("org.jcae.mesh");
final JCAEFormatter jcaeFormatter = new JCAEFormatter();
//getting and redirecting logs from Bora mesher
root.setLevel(Level.INFO);
Handler redirector = new Handler() {
@Override
public void publish(LogRecord record) {
io.getOut().print(getFormatter().format(record));
}
@Override
public void close() throws SecurityException {
}
@Override
public void flush() {
}
};
try {
root.addHandler(redirector);
for (Handler h : root.getHandlers()) {
h.setFormatter(jcaeFormatter);
}
PythonInterpreter interp = new PythonInterpreter();
interp.setOut(io.getOut());
interp.setIn(io.getIn());
interp.setErr(io.getErr());
interp.set("args", args);
interp.exec("import sys");
interp.exec("sys.argv.extend(args)");
ph.start();
interp.execfile(pyFile.getPath());
} finally {
root.removeHandler(redirector);
ph.finish();
}
}Example 68
| Project: squale-master File: FormulaInterpreter.java View source code |
/**
* Vérification de la syntaxe d'une formule
*
* @param pFormula formule
* @throws FormulaException si erreur
*/
public void checkSyntax(AbstractFormulaBO pFormula) throws FormulaException {
FormulaConverter converter = new FormulaConverter();
String result = converter.convertFormula(pFormula);
PythonInterpreter inter = getInterpreter();
try {
inter.exec(result);
} catch (PyException e) {
throw new FormulaException(e);
}
// Test des paramètres utilisés
MeasureBOFactory factory = new MeasureBOFactory();
MeasureBO[] measures = new MeasureBO[pFormula.getMeasureKinds().size()];
Iterator measureKinds = pFormula.getMeasureKinds().iterator();
int i = 0;
while (measureKinds.hasNext()) {
measures[i] = factory.createMeasure(pFormula.getComponentLevel(), (String) measureKinds.next());
i++;
}
checkParameters(pFormula, measures);
}Example 69
| Project: DrupalLoadTest-master File: TestTraditionalJythonInstrumenter.java View source code |
@Test
public void testCreateProxyWithNonWrappableParameters() throws Exception {
// The types that can be wrapped depend on the Instrumenter.
// Can't wrap arrays.
assertNotWrappableByThisInstrumenter(new int[] { 1, 2, 3 });
assertNotWrappableByThisInstrumenter(new Object[] { "foo", new Object() });
// Can't wrap strings.
assertNotWrappableByThisInstrumenter("foo bah");
// Can't wrap numbers.
assertNotWrappableByThisInstrumenter(new Long(56));
assertNotWrappableByThisInstrumenter(new Integer(56));
assertNotWrappableByThisInstrumenter(new Short((short) 56));
assertNotWrappableByThisInstrumenter(new Byte((byte) 56));
final PythonInterpreter interpreter = getInterpretter();
// Can't wrap PyInteger.
interpreter.exec("x=1");
assertNotWrappable(interpreter.get("x"));
// Can't wrap PyClass.
interpreter.exec("class Foo: pass");
assertNotWrappable(interpreter.get("Foo"));
// Can't wrap None.
assertNotWrappableByThisInstrumenter(null);
}Example 70
| Project: PonIC-master File: JythonScriptEngine.java View source code |
@Override
public void registerFunctions(String path, String namespace, PigContext pigContext) throws IOException {
Interpreter.setMain(false);
Interpreter.init(path, pigContext);
pigContext.scriptJars.add(getJarPath(PythonInterpreter.class));
PythonInterpreter pi = Interpreter.interpreter;
@SuppressWarnings("unchecked") List<PyTuple> locals = ((PyStringMap) pi.getLocals()).items();
namespace = (namespace == null) ? "" : namespace + NAMESPACE_SEPARATOR;
try {
for (PyTuple item : locals) {
String key = (String) item.get(0);
Object value = item.get(1);
FuncSpec funcspec = null;
if (!key.startsWith("__") && !key.equals("schemaFunction") && !key.equals("outputSchema") && !key.equals("outputSchemaFunction") && (value instanceof PyFunction) && (((PyFunction) value).__findattr__("schemaFunction") == null)) {
PyObject obj = ((PyFunction) value).__findattr__("outputSchema");
if (obj != null) {
Utils.getSchemaFromString(obj.toString());
}
funcspec = new FuncSpec(JythonFunction.class.getCanonicalName() + "('" + path + "','" + key + "')");
pigContext.registerFunction(namespace + key, funcspec);
LOG.info("Register scripting UDF: " + namespace + key);
}
}
} catch (ParserException pe) {
throw new IOException("Error parsing schema for script function from the decorator", pe);
}
pigContext.addScriptFile(path);
Interpreter.setMain(true);
}Example 71
| Project: pploader-master File: PythonPluginLoader.java View source code |
private Plugin loadPlugin(File file, boolean ignoreSoftDependencies, PluginDataFile data) throws InvalidPluginException /*, InvalidDescriptionException, UnknownDependencyException*/
{
Properties props;
System.out.println("[PPLoader] Loading Plugin " + file.getName());
PythonPlugin result = null;
PluginDescriptionFile description = null;
try {
InputStream stream = data.getStream("plugin.yml");
if (stream == null) {
throw new InvalidPluginException(new Exception("You must include plugin.yml!"));
}
description = new PluginDescriptionFile(stream);
if (stream != null)
stream.close();
} catch (IOException ex) {
throw new InvalidPluginException(ex);
} catch (YAMLException ex) {
throw new InvalidPluginException(ex);
} catch (InvalidDescriptionException ex) {
throw new InvalidPluginException(ex);
}
File dataFolder = new File(file.getParentFile(), description.getName());
if (dataFolder.getAbsolutePath().equals(file.getAbsolutePath())) {
throw new InvalidPluginException(new Exception(String.format("Projected datafolder: '%s' for %s is the same file as the plugin itself (%s)", dataFolder, description.getName(), file)));
}
if (dataFolder.exists() && !dataFolder.isDirectory()) {
throw new InvalidPluginException(new Exception(String.format("Projected datafolder: '%s' for %s (%s) exists and is not a directory", dataFolder, description.getName(), file)));
}
List<String> depend;
try {
depend = description.getDepend();
if (depend == null) {
depend = new ArrayList<>();
}
} catch (ClassCastException ex) {
throw new InvalidPluginException(ex);
}
for (String pluginName : depend) {
if (!isPluginLoaded(pluginName)) {
throw new UnknownDependencyException(pluginName);
}
}
props = PySystemState.getBaseProperties();
props = setDefaultPythonPath(props, file.getAbsolutePath());
PySystemState state = new PySystemState();
state.initialize(System.getProperties(), props, null);
PyList pythonpath = state.path;
PyString filepath = new PyString(file.getAbsolutePath());
pythonpath.append(filepath);
String mainfile = "plugin.py";
InputStream instream = null;
try {
instream = data.getStream(mainfile);
if (instream == null) {
mainfile = "main.py";
instream = data.getStream(mainfile);
}
} catch (IOException e) {
throw new InvalidPluginException(e);
}
if (instream == null) {
throw new InvalidPluginException(new FileNotFoundException("Can not find plugin.py or main.py"));
}
try {
PyDictionary table = new PyDictionary();
PythonInterpreter interp = new PythonInterpreter(table, state);
String[] pre_plugin_scripts = { "preload.py" };
String[] post_plugin_scripts = { "postload.py" };
// Run scripts designed to be run before plugin creation
for (String script : pre_plugin_scripts) {
InputStream metastream = this.getClass().getClassLoader().getResourceAsStream("scripts/" + script);
interp.execfile(metastream);
metastream.close();
}
interp.execfile(instream);
instream.close();
String mainclass = description.getMain();
PyObject pyClass = interp.get(mainclass);
if (pyClass == null)
pyClass = interp.get("Plugin");
if (pyClass == null) {
throw new InvalidPluginException(new Exception("Can not find Mainclass."));
} else
result = (PythonPlugin) pyClass.__call__().__tojava__(PythonPlugin.class);
interp.set("PYPLUGIN", result);
result.interp = interp;
// Run scripts designed to be run after plugin creation
for (String script : post_plugin_scripts) {
InputStream metastream = this.getClass().getClassLoader().getResourceAsStream("scripts/" + script);
interp.execfile(metastream);
metastream.close();
}
result.initialize(this, server, description, dataFolder, file);
result.setDataFile(data);
} catch (Throwable t) {
throw new InvalidPluginException(t);
}
if (!loadedplugins.contains(description.getName()))
loadedplugins.add(description.getName());
return result;
}Example 72
| Project: flare-spork-master File: JythonScriptEngine.java View source code |
@Override
public void registerFunctions(String path, String namespace, PigContext pigContext) throws IOException {
Interpreter.setMain(false);
Interpreter.init(path, pigContext);
pigContext.scriptJars.add(getJarPath(PythonInterpreter.class));
PythonInterpreter pi = Interpreter.interpreter;
@SuppressWarnings("unchecked") List<PyTuple> locals = ((PyStringMap) pi.getLocals()).items();
namespace = (namespace == null) ? "" : namespace + NAMESPACE_SEPARATOR;
try {
for (PyTuple item : locals) {
String key = (String) item.get(0);
Object value = item.get(1);
FuncSpec funcspec = null;
if (!key.startsWith("__") && !key.equals("schemaFunction") && !key.equals("outputSchema") && !key.equals("outputSchemaFunction") && (value instanceof PyFunction) && (((PyFunction) value).__findattr__("schemaFunction") == null)) {
PyObject obj = ((PyFunction) value).__findattr__("outputSchema");
if (obj != null) {
Utils.getSchemaFromString(obj.toString());
}
funcspec = new FuncSpec(JythonFunction.class.getCanonicalName() + "('" + path + "','" + key + "')");
pigContext.registerFunction(namespace + key, funcspec);
LOG.info("Register scripting UDF: " + namespace + key);
}
}
} catch (ParserException pe) {
throw new IOException("Error parsing schema for script function from the decorator", pe);
}
pigContext.addScriptFile(path);
Interpreter.setMain(true);
}Example 73
| Project: pig-master File: JythonScriptEngine.java View source code |
@Override
public void registerFunctions(String path, String namespace, PigContext pigContext) throws IOException {
Interpreter.setMain(false);
Interpreter.init(path, pigContext);
pigContext.addScriptJar(getJarPath(PythonInterpreter.class));
PythonInterpreter pi = Interpreter.interpreter;
@SuppressWarnings("unchecked") List<PyTuple> locals = ((PyStringMap) pi.getLocals()).items();
namespace = (namespace == null) ? "" : namespace + NAMESPACE_SEPARATOR;
try {
for (PyTuple item : locals) {
String key = (String) item.get(0);
Object value = item.get(1);
FuncSpec funcspec = null;
if (!key.startsWith("__") && !key.equals("schemaFunction") && !key.equals("outputSchema") && !key.equals("outputSchemaFunction") && (value instanceof PyFunction) && (((PyFunction) value).__findattr__("schemaFunction") == null)) {
PyObject obj = ((PyFunction) value).__findattr__("outputSchema");
if (obj != null) {
Utils.getSchemaFromString(obj.toString());
}
funcspec = new FuncSpec(JythonFunction.class.getCanonicalName() + "('" + path + "','" + key + "')");
pigContext.registerFunction(namespace + key, funcspec);
LOG.info("Register scripting UDF: " + namespace + key);
}
}
} catch (ParserException pe) {
throw new IOException("Error parsing schema for script function from the decorator", pe);
}
pigContext.addScriptFile(path);
Interpreter.setMain(true);
}Example 74
| Project: spork-master File: JythonScriptEngine.java View source code |
@Override
public void registerFunctions(String path, String namespace, PigContext pigContext) throws IOException {
Interpreter.setMain(false);
Interpreter.init(path, pigContext);
pigContext.addScriptJar(getJarPath(PythonInterpreter.class));
PythonInterpreter pi = Interpreter.interpreter;
@SuppressWarnings("unchecked") List<PyTuple> locals = ((PyStringMap) pi.getLocals()).items();
namespace = (namespace == null) ? "" : namespace + NAMESPACE_SEPARATOR;
try {
for (PyTuple item : locals) {
String key = (String) item.get(0);
Object value = item.get(1);
FuncSpec funcspec = null;
if (!key.startsWith("__") && !key.equals("schemaFunction") && !key.equals("outputSchema") && !key.equals("outputSchemaFunction") && (value instanceof PyFunction) && (((PyFunction) value).__findattr__("schemaFunction") == null)) {
PyObject obj = ((PyFunction) value).__findattr__("outputSchema");
if (obj != null) {
Utils.getSchemaFromString(obj.toString());
}
funcspec = new FuncSpec(JythonFunction.class.getCanonicalName() + "('" + path + "','" + key + "')");
pigContext.registerFunction(namespace + key, funcspec);
LOG.info("Register scripting UDF: " + namespace + key);
}
}
} catch (ParserException pe) {
throw new IOException("Error parsing schema for script function from the decorator", pe);
}
pigContext.addScriptFile(path);
Interpreter.setMain(true);
}Example 75
| Project: spork-streaming-master File: JythonScriptEngine.java View source code |
@Override
public void registerFunctions(String path, String namespace, PigContext pigContext) throws IOException {
Interpreter.setMain(false);
Interpreter.init(path, pigContext);
pigContext.scriptJars.add(getJarPath(PythonInterpreter.class));
PythonInterpreter pi = Interpreter.interpreter;
@SuppressWarnings("unchecked") List<PyTuple> locals = ((PyStringMap) pi.getLocals()).items();
namespace = (namespace == null) ? "" : namespace + NAMESPACE_SEPARATOR;
try {
for (PyTuple item : locals) {
String key = (String) item.get(0);
Object value = item.get(1);
FuncSpec funcspec = null;
if (!key.startsWith("__") && !key.equals("schemaFunction") && !key.equals("outputSchema") && !key.equals("outputSchemaFunction") && (value instanceof PyFunction) && (((PyFunction) value).__findattr__("schemaFunction") == null)) {
PyObject obj = ((PyFunction) value).__findattr__("outputSchema");
if (obj != null) {
Utils.getSchemaFromString(obj.toString());
}
funcspec = new FuncSpec(JythonFunction.class.getCanonicalName() + "('" + path + "','" + key + "')");
pigContext.registerFunction(namespace + key, funcspec);
LOG.info("Register scripting UDF: " + namespace + key);
}
}
} catch (ParserException pe) {
throw new IOException("Error parsing schema for script function from the decorator", pe);
}
pigContext.addScriptFile(path);
Interpreter.setMain(true);
}Example 76
| Project: myrobotlab-master File: Python.java View source code |
/**
* Get a compiled version of the python call.
*
* @param name
* @param code
* @param interp
* @return
*/
private static synchronized PyObject getCompiledMethod(String name, String code, PythonInterpreter interp) {
// improve concurrent performance
if (objectCache.containsKey(name)) {
return objectCache.get(name);
}
PyObject compiled = interp.compile(code);
if (objectCache.size() > 25) {
// keep the size to 6
objectCache.remove(objectCache.keySet().iterator().next());
}
objectCache.put(name, compiled);
return compiled;
}Example 77
| Project: sod-master File: SodUtil.java View source code |
public static synchronized Object loadJython(String tagName, String[] armNames, Element config) throws Exception {
Class mustImplement = load(tagName.substring("jython".length()), armNames);
if (getElement(config, "inline") != null) {
String jythonCode = getNestedText(getElement(config, "inline"));
return inlineJython(tagName + pythonClassNum++, mustImplement, jythonCode);
} else {
String moduleName = getNestedText(SodUtil.getElement(config, "module"));
String className = getNestedText(SodUtil.getElement(config, "class"));
PythonInterpreter interp = getPythonInterpreter();
interp.exec("from " + moduleName + " import " + className);
PyObject jyWaveformProcessClass = interp.get(className);
PyObject pyWaveformProcessObj = jyWaveformProcessClass.__call__(PyJavaType.wrapJavaObject(config));
return mustImplement.cast(pyWaveformProcessObj.__tojava__(mustImplement));
}
}Example 78
| Project: the-fascinator-master File: ReIndexClient.java View source code |
/**
* Evaluate the requested python script and return the resulting object for
* later user.
*
* @param script The path to the script's location
* @return PyObject An evaluated PyObject, null for errors
*
*/
private PyObject evalScript(String script) {
// Find the script file
File file = new File(script);
if (file == null || !file.exists()) {
log.error("Could not find script: '{}'", script);
return null;
}
// Open the file for reading
FileInputStream inStream = null;
try {
inStream = new FileInputStream(file);
} catch (Exception ex) {
log.error("Error accessing script: '{}'", script, ex);
return null;
}
// Run it though an interpreter
PythonInterpreter python = null;
try {
python = PythonInterpreter.threadLocalStateInterpreter(null);
python.execfile(inStream, "scriptname");
} catch (Exception ex) {
log.error("Error evaluating Python script: '{}'", script, ex);
return null;
}
// Get the result and cleanup
PyObject scriptClass = null;
try {
scriptClass = python.get(SCRIPT_CLASS_NAME);
python.cleanup();
} catch (Exception ex) {
log.error("Error accessing class: '{}'", SCRIPT_CLASS_NAME, ex);
return null;
}
// Instantiate and return the result
return scriptClass.__call__();
}Example 79
| Project: zenoss-zep-master File: TriggerPlugin.java View source code |
private synchronized void initialize() {
if (initialized) {
return;
}
logger.info("Initializing Jython");
this.initialized = true;
PythonInterpreter.initialize(System.getProperties(), new Properties(), new String[0]);
this.python = new PythonInterpreter();
// define basic infrastructure class for evaluating rules
this.python.exec("class DictAsObj(object):\n" + " def __init__(self, **kwargs):\n" + " for k,v in kwargs.iteritems(): setattr(self,k,v)");
// expose to Java a Python dict->DictAsObj conversion function
this.toObject = (PyFunction) this.python.eval("lambda dd : DictAsObj(**dd)");
logger.info("Completed Jython initialization");
}Example 80
| Project: jspringbot-master File: RunnerPyObjectFactory.java View source code |
public static PyObject createRunnerPyObject(PythonInterpreter interpreter) {
interpreter.exec("import robot; from robot.jarrunner import JarRunner");
return interpreter.get("JarRunner");
}Example 81
| Project: Single-JAR-Jython-Example-master File: Main.java View source code |
public static void main(String[] args) throws PyException {
PythonInterpreter intrp = new PythonInterpreter();
intrp.exec("import excel_example");
intrp.exec("excel_example.main()");
}Example 82
| Project: SeanMovieManager-master File: JythonTest.java View source code |
@Test
public void testFirstJythonScript() throws Exception {
StringBuffer sb = new StringBuffer();
sb.append("import random\n");
sb.append("print random.random()\n");
new PythonInterpreter().exec(sb.toString());
}Example 83
| Project: sikuli-monkey-master File: MonkeyPlugin.java View source code |
@Override
public boolean apply(PythonInterpreter anInterpreter) {
anInterpreter.exec(readInitScript());
return true;
}Example 84
| Project: montysolr-master File: TestJythonWeakref.java View source code |
@Ignore
@Test
public void test() {
PythonInterpreter interp = new PythonInterpreter();
interp.exec("import weakref");
}Example 85
| Project: spudplayer-master File: PythonConsole.java View source code |
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import scripts");
interpreter.exec("import external.JythonConsole.console");
interpreter.exec("external.JythonConsole.console.main(locals())");
}Example 86
| Project: freemarker-old-master File: UnlinkedJythonOperationsImpl.java View source code |
public void execute(String script, Map vars) throws BuildException {
PythonInterpreter pi = createInterpreter(vars);
pi.exec(script);
}Example 87
| Project: incubator-freemarker-master File: UnlinkedJythonOperationsImpl.java View source code |
public void execute(String script, Map vars) throws BuildException {
PythonInterpreter pi = createInterpreter(vars);
pi.exec(script);
}Example 88
| Project: openge-master File: ScriptingManager.java View source code |
private static PyObject getMethod(String path, String module, String method) {
// Ziggy: This could probably be moved to callScript()
// since it's not used elsewhere.
PythonInterpreter python = new PythonInterpreter();
python.cleanup();
python.execfile(path + module + ".py");
return python.get(method);
}Example 89
| Project: elasticsearch-inout-plugin-master File: DocTest.java View source code |
private void resetInterpreter() {
interp = new PythonInterpreter(null, new PySystemState());
sys = Py.getSystemState();
}Example 90
| Project: qafe-platform-master File: PythonScriptEngine.java View source code |
public synchronized void init() {
if (interp == null) {
PythonInterpreter.initialize(System.getProperties(), new Properties(), new String[] {});
interp = new PythonInterpreter();
interp.exec("import sys");
}
}Example 91
| Project: HermesJMS-master File: JythonManager.java View source code |
public PythonInterpreter getInterpreter() {
return jyConsole.getPythonInterpreter();
}Example 92
| Project: robotframework-selenium2library-java-master File: RunOnFailure.java View source code |
@Override protected PythonInterpreter initialValue() { PythonInterpreter pythonInterpreter = new PythonInterpreter(); pythonInterpreter.exec("from robot.libraries.BuiltIn import BuiltIn; from robot.running.context import EXECUTION_CONTEXTS; BIN = BuiltIn();"); return pythonInterpreter; }
Example 93
| Project: VisAD-master File: Util.java View source code |
/**
* Test whether Jython is present in this JVM.
* @return true if found, otherwise false
*/
public static boolean canDoPython() {
return canDoClass("org.python.util.PythonInterpreter") != null;
}