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 (!ScriptRunner.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 (!ScriptRunner.systemRedirected) { System.setErr(eps); } py.setErr(eps); } catch (Exception e) { log(-1, "%s: redirect STDERR: %s", getName(), e.getMessage()); return false; } return true; }
// likely want to reuse PySystemState in some clever fashion since expensive to setup... public JythonObjectFactory( PythonInterpreter interpreter, Class<? extends T> interfaceType, String script) { this.interfaceType = interfaceType; pythonInterpreter = interpreter; pythonInterpreter.exec(FileUtil.wrap(IOUtils.toInputStream(script))); klass = pythonInterpreter.get("rep"); }
private void convertSrcToHtml(String bundle) { PythonInterpreter py = new PythonInterpreter(); log(lvl, "Convert Sikuli source code " + bundle + " to HTML"); py.set("local_convert", true); py.set("sikuli_src", bundle); py.exec(pyConverter); }
public String markdownToHtml(String chaine) { PythonInterpreter console = getMdBox().getPyconsole(); console.set("text", chaine); console.exec( "render = Markdown(extensions=(ZdsExtension({'inline': False, 'emoticons': smileys}),),safe_mode = 'escape', enable_attributes = False, tab_length = 4, output_format = 'html5', smart_emphasis = True, lazy_ol = True).convert(text)"); PyString render = console.get("render", PyString.class); return render.toString(); }
@Test public void raw_customFormatter() { 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!\"))"); RFormatter rFormatter = new RFormatter(); interpreter.set("out", rFormatter); // Simple use Pygments as you would in Python interpreter.exec( "" + "from pygments import highlight\n" + "from pygments.lexers.jvm import ClojureLexer\n" + "from pygments.formatter import Formatter\n" + "\n" + "class ForwardFormatter(Formatter):\n" + " def format(self, tokensource, outfile):\n" + " for ttype, value in tokensource:\n" + " out.write(ttype, value)\n" + "\n" + "highlight(code, ClojureLexer(), ForwardFormatter())"); assertThat(rFormatter.out.toString()) .isEqualTo( "" + "Token.Punctuation:[(']\n" + "Token.Keyword.Declaration:[defn ']\n" + "Token.Name.Variable:[year-end-evaluation']\n" + "Token.Text:[\\n ']\n" + "Token.Punctuation:[[']\n" + "Token.Punctuation:[]']\n" + "Token.Text:[\\n ']\n" + "Token.Punctuation:[(']\n" + "Token.Keyword:[if ']\n" + "Token.Punctuation:[(']\n" + "Token.Name.Builtin:[> ']\n" + "Token.Punctuation:[(']\n" + "Token.Name.Function:[rand']\n" + "Token.Punctuation:[)']\n" + "Token.Text:[ ']\n" + "Token.Literal.Number.Float:[0.5']\n" + "Token.Punctuation:[)']\n" + "Token.Text:[\\n ']\n" + "Token.Literal.String:[\"You get a raise!\"']\n" + "Token.Text:[\\n ']\n" + "Token.Literal.String:[\"Better luck next year!\"']\n" + "Token.Punctuation:[)']\n" + "Token.Punctuation:[)']\n" + "Token.Text:[\\n']\n"); }
private Test genTestSuite(String className, String filename, String bundlePath) throws IOException { // remove any "non-word" characters, i.e., leave only letters // that should ensure the python class name is syntatically valid className = className.replaceAll("\\W", ""); TestSuite ret = new TestSuite(className); PythonInterpreter interp = new PythonInterpreter(); String testCode = "# coding=utf-8\n" + "from __future__ import with_statement\n" + "import junit\n" + "from junit.framework.Assert import *\n" + "from sikuli.Sikuli import *\n" + "class " + className + " (junit.framework.TestCase):\n" + "\tdef __init__(self, name):\n" + "\t\tjunit.framework.TestCase.__init__(self,name)\n" + "\t\tself.theTestFunction = getattr(self,name)\n" + "\t\tsetBundlePath('" + bundlePath + "')\n" + "\tdef runTest(self):\n" + "\t\tself.theTestFunction()\n"; BufferedReader in = new BufferedReader(new FileReader(filename)); String line; // int lineNo = 0; // Pattern patDef = Pattern.compile("def\\s+(\\w+)\\s*\\("); while ((line = in.readLine()) != null) { // lineNo++; testCode += "\t" + line + "\n"; /* Matcher matcher = patDef.matcher(line); if(matcher.find()){ String func = matcher.group(1); Debug.log("Parsed " + lineNo + ": " + func); _lineNoOfTest.put( func, lineNo ); } */ } interp.exec(testCode); PyList tests = (PyList) interp.eval( "[" + className + "(f) for f in dir(" + className + ") if f.startswith(\"test\")]"); while (tests.size() > 0) { PyObject t = tests.pop(); Test t2 = (Test) (t).__tojava__(TestCase.class); ret.addTest(t2); } return ret; }
/** * Creates a formatter using the provided Python code snippet. Within the snippet, the following * variables are available: * <li>o: a proxy to the reconstituted object. Use o.n to access the value of field n of o. * Example: <code> * return "x: "+o.x+", y: "+o.y * </code> */ public IPyObjectFormatter createFormatter(String aFunctionBody) { // initFactory(); // Temp. This is for debugging. String theImports = "from java.util import *\n"; String theDef = theImports + "def func(o):\n" + Utils.indent(aFunctionBody, 1, "\t"); itsInterpreter.exec(theDef); PyFunction theFunction = (PyFunction) itsInterpreter.get("func"); return itsFactory.createFormatter(theFunction); }
public Object evaluateString( LocalhostConnector connector, Path locator, String content, String name, int lineno, Object security) throws Exception { final PyCode code = interpreter.compile(content, name); return interpreter.eval(code); }
@Override public void clear() { HashMap<String, Object> bindings = getBindings(); for (String s : bindings.keySet()) { bindings.put(s, null); py.set(s, null); } // let Jython do its housekeeping py.cleanup(); }
public SoSReport(PythonInterpreter interpreter, String pyLocation) { interpreter.exec("import sys"); interpreter.exec("sys.path.append(\"" + pyLocation + "\")"); interpreter.exec("from sos.sosreport import SoSReport"); interpreter.exec("reporter = SoSReport([])"); this.sosreport = interpreter.get("reporter"); enableOption("--batch"); enableOption("--report"); enableOption("--silent"); }
@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"); }
public Object evaluateReader( LocalhostConnector connector, Path locator, FileReader reader, String filename, int lineno, Object security) throws Exception { ScriptContext.get().setCurrentScriptPath(Paths.get(filename)); final PyCode code = interpreter.compile(reader, filename); return interpreter.eval(code); }
private void initFactory() { try { String theScript = Utils.readInputStream(FormatterFactory.class.getResourceAsStream("formatter.py")); itsInterpreter = new PythonInterpreter(); itsInterpreter.setLocals(new PyStringMap()); itsInterpreter.exec(theScript); PyObject f = itsInterpreter.get("factory"); itsFactory = (IPyFormatterFactory) f.__tojava__(IPyFormatterFactory.class); } catch (IOException e) { throw new RuntimeException(e); } }
private int runPython(File pyFile, String[] stmts, String[] scriptPaths) { int exitCode = 0; String stmt = ""; try { if (scriptPaths != null) { // TODO implement compile only if (isCompileOnly) { log(lvl, "runPython: running COMPILE_ONLY"); interpreter.compile(pyFile.getAbsolutePath()); } else { if (scriptPaths.length > 1) { String scr = FileManager.slashify(scriptPaths[0], true) + scriptPaths[1] + ".sikuli"; log(lvl, "runPython: running script from IDE: \n" + scr); interpreter.exec("sys.argv[0] = \"" + scr + "\""); } else { log(lvl, "runPython: running script: \n" + scriptPaths[0]); interpreter.exec("sys.argv[0] = \"" + scriptPaths[0] + "\""); } interpreter.execfile(pyFile.getAbsolutePath()); } } else { log(-1, "runPython: invalid arguments"); exitCode = -1; } } catch (Exception e) { java.util.regex.Pattern p = java.util.regex.Pattern.compile("SystemExit: ([0-9]+)"); Matcher matcher = p.matcher(e.toString()); // TODO error stop I18N if (matcher.find()) { exitCode = Integer.parseInt(matcher.group(1)); Debug.info("Exit code: " + exitCode); } else { // log(-1,_I("msgStopped")); if (null != pyFile) { exitCode = findErrorSource(e, pyFile.getAbsolutePath(), scriptPaths); } else { Debug.error("runPython: Python exception: %s with %s", e.getMessage(), stmt); } if (isFromIDE) { exitCode *= -1; } else { exitCode = 1; } } } return exitCode; }
@Override public void exec(PyObject s) { try { super.exec(s); } catch (PyException e) { throw new PyExceptionWrapper(e); } }
private PythonInterpreter getInterpreter() { if (interpreter == null) { sysargv.add(""); PythonInterpreter.initialize(System.getProperties(), null, sysargv.toArray(new String[0])); interpreter = new PythonInterpreter(); } return interpreter; }
@Override public void execfile(String s) { try { super.execfile(s); } catch (PyException e) { throw new PyExceptionWrapper(e); } }
@Override public void execfile(InputStream s, String name) { try { super.execfile(s, name); } catch (PyException e) { throw new PyExceptionWrapper(e); } }
public void evalFile(String s) { for (String s2 : bindings.keySet()) { py.set(s2, bindings.get(s2)); } py.setOut(getWriter()); py.setErr(getErrorWriter()); try { py.execfile(s); PyStringMap locals = (PyStringMap) py.getLocals(); Object[] values = locals.values().toArray(); Object[] keys = locals.keys().toArray(); bindings.clear(); for (int i = 0; i < keys.length; ++i) { bindings.put((String) keys[i], values[i]); } } catch (PyException pe) { getErrorWriter().write(pe.toString()); getErrorWriter().flush(); } }
protected PicoContainer createContainerFromScript( PicoContainer parentContainer, Object assemblyScope) { ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader pyClassLoader = Py.getSystemState().getClassLoader(); try { Thread.currentThread().setContextClassLoader(getClassLoader()); Py.getSystemState().setClassLoader(getClassLoader()); PythonInterpreter interpreter = new PythonInterpreter(); interpreter.set("parent", parentContainer); interpreter.set("assemblyScope", assemblyScope); interpreter.execfile(getScriptInputStream(), "picocontainer.py"); return (PicoContainer) interpreter.get("pico", PicoContainer.class); } catch (IOException e) { throw new ScriptedPicoContainerMarkupException(e); } finally { Thread.currentThread().setContextClassLoader(oldClassLoader); Py.getSystemState().setClassLoader(pyClassLoader); } }
/** * Executes the jythonscript * * @param pyFile The file containing the script * @param imagePath The directory containing the images * @param argv The arguments passed by the --args parameter * @param forIDE * @return The exitcode */ @Override public int runScript(File pyFile, File imagePath, String[] argv, String[] forIDE) { if (null == pyFile) { // run the Python statements from argv (special for setup functional test) executeScriptHeader(null); log(lvl, "runPython: running statements"); try { for (String e : argv) { interpreter.exec(e); } } catch (Exception ex) { log(-1, "runPython: raised: %s", ex.getMessage()); return -1; } return 0; } isFromIDE = !(forIDE == null); if (isFromIDE && forIDE.length > 1 && forIDE[0] != null) { isCompileOnly = forIDE[0].toUpperCase().equals(COMPILE_ONLY); } pyFile = new File(pyFile.getAbsolutePath()); fillSysArgv(pyFile, argv); if (forIDE == null) { executeScriptHeader(new String[] {pyFile.getParent(), pyFile.getParentFile().getParent()}); } else { executeScriptHeader(new String[] {forIDE[0]}); } int exitCode = 0; if (isFromIDE) { exitCode = runPython(pyFile, null, forIDE); } else { exitCode = runPython(pyFile, null, new String[] {pyFile.getParentFile().getAbsolutePath()}); } log(lvl + 1, "runScript: at exit: path:"); for (Object p : interpreter.getSystemState().path.toArray()) { log(lvl + 1, "runScript: " + p.toString()); } log(lvl + 1, "runScript: at exit: --- end ---"); return exitCode; }
/** Initialize the python interpreter state (paths, etc.) */ public static void initializer() { // Get preProperties postProperties, and System properties Properties postProps = new Properties(); Properties sysProps = System.getProperties(); // put System properties (those set with -D on the command line) in // postProps Enumeration<?> e = sysProps.propertyNames(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); if (name.startsWith("python.")) postProps.put(name, System.getProperty(name)); } // Here's the initialization step PythonInterpreter.initialize(sysProps, postProps, null); }
/** * Creates a jython object instance for the script cache. * * @param key the path to the jython script * @return an instantiated jython object * @throws Exception if the jython object failed to be instantiated */ @Override public Object createEntry(Object key) throws Exception { // log.debug("createEntry({})", key); String path = key.toString(); int qmarkPos = path.lastIndexOf("?"); if (qmarkPos != -1) { path = path.substring(0, qmarkPos); } int slashPos = path.indexOf("/"); String portalId = path.substring(0, slashPos); PyObject scriptObject = null; InputStream in = velocityService.getResource(path); if (in == null) { log.debug("Failed to load script: '{}'", path); } else { // add current and default portal directories to python sys.path addSysPaths(portalId, Py.getSystemState()); // setup the python interpreter PythonInterpreter python = PythonInterpreter.threadLocalStateInterpreter(null); // expose services for backward compatibility - deprecated python.set("Services", scriptingServices); // python.setLocals(scriptObject); JythonLogger jythonLogger = new JythonLogger(path); python.setOut(jythonLogger); python.setErr(jythonLogger); python.execfile(in, path); String scriptClassName = StringUtils.capitalize(FilenameUtils.getBaseName(path)) + "Data"; PyObject scriptClass = python.get(scriptClassName); if (scriptClass != null) { scriptObject = scriptClass.__call__(); } else { log.debug("Failed to find class '{}'", scriptClassName); } python.cleanup(); } return scriptObject; }
@Before public void setUpPythonInterpreter() throws Exception { out = new ByteArrayOutputStream(); pi = python.interpreter(); pi.setOut(out); }
@Override protected void removeFromRealEngine(String name) { py.set(name, null); }
@Override protected void putInRealEngine(String name, Object value) { py.set(name, value); }
@SuppressWarnings("nls") public static void main(String[] args) { try { // Launch Thread Watchdog // Checks for deadlocks new Thread( new Runnable() { @Override public void run() { ThreadMXBean bean = ManagementFactory.getThreadMXBean(); while (true) { long[] threadIds = bean.findDeadlockedThreads(); // Returns null if no threads are deadlocked. if (threadIds != null) { System.err.println("DEADLOCK"); System.err.println("========"); ThreadInfo[] infos = bean.getThreadInfo(threadIds); for (ThreadInfo info : infos) { System.err.println("STACK:"); StackTraceElement[] stack = info.getStackTrace(); for (StackTraceElement x : stack) { System.err.println(" " + x); } } System.exit(1); } try { Thread.sleep(1000); } catch (InterruptedException e) { // nop } } } }) .start(); // Create Java/Jython interface // ============================ 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); } } } } // Load operators // ============== 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); }
public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("from scripts import *"); interpreter.exec("import external.JythonConsole.console"); interpreter.exec("external.JythonConsole.console.main(locals())"); }
/** {@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] = new PyJavaInstance(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) { // Raise to caller throw (IOException) ex; } else if (ex instanceof ScriptCompilationException) { // Raise to caller throw (ScriptCompilationException) ex; } throw new ScriptCompilationException(ex.getMessage()); } } logger.error("No scriptInterfaces provided."); return null; }
void clear() { out = new ByteArrayOutputStream(); pi.setOut(out); }