edu.wisc.ssec.mcidasv.jython
Class Interpreter

java.lang.Object
  extended by org.python.util.PythonInterpreter
      extended by org.python.util.InteractiveInterpreter
          extended by edu.wisc.ssec.mcidasv.jython.Interpreter

public class Interpreter
extends org.python.util.InteractiveInterpreter


Field Summary
private  ConsoleCallback callback
          A hook that allows external classes to respond to events.
private static String CONSOLE_FILENAME
          Dummy filename for the interactive interpreter.
private  boolean moreInput
          Whether or not jython needs more input to run something.
private  ByteArrayOutputStream stderr
          Stream used for error output.
private  ByteArrayOutputStream stdout
          Stream used for normal output.
private  boolean thinking
          Whether or not Jython is working on something
 
Fields inherited from class org.python.util.InteractiveInterpreter
buffer, filename
 
Fields inherited from class org.python.util.PythonInterpreter
cflags, systemState, threadLocals
 
Constructor Summary
Interpreter(org.python.core.PySystemState state, ByteArrayOutputStream stdout, ByteArrayOutputStream stderr)
          Creates a Jython interpreter based upon the specified system state and whose output streams are mapped to the specified byte streams.
 
Method Summary
private static String clearStream(String command, ByteArrayOutputStream stream)
          Removes and returns all existing text from stream.
 void handleException(Console console, Throwable e)
          Sends error information to the specified console.
 void handleStreams(Console console, String command)
          Sends the contents of stdout and stderr on their merry way.
 boolean isBusy()
          Determines whether or not Jython is busy.
 boolean needMoreInput()
           
 boolean push(Console console, String line)
          Here's the magic!
protected  void setCallbackHandler(ConsoleCallback newCallback)
          Registers a new callback handler with the interpreter.
 
Methods inherited from class org.python.util.InteractiveInterpreter
interrupt, resetbuffer, runcode, runsource, runsource, runsource, showexception, write
 
Methods inherited from class org.python.util.PythonInterpreter
cleanup, compile, compile, compile, compile, eval, eval, exec, exec, execfile, execfile, execfile, get, get, getLocals, getSystemState, initialize, set, set, setErr, setErr, setErr, setIn, setIn, setIn, setLocals, setOut, setOut, setOut, setSystemState, threadLocalStateInterpreter
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONSOLE_FILENAME

private static final String CONSOLE_FILENAME
Dummy filename for the interactive interpreter.

See Also:
Constant Field Values

stderr

private ByteArrayOutputStream stderr
Stream used for error output.


stdout

private ByteArrayOutputStream stdout
Stream used for normal output.


moreInput

private boolean moreInput
Whether or not jython needs more input to run something.


callback

private ConsoleCallback callback
A hook that allows external classes to respond to events.


thinking

private boolean thinking
Whether or not Jython is working on something

Constructor Detail

Interpreter

public Interpreter(org.python.core.PySystemState state,
                   ByteArrayOutputStream stdout,
                   ByteArrayOutputStream stderr)
Creates a Jython interpreter based upon the specified system state and whose output streams are mapped to the specified byte streams.

Additionally, the "__main__" module is imported by default so that the locals namespace makes sense.

Parameters:
state - The system state you want to use with the interpreter.
stdout - The stream Jython will use for standard output.
stderr - The stream Jython will use for error output.
Method Detail

setCallbackHandler

protected void setCallbackHandler(ConsoleCallback newCallback)
Registers a new callback handler with the interpreter. This mechanism allows external code to easily react to events taking place in the interpreter.

Parameters:
newCallback - The new callback handler.

push

public boolean push(Console console,
                    String line)
Here's the magic! Basically just accumulates a buffer that gets passed off to jython-land until it can run.

Parameters:
line - A Jython command.
Returns:
False if Jython did something. True if more input is needed.

isBusy

public boolean isBusy()
Determines whether or not Jython is busy.

Returns:
true if busy, false otherwise.

needMoreInput

public boolean needMoreInput()
Returns:
Whether or not Jython needs more input to run something.

handleStreams

public void handleStreams(Console console,
                          String command)
Sends the contents of stdout and stderr on their merry way. Both streams are emptied as a result.

Parameters:
console - Console where the command originated.
command - The command that was executed. Null values are permitted, as they signify that no command was entered for any generated output.

clearStream

private static String clearStream(String command,
                                  ByteArrayOutputStream stream)
Removes and returns all existing text from stream.

Parameters:
command - Command that was executed. Null values are permitted and imply that no command is "associated" with text in stream.
stream - Stream to be cleared out.
Returns:
The contents of stream before it was reset.
See Also:
handleStreams(Console, String)

handleException

public void handleException(Console console,
                            Throwable e)
Sends error information to the specified console.

Parameters:
console - The console that caused the exception.
e - The exception!