Package org.python.util

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 = (T) 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


        String testScript = getJythonTestScript();
        String testRoot = getJythonTestRoot();
        String xmlReportFile = getOptionalXmlReportFilename();
        String ignoreFile = getOptionalIgnoreFile();

        PythonInterpreter interp = createInterpreterWithArgs(xmlReportFile, ignoreFile);
        interp.getSystemState().path.insert(0, new PyString(testRoot));

        LOGGER.info("About to call Jython test script: '" + testScript
                + "' with '" + testRoot + "' added to Jython path");

        int maxInvocations = Integer.getInteger(TEST_INVOCATIONS_SYSTEM_PROPERTY, 1);
View Full Code Here

        if(Boolean.getBoolean(ALWAYS_COLORIZE_SYSTEM_PROPERTY))
        {
            systemState.argv.append(new PyString("--always-colorize"));
        }

        PythonInterpreter interp = new PythonInterpreter(null, systemState);
        return interp;
    }
View Full Code Here

     * @return the designated object, or null
     */
    public static PyObject getFromFile(String fileName, String objectName)
    {
        System.out.println("jythools: get "+objectName+" from file "+fileName);
        PythonInterpreter interp = new PythonInterpreter();
        interp.execfile(fileName);
        return interp.get(objectName);
    }
View Full Code Here

    // init. Jython
    PySystemState.initialize();
    PySystemState sys = Py.getSystemState();
    sys.path.append(new PyString(System.getProperty("user.dir") + File.separator + "web" + File.separator + "python"));
    jython = new PythonInterpreter(null, sys);
  }
View Full Code Here

    Options.classBasedExceptions = false;
      } catch (NoSuchFieldError e) {};
      if (hr.server.logLevel >= Server.LOG_DIAGNOSTIC) {
    Options.showJavaExceptions = true;
      }
      interp = new PythonInterpreter();

      String script = props.getProperty(hr.prefix + SCRIPT);
      // System.err.println("Creating Python interp");
      try {
    interp.set("prefix", hr.prefix);
View Full Code Here

      jythonProps.put( "python.home", tmps.get("jython.home") );
      PythonInterpreter.initialize( System.getProperties(), jythonProps, args );

      PyStringMap       dict  = new PyStringMap();
      PySystemState     pysys = new PySystemState();
      PythonInterpreter pi    = new PythonInterpreter( dict, pysys );


      // DO NOT use CommandLineUserMessageHandler here - causes infinite recursion
      MessageHandlerOutputStream  out  = new MessageHandlerOutputStream( UserMessageHandler.INFO,  rumh );
      MessageHandlerOutputStream  err  = new MessageHandlerOutputStream( UserMessageHandler.ERROR, rumh );

      // space out dots to avoid accidental replace on System DOT out search
      sysOut = System . out;
      sysErr = System . err;
   
      // set context if not null
      Object context = template.getContext();
      if( null != context ) {
        pi.set( "context", context );
      }

      System.setOut( new PrintStream( out ) );
      System.setErr( new PrintStream( err ) );

      pi.execfile( cwp );
     
      String jythonexec
        = "cw = "+cw+"()\n"
        + (null!=context?"cw._setContext( context )\n":"")
        + "cw.main( sys.argv )\n"
        + "result = cw._getResult()\n"
        ;
      pi.exec( jythonexec );
      WayPointRecorder.add( BasicWayPoint.ExecutingCodeWriter.make( template.getCodeWriterPath().getAbsolutePath() ) );

      Object result = pi.get("result");

      if( result instanceof Integer ) {
        successful = ( 0 == ((Integer)result).intValue() );
      }
      else {
View Full Code Here

public class Interpretor {
  private PyObject interpreterClass;

  private static Logger logger = LoggerFactory.getLogger(Interpretor.class);
  public Interpretor() {
    PythonInterpreter interpreter = new PythonInterpreter();
   
    String dirpathString = ServletContextParameterMap.getParameterValue(ContextParameter.WEBAPP_PATH) +
                  "/" + ServletContextParameterMap
                  .getParameterValue(ContextParameter.PYTHON_SCRIPTS_DIRECTORY);
   
                  ;
    if(dirpathString == null || dirpathString.toString().length() <= 1) {
      dirpathString = "../karma-web/src/main/webapp/resources/pythonCleaningscripts";
    }
    logger.info("Setting Python Scripts Directory for karma-cleaning: " + dirpathString);
   
    interpreter.exec("import sys");
    // /Users/bowu/projects/IDCT/src/edu/isi/karma/cleaning
    interpreter.exec("sys.path.append('" + dirpathString + "')");
    interpreter.exec("from FunctionList import *");
    interpreter.exec("from Interpreter import *");
    // interpreter.exec("print sys.path");
    interpreterClass = interpreter.get("Interpreter");
  }
View Full Code Here


    logger.debug("Executing PyTransform\n" + transformMethodStmt);

    // Prepare the Python interpreter
    PythonInterpreter interpreter = new PythonInterpreter();

    PythonRepository repo = PythonRepository.getInstance();
    repo.initializeInterperter(interpreter);
    repo.importUserScripts(interpreter);
   
    repo.compileAndAddToRepositoryAndExec(interpreter, transformMethodStmt);

    Collection<Node> nodes = new ArrayList<Node>(Math.max(1000, worksheet
        .getDataTable().getNumRows()));
    worksheet.getDataTable().collectNodes(hNode.getHNodePath(f), nodes, selection);

    Map<String, String> rowToValueMap = new HashMap<String, String>();

    int counter = 0;
    long starttime = System.currentTimeMillis();
    // Go through all nodes collected for the column with given hNodeId

    interpreter.set("workspaceid", workspace.getId());
    interpreter.set("command", this);
    interpreter.set("selectionName", selection.getName());
    PyCode py = repo.getTransformCode();

    int numRowsWithErrors = 0;

    for (Node node : nodes) {
      Row row = node.getBelongsToRow();

      interpreter.set("nodeid", node.getId());

      try {
        PyObject output = interpreter.eval(py);
        String transformedValue = PythonTransformationHelper
            .getPyObjectValueAsString(output);
        addTransformedValue(transformedRows, row, transformedValue);
      } catch (PyException p) {
        logger.info("error in evaluation python, skipping one row");
View Full Code Here

  }

  private void initialize()
  {
    scripts = new ConcurrentHashMap<String, PyCode>();
    PythonInterpreter interpreter = new PythonInterpreter();
    compileAndAddToRepository(interpreter, PythonTransformationHelper.getImportStatements());
    compileAndAddToRepository(interpreter, PythonTransformationHelper.getGetValueDefStatement());
    compileAndAddToRepository(interpreter, PythonTransformationHelper.getIsEmptyDefStatement());
    compileAndAddToRepository(interpreter, PythonTransformationHelper.getHasSelectedRowsStatement());
    compileAndAddToRepository(interpreter, PythonTransformationHelper.getVDefStatement());
View Full Code Here

TOP

Related Classes of org.python.util.PythonInterpreter

Copyright © 2018 www.massapicom. 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.