Source Code Cross Referenced for PythonInterpreter.java in  » Testing » Marathon » org » python » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » Marathon » org.python.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.