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


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

                            // solve import statements.
                            PySystemState.add_extdir(jarDir);
                        }
                    }

                    PythonInterpreter interpreter = new PythonInterpreter();
                    interpreter.exec(script);
                    return null;
                }
            });
        } catch (PrivilegedActionException e) {
            logger.error(e.getMessage(), e);
View Full Code Here

        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 necissarry 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 scrypt.  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);

                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.\n"+
                                         "Filenames 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.
View Full Code Here

    }
    String extension = fileName.substring(locationOfDot + 1);
    NonCoreContract parserContract = NonCoreContract
        .getNonCoreContract("bill-parser-" + extension);
    try {
      PythonInterpreter interpreter = new PythonInterpreter();
      interpreter.exec(parserContract.getChargeScript());
      PyObject parserClass = interpreter.get("Parser");
      parser = (BillParser) parserClass
          .__call__(
              PyJavaType.wrapJavaObject(new InputStreamReader(is,
                  "UTF-8"))).__tojava__(BillParser.class);
    } catch (UnsupportedEncodingException e) {
View Full Code Here

    if (finishDate != null
        && startDate.getDate().after(finishDate.getDate())) {
      throw new UserException(
          "The start date can't be after the finish date");
    }
    PythonInterpreter interp = new PythonInterpreter();
    try {
      interp.compile(script);
    } catch (Throwable e) {
      throw new UserException(HttpException.getStackTraceString(e));
    }
    setScript(script);
  }
View Full Code Here

    name = name.trim();
    if (name.length() == 0) {
      throw new UserException("The contract name can't be blank.");
    }
    setName(name);
    PythonInterpreter interp = new PythonInterpreter();
    interp.set("contract", this);
    try {
      interp.compile(chargeScript);
    } catch (Throwable e) {
      throw new UserException(HttpException.getStackTraceString(e));
    }
    setChargeScript(chargeScript);
  }
View Full Code Here

  }

  public Object callFunction(String name, Object... args)
      throws HttpException {
    Object result = null;
    PythonInterpreter interp = new PythonInterpreter();
    try {
      interp.set("contract", this);
      interp.exec(chargeScript);
      PyObject function = interp.get(name);
      if (function == null) {
        throw new UserException("There isn't a function called " + name);
      }
      result = function.__call__(Py.javas2pys(args)).__tojava__(
          Object.class);
    } catch (Throwable e) {
      throw new UserException(HttpException.getStackTraceString(e));
    } finally {
      interp.cleanup();
    }
    return result;
  }
View Full Code Here

  public void update(String name, String script, String template)
      throws HttpException {
    setName(name);
    try {
      PythonInterpreter interp = new PythonInterpreter();
      interp.compile(script);
      setScript(script);
    } catch (Throwable e) {
      throw new UserException(HttpException.getStackTraceString(e));
    }
    if (template != null && template.trim().length() == 0) {
View Full Code Here

    }
  }

  @SuppressWarnings("unchecked")
  public void run(Invocation inv, Document doc) throws HttpException {
    PythonInterpreter interp = new PythonInterpreter();

    try {
      Element source = doc.getDocumentElement();
      interp.set("doc", doc);
      interp.set("source", source);
      interp.set("inv", inv);
      interp.set("template", getTemplate());
      StringWriter out = new StringWriter();
      interp.setOut(out);
      StringWriter err = new StringWriter();
      interp.setErr(err);

      ServletContext ctx = inv.getMonad().getServletConfig()
          .getServletContext();
      Map<Long, String> request_map = (Map<Long, String>) ctx
          .getAttribute(ContextListener.CONTEXT_REQUEST_MAP);
      request_map.put(
          Thread.currentThread().getId(),
          inv.getRequest()
              .getRequestURL()
              .append('?' + inv.getRequest().getQueryString()
                  + ' ' + new MonadDate() + ' '
                  + inv.getRequest().getRemoteAddr())
              .toString());

      interp.exec(script);
    } catch (Throwable e) {
      throw new UserException(e.getMessage() + " "
          + HttpException.getStackTraceString(e));
    } finally {
      interp.cleanup();
    }
  }
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.