Examples of PythonInterpreter


Examples of org.python.util.PythonInterpreter

    }
    String pythonClass = config.get("pythonClass");
    String pythonPackage = config.containsKey("pythonPackage") ?
      config.get("pythonPackage") : pythonClass;
   
    _interpreter = new PythonInterpreter();
    _interpreter.exec("import sys;");
    _interpreter.exec("sys.path.append('" + new File("lib").getAbsolutePath() + "')");
    _interpreter.exec("sys.path.append('" + new File("lib", "external").getAbsolutePath() + "')");
        _interpreter.exec("from " + pythonPackage + " import " + pythonClass);
        PyObject serviceClass = _interpreter.get(pythonClass);
View Full Code Here

Examples of org.python.util.PythonInterpreter

    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");
        }
View Full Code Here

Examples of org.python.util.PythonInterpreter

    this.resultFormatter = new JythonResultFormatter(logger);
  }

    public Data[] execute() {   
     
      PythonInterpreter interp = interpInitializer.initializeInterpreter(
          new PythonInterpreter(), data, parameters);
     
      List rawResults = runScript(interp, script);
     
      Data[] results  = resultFormatter.formatRawResults(rawResults,
          data, properties);
View Full Code Here

Examples of org.python.util.PythonInterpreter

import org.python.util.PythonInterpreter;
import org.python.core.*;

public class SimpleEmbedded {
    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");
    }
View Full Code Here

Examples of org.python.util.PythonInterpreter

    * Executes a given script. If useText is true the text passed will be run as a script
    * otherwise the script will be loaded from the URL an executed
    */
   public void runScript()
   {
      PythonInterpreter interp = getPythonInterpreter();
      interp.set("server", server);
      String script = null;
      if (useText)
      {
         script = scriptText;
      }
      else
      {
         try
         {
            script = loadStream(scriptFile.openStream());
         }
         catch (IOException e)
         {
            Logger log = getLogger();
            log.error("Exception during url opening", e);
         }
      }
      interp.exec(script);
   }
View Full Code Here

Examples of org.python.util.PythonInterpreter

   public static PythonInterpreter getPythonInterpreter()
   {
      if (interpreter == null)
      {
         interpreter = new PythonInterpreter();
         PySystemState sys = Py.getSystemState();
         sys.add_package("javax.management");
         sys.add_package("javax.management.loading");
         sys.add_package("javax.management.modelmbean");
         sys.add_package("javax.management.monitor");
View Full Code Here

Examples of org.python.util.PythonInterpreter

        logger.debug("evaluating script");
       
        try
        {
            logger.debug("creating Python interpreter");
            python = new PythonInterpreter();
            logger.trace("python: " + python);
           
            logger.debug("setting script bindings");
            for(String key : pairs.keySet())
            {
View Full Code Here

Examples of org.python.util.PythonInterpreter

        if (f.exists())
        {
            try
            {
                // We try to open the Py Interpreter
                PythonInterpreter interp = new PythonInterpreter();

                // Make sure the Py Interpreter use the right classloader
                // This is necessary for servlet engines generally has
                // their own classloader implementations and servlets aren't
                // loaded in the system classloader.  The python script will
                // load java package
                // org.apache.turbine.services.assemblerbroker.util.python;
                // the new classes to it as well.
                Py.getSystemState().setClassLoader(
                        this.getClass().getClassLoader());

                // We import the Python SYS module. Now we don't need to do this
                // explicitely in the script.  We always use the sys module to
                // do stuff like loading java package
                // org.apache.turbine.services.assemblerbroker.util.python;
                interp.exec("import sys");

                // Now we try to load the script file
                interp.execfile(confName);
                interp.execfile(fName.toString());

                try
                {
                    // We create an instance of the screen class from the
                    // python script
                    interp.exec("scr = " + name + "()");
                }
                catch (Throwable e)
                {
                    throw new Exception(
                        "\nCannot create an instance of the python class.\n"
                        + "You probably gave your class the wrong name.\n"
                        + "Your class should have the same name as your "
                        + "filename.\nFilenames should be all lowercase and "
                        + "classnames should start with a capital.\n"
                        + "Expected class name: " + name + "\n");
                }

                // Here we convert the python sceen instance to a java instance.
                assembler = (Assembler) interp.get("scr", Assembler.class);
            }
            catch (Exception e)
            {
                // We log the error here because this code is not widely tested
                // yet. After we tested the code on a range of platforms this
View Full Code Here

Examples of org.python.util.PythonInterpreter

            try {
                StringWriter output = new StringWriter();
                Properties properties = new Properties();
                properties.setProperty("python.home", StaticConfiguration.JYTHON_HOME);
                PythonInterpreter.initialize(System.getProperties(), properties, new String[]{""});
                PythonInterpreter interp = new PythonInterpreter(new org.python.core.PyStringMap(), new org.python.core.PySystemState());
                interp.setOut(output);
                interp.setErr(output);
                interp.cleanup();
                //java -cp %JYTHON_HOME%\jython.jar -Dpython.home=%JYTHON_HOME% -Dpython.path=%FORMATTER_DIR% org.python.util.jython %JYTHON_HOME%\Lib\pythondoc.py -f -s -Otestscriptdoc_xmlformatter -Dtestsuite_dir=%TEST_SUITE_DIR% !TEST_SCRIPTS!
                String args = "import sys;sys.argv[1:]= ['" + campaignFile +  "']";
                interp.exec(args);
                interp.exec("__name__ = '__main__'");
                interp.exec("execfile(r'" + StaticConfiguration.QTASTE_ROOT + "/tools/TestProcedureDoc/generateTestCampaignDoc.py')");
                interp.cleanup();
                interp = null;

            } catch (PyException e) {
                System.err.println("Exception occurs executing PythonInterpreter:" + e.value);
            }
View Full Code Here

Examples of org.python.util.PythonInterpreter

            StringWriter output = new StringWriter();
            Properties properties = new Properties();
            properties.setProperty("python.home", StaticConfiguration.JYTHON_HOME);
            properties.setProperty("python.path", StaticConfiguration.JYTHON_LIB);
            PythonInterpreter.initialize(System.getProperties(), properties, new String[]{""});
            PythonInterpreter interp = new PythonInterpreter(new org.python.core.PyStringMap(), new org.python.core.PySystemState());
            interp.setOut(output);
            interp.setErr(output);
            interp.cleanup();
            //java -cp %JYTHON_HOME%/jython.jar;%QTASTE_ROOT%/kernel/target/qtaste-kernel-deploy.jar -Dpython.home=%JYTHON_HOME% -Dpython.path=%FORMATTER_DIR% org.python.util.jython %JYTHON_HOME%\Lib\pythondoc.py -f -s -Otestscriptdoc_xmlformatter %TEST_SCRIPT% -V
            //java -cp %JYTHON_HOME%\jython.jar -Dpython.home=%JYTHON_HOME% -Dpython.path=%FORMATTER_DIR% org.python.util.jython %JYTHON_HOME%\Lib\pythondoc.py -f -s -Otestscriptdoc_xmlformatter -Dtestsuite_dir=%TEST_SUITE_DIR% !TEST_SCRIPTS!
            String args = "import sys;sys.argv[1:]= ['-V', '-f', '-s', '-Otestscriptdoc_xmlformatter', '" + testScriptFile.getAbsolutePath() + "']";
            interp.exec(args);
            interp.exec("__name__ = '__main__'");
            interp.exec("execfile(r'" + StaticConfiguration.JYTHON_HOME + "/Lib/pythondoc.py')");
            interp.cleanup();
            interp = null;
        } catch (Exception e) {
            System.err.println("Error executing PythonInterpreter " + e.getMessage());
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.