KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.util;
3
4 import org.gnu.readline.Readline;
5 import org.gnu.readline.ReadlineLibrary;
6 import org.python.core.ArgParser;
7 import org.python.core.Py;
8 import org.python.core.PyException;
9 import org.python.core.PyObject;
10 import org.python.core.PyString;
11 import org.python.core.PySystemState;
12
13 /**
14  * Uses:
15  * <a HREF="http://java-readline.sourceforge.net/">Java Readline</a>
16  * <p/>
17  *
18  * Based on CPython-1.5.2's code module
19  *
20  */

21 public class ReadlineConsole extends InteractiveConsole {
22     public String JavaDoc filename;
23
24     public ReadlineConsole() {
25         this(null, "<console>");
26     }
27     public ReadlineConsole(PyObject locals) {
28         this(locals, "<console>");
29     }
30     public ReadlineConsole(PyObject locals, String JavaDoc filename) {
31         super(locals,filename);
32         String JavaDoc backingLib = PySystemState.registry.getProperty(
33                                  "python.console.readlinelib", "Editline");
34         try {
35             Readline.load(ReadlineLibrary.byName(backingLib));
36         } catch (RuntimeException JavaDoc e) {
37             // Silently ignore errors during load of the native library.
38
// Will use a pure java fallback.
39
}
40
41         // hook into the builtins table so that clients like cmd.Cmd can
42
// also use readline.
43
Py.getSystemState().builtins.__setitem__("raw_input",
44               Py.newJavaFunc(this.getClass(), "_raw_input"));
45
46         Readline.initReadline("jython");
47     }
48
49
50     /**
51      * Write a prompt and read a line.
52      *
53      * The returned line does not include the trailing newline. When the
54      * user enters the EOF key sequence, EOFError is raised.
55      *
56      * This subclass implements the functionality using JavaReadline.
57      **/

58     public String JavaDoc raw_input(PyObject prompt) {
59         return _raw_input(new PyObject[] { prompt }, new String JavaDoc[0]);
60     }
61
62     /**
63      * Central point of dispatch to Readline library for all clients,
64      * whether the console itself or others like cmd.Cmd interpreters.
65      * Both of these uses come through here.
66      *
67      * @param args should contain a single prompt
68      * @param kws keywords
69      * @return the user input
70      **/

71     public static String JavaDoc _raw_input(PyObject args[], String JavaDoc kws[]) {
72         ArgParser ap = new ArgParser("raw_input", args, kws, "prompt");
73         PyObject prompt = ap.getPyObject(0, new PyString(""));
74         try {
75             String JavaDoc line = Readline.readline(
76                             prompt==null ? "" : prompt.toString());
77             return (line == null ? "" : line);
78         } catch (java.io.EOFException JavaDoc eofe) {
79             throw new PyException(Py.EOFError);
80         } catch (java.io.IOException JavaDoc e) {
81             throw new PyException(Py.IOError);
82         }
83     }
84 }
85
Popular Tags