KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > util > InteractiveConsole


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.util;
3 import org.python.core.*;
4
5 // Based on CPython-1.5.2's code module
6

7 public class InteractiveConsole extends InteractiveInterpreter {
8     public String JavaDoc filename;
9
10     public InteractiveConsole() {
11         this(null, "<console>");
12     }
13     public InteractiveConsole(PyObject locals) {
14         this(locals, "<console>");
15     }
16     public InteractiveConsole(PyObject locals, String JavaDoc filename) {
17         super(locals);
18         this.filename = filename;
19     }
20
21     /**
22      * Closely emulate the interactive Python console.
23      *
24      * The optional banner argument specifies the banner to print before
25      * the first interaction; by default it prints a banner similar to the
26      * one printed by the real Python interpreter, followed by the current
27      * class name in parentheses (so as not to confuse this with the real
28      * interpreter -- since it's so close!).
29      **/

30     public void interact() {
31         interact(getDefaultBanner());
32     }
33
34     public static String JavaDoc getDefaultBanner() {
35         String JavaDoc compiler = System.getProperty("java.compiler");
36
37         return "Jython " + PySystemState.version + " on " +
38             PySystemState.platform + " (JIT: " +
39             ((compiler == null) ? "null" : compiler) + ")";
40     }
41
42     public void interact(String JavaDoc banner) {
43         if (banner != null) {
44             write(banner);
45             write("\n");
46         }
47         // Dummy exec in order to speed up response on first command
48
exec("2");
49         //System.err.println("interp2");
50
boolean more = false;
51         while (true) {
52             PyObject prompt = more ? systemState.ps2 : systemState.ps1;
53             String JavaDoc line;
54             try {
55                 line = raw_input(prompt);
56             } catch (PyException exc) {
57                 if (!Py.matchException(exc, Py.EOFError))
58                     throw exc;
59                 write("\n");
60                 break;
61             }
62             more = push(line);
63         }
64     }
65
66     /**
67      * Push a line to the interpreter.
68      *
69      * The line should not have a trailing newline; it may have internal
70      * newlines. The line is appended to a buffer and the interpreter's
71      * runsource() method is called with the concatenated contents of the
72      * buffer as source. If this indicates that the command was executed
73      * or invalid, the buffer is reset; otherwise, the command is
74      * incomplete, and the buffer is left as it was after the line was
75      * appended. The return value is 1 if more input is required, 0 if the
76      * line was dealt with in some way (this is the same as runsource()).
77      **/

78
79     public boolean push(String JavaDoc line) {
80         if (buffer.length() > 0)
81             buffer.append("\n");
82         buffer.append(line);
83         boolean more = runsource(buffer.toString(), filename);
84         if (!more)
85             resetbuffer();
86         return more;
87     }
88
89     /**
90      * Write a prompt and read a line.
91      *
92      * The returned line does not include the trailing newline. When the
93      * user enters the EOF key sequence, EOFError is raised.
94      *
95      * The base implementation uses the built-in function raw_input(); a
96      * subclass may replace this with a different implementation.
97      **/

98     public String JavaDoc raw_input(PyObject prompt) {
99         return __builtin__.raw_input(prompt);
100     }
101 }
102
Popular Tags