001: // Copyright (c) Corporation for National Research Initiatives
002: package org.python.util;
003:
004: import org.python.core.*;
005: import java.util.*;
006:
007: /**
008: * The PythonInterpreter class is a standard wrapper for a JPython
009: * interpreter for use embedding in a Java application.
010: *
011: * @author Jim Hugunin
012: * @version 1.0, 02/23/97
013: */
014:
015: public class PythonInterpreter {
016: PyModule module;
017: protected PySystemState systemState;
018: PyObject locals;
019:
020: protected CompilerFlags cflags = null;
021:
022: /**
023: * Initializes the jython runtime. This should only be called once, and
024: * should be called before any other python objects are created (including a
025: * PythonInterpreter).
026: *
027: * @param preProperties
028: * A set of properties. Typically System.getProperties() is used.
029: * @param postProperties
030: * An other set of properties. Values like python.home,
031: * python.path and all other values from the registry files can
032: * be added to this property set. PostProperties will override
033: * system properties and registry properties.
034: * @param argv
035: * Command line argument. These values will assigned to sys.argv.
036: */
037: public static void initialize(Properties preProperties,
038: Properties postProperties, String[] argv) {
039: PySystemState.initialize(preProperties, postProperties, argv);
040: }
041:
042: /**
043: * Create a new Interpreter with an empty dictionary
044: */
045: public PythonInterpreter() {
046: this (null, null);
047: }
048:
049: /**
050: * Create a new interpreter with the given dictionary to use as its
051: * namespace
052: *
053: * @param dict the dictionary to use
054: */
055:
056: // Optional dictionary willl be used for locals namespace
057: public PythonInterpreter(PyObject dict) {
058: this (dict, null);
059: }
060:
061: public PythonInterpreter(PyObject dict, PySystemState systemState) {
062: if (dict == null)
063: dict = new PyStringMap();
064: if (systemState == null) {
065: systemState = Py.getSystemState();
066: if (systemState == null)
067: systemState = new PySystemState();
068: }
069: module = new PyModule("main", dict);
070: this .systemState = systemState;
071: locals = module.__dict__;
072: setState();
073: }
074:
075: protected void setState() {
076: Py.setSystemState(systemState);
077: }
078:
079: /**
080: * Set the Python object to use for the standard output stream
081: *
082: * @param outStream Python file-like object to use as output stream
083: */
084: public void setOut(PyObject outStream) {
085: systemState.stdout = outStream;
086: }
087:
088: /**
089: * Set a java.io.Writer to use for the standard output stream
090: *
091: * @param outStream Writer to use as output stream
092: */
093: public void setOut(java.io.Writer outStream) {
094: setOut(new PyFile(outStream));
095: }
096:
097: /**
098: * Set a java.io.OutputStream to use for the standard output stream
099: *
100: * @param outStream OutputStream to use as output stream
101: */
102: public void setOut(java.io.OutputStream outStream) {
103: setOut(new PyFile(outStream));
104: }
105:
106: public void setErr(PyObject outStream) {
107: systemState.stderr = outStream;
108: }
109:
110: public void setErr(java.io.Writer outStream) {
111: setErr(new PyFile(outStream));
112: }
113:
114: public void setErr(java.io.OutputStream outStream) {
115: setErr(new PyFile(outStream));
116: }
117:
118: /**
119: * Evaluate a string as Python source and return the result
120: *
121: * @param s the string to evaluate
122: */
123: public PyObject eval(String s) {
124: setState();
125: return __builtin__.eval(new PyString(s), locals);
126: }
127:
128: /**
129: * Execute a string of Python source in the local namespace
130: *
131: * @param s the string to execute
132: */
133: public void exec(String s) {
134: setState();
135: Py.exec(Py.compile_flags(s, "<string>", "exec", cflags),
136: locals, locals);
137: }
138:
139: /**
140: * Execute a Python code object in the local namespace
141: *
142: * @param code the code object to execute
143: */
144: public void exec(PyObject code) {
145: setState();
146: Py.exec(code, locals, locals);
147: }
148:
149: /**
150: * Execute a file of Python source in the local namespace
151: *
152: * @param s the name of the file to execute
153: */
154: public void execfile(String s) {
155: setState();
156: __builtin__.execfile_flags(s, locals, locals, cflags);
157: }
158:
159: public void execfile(java.io.InputStream s) {
160: execfile(s, "<iostream>");
161: }
162:
163: public void execfile(java.io.InputStream s, String name) {
164: setState();
165: Py.runCode(Py.compile_flags(s, name, "exec", cflags), locals,
166: locals);
167: }
168:
169: // Getting and setting the locals dictionary
170: public PyObject getLocals() {
171: return locals;
172: }
173:
174: public void setLocals(PyObject d) {
175: locals = d;
176: }
177:
178: /**
179: * Set a variable in the local namespace
180: *
181: * @param name the name of the variable
182: * @param value the value to set the variable to.
183: Will be automatically converted to an appropriate Python object.
184: */
185: public void set(String name, Object value) {
186: locals.__setitem__(name.intern(), Py.java2py(value));
187: }
188:
189: /**
190: * Set a variable in the local namespace
191: *
192: * @param name the name of the variable
193: * @param value the value to set the variable to
194: */
195: public void set(String name, PyObject value) {
196: locals.__setitem__(name.intern(), value);
197: }
198:
199: /**
200: * Get the value of a variable in the local namespace
201: *
202: * @param name the name of the variable
203: */
204: public PyObject get(String name) {
205: return locals.__finditem__(name.intern());
206: }
207:
208: /**
209: * Get the value of a variable in the local namespace Value will be
210: * returned as an instance of the given Java class.
211: * <code>interp.get("foo", Object.class)</code> will return the most
212: * appropriate generic Java object.
213: *
214: * @param name the name of the variable
215: * @param javaclass the class of object to return
216: */
217: public Object get(String name, Class javaclass) {
218: return Py.tojava(locals.__finditem__(name.intern()), javaclass);
219: }
220:
221: public void cleanup() {
222: systemState.callExitFunc();
223: }
224: }
|