?? pythoninterpreter.java
字號:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: PythonInterpreter.java,v 1.3 2005/05/17 16:40:19 jmettraux Exp $ *///// PythonInterpreter.java//// jmettraux@openwfe.org//// generated with // jtmpl 1.0.04 20.11.2001 John Mettraux (jmettraux@openwfe.org)//package openwfe.org.apre.jython;import org.python.core.PyObject;import openwfe.org.apre.ApreException;/** * An encapsulation of the jython interpreter with methods for easily * getting python-written commands ready to use. * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Date: 2005/05/17 16:40:19 $ * <br>$Id: PythonInterpreter.java,v 1.3 2005/05/17 16:40:19 jmettraux Exp $ </font> * * @author jmettraux@openwfe.org */public class PythonInterpreter{ private final static PythonInterpreter theInterpreter = new PythonInterpreter(); org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(PythonInterpreter.class.getName()); // // FIELDS private org.python.util.PythonInterpreter interpreter; private java.util.List interpretedFiles = new java.util.ArrayList(25); // // CONSTRUCTORS public PythonInterpreter () { org.python.core.PySystemState.initialize(); this.interpreter = new org.python.util.PythonInterpreter(); } // // METHODS /* private void importClass (String fullClassName) { if (fullClassName.indexOf(".") < 0) return; String[] ss = fullClassName.split("\\."); for (int j=0; j<ss.length; j++) log.debug("........ "+j+" : "+ss[j]); StringBuffer path = new StringBuffer(); String className = ss[ss.length-1]; for (int i=0; i<ss.length-1; i++) { path.append(ss[i]); if (i < ss.length-2) path.append('.'); } log.debug("path is >"+path+"<"); log.debug("className is >"+className+"<"); StringBuffer command = new StringBuffer(); command.append("from "); command.append(path); command.append(" import "); command.append(className); this.interpreter.exec(command.toString()); } */ public Object instantiate (String className, Class classType) throws ApreException { //System.out.println("Looking for class '"+className+"'"); //importClass(className); PyObject pyClass = this.interpreter.get(className); if (pyClass == null) { //System.out.println("found nothing..."); return null; } PyObject po = pyClass.__call__(); Object o = po.__tojava__(classType); if (o == org.python.core.Py.NoConversion) { throw new ApreException ("Found class '"+className+ "' in python interpreter but failed to use it\n"); } return o; } public void loadAndInterpret (String fileName) { if (this.interpretedFiles.contains(fileName)) return; log.info("Loading and interpreting '"+fileName+"'"); this.interpreter.execfile(fileName); this.interpretedFiles.add(fileName); } public void loadAndInterpretDirectory (String dir) { loadAndInterpretDirectory(new java.io.File(dir)); } private void loadAndInterpretDirectory (java.io.File dir) { java.io.File[] files = dir.listFiles(); for (int i=0; i<files.length; i++) { java.io.File f = files[i]; if (f.isDirectory()) { loadAndInterpretDirectory(f); continue; } if (f.getName().endsWith(".py")) { loadAndInterpret(f.getPath()); } } } /* public void loadAndInterpret (java.util.List fileNames) { if (fileNames == null || fileNames.size() < 1) return; java.util.Iterator it = fileNames.iterator(); while (it.hasNext()) { loadAndInterpret((String)it.next()); } } */ /* public PyObject eval (String command) throws ApreException { java.util.StringTokenizer st = new java.util.StringTokenizer(command); st.nextToken(); // skip 'eval'; if ( ! st.hasMoreTokens()) { throw new ApreException ("'get' or 'exec' awaited at the beginning of '"+command+"'"); } String intention = st.nextToken().toLowerCase(); String pyStuff = command.substring(6+intention.length()); if (intention.equals("exec")) { this.interpreter.exec(pyStuff); return null; } else if (intention.equals("get")) { return this.interpreter.get(pyStuff); } throw new ApreException ("'get' or 'exec' awaited at the beginning of '"+command+"'"); } */ // // STATIC METHODS public static PythonInterpreter getInstance () { return theInterpreter; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -