<s> import __builtin__ <EOL> import __main__ <EOL> import codeop <EOL> import keyword <EOL> import os <EOL> import re <EOL> import readline <EOL> import threading <EOL> import traceback <EOL> import signal <EOL> import sys <EOL> if sys . version [ <NUM_LIT:0> ] == '<STR_LIT:2>' : <EOL> import pygtk <EOL> pygtk . require ( "<STR_LIT>" ) <EOL> import gtk , gobject <EOL> def walk_class ( klass ) : <EOL> list = [ ] <EOL> for item in dir ( klass . __class__ ) : <EOL> if item [ <NUM_LIT:0> ] != "<STR_LIT:_>" : <EOL> list . append ( item ) <EOL> for base in klass . __class__ . __bases__ : <EOL> list = list + walk_class ( base ( ) ) <EOL> return list <EOL> class Completer : <EOL> def __init__ ( self , lokals ) : <EOL> self . locals = lokals <EOL> self . completions = keyword . kwlist + __builtin__ . __dict__ . keys ( ) + __main__ . __dict__ . keys ( ) <EOL> def complete ( self , text , state ) : <EOL> if state == <NUM_LIT:0> : <EOL> if "<STR_LIT:.>" in text : <EOL> self . matches = self . attr_matches ( text ) <EOL> else : <EOL> self . matches = self . global_matches ( text ) <EOL> try : <EOL> return self . matches [ state ] <EOL> except IndexError : <EOL> return None <EOL> def update ( self , locs ) : <EOL> self . locals = locs <EOL> for key in self . locals . keys ( ) : <EOL> if not key in self . completions : <EOL> self . completions . append ( key ) <EOL> def global_matches ( self , text ) : <EOL> matches = [ ] <EOL> n = len ( text ) <EOL> for word in self . completions : <EOL> if word [ : n ] == text : <EOL> matches . append ( word ) <EOL> return matches <EOL> def attr_matches ( self , text ) : <EOL> m = re . match ( r"<STR_LIT>" , text ) <EOL> if not m : <EOL> return <EOL> expr , attr = m . group ( <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> obj = eval ( expr , self . locals ) <EOL> if str ( obj ) [ <NUM_LIT:1> : <NUM_LIT:4> ] == "<STR_LIT>" : <EOL> words = walk_class ( obj ) <EOL> else : <EOL> words = dir ( eval ( expr , self . locals ) ) <EOL> matches = [ ] <EOL> n = len ( attr ) <EOL> for word in words : <EOL> if word [ : n ] == attr : <EOL> matches . append ( "<STR_LIT>" % ( expr , word ) ) <EOL> return matches <EOL> class GtkInterpreter ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> TIMEOUT = <NUM_LIT:100> <EOL> def __init__ ( self ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . ready = threading . Condition ( ) <EOL> self . globs = globals ( ) <EOL> self . locs = locals ( ) <EOL> self . _kill = <NUM_LIT:0> <EOL> self . cmd = '<STR_LIT>' <EOL> self . new_cmd = None <EOL> self . completer = Completer ( self . locs ) <EOL> readline . set_completer ( self . completer . complete ) <EOL> readline . parse_and_bind ( '<STR_LIT>' ) <EOL> def run ( self ) : <EOL> gobject . timeout_add ( self . TIMEOUT , self . code_exec ) <EOL> try : <EOL> if gtk . gtk_version [ <NUM_LIT:0> ] == <NUM_LIT:2> : <EOL> gtk . gdk . threads_init ( ) <EOL> except : <EOL> pass <EOL> gtk . main ( ) <EOL> def code_exec ( self ) : <EOL> """<STR_LIT>""" <EOL> self . ready . acquire ( ) <EOL> if self . _kill : gtk . main_quit ( ) <EOL> if self . new_cmd is not None : <EOL> self . ready . notify ( ) <EOL> self . cmd = self . cmd + self . new_cmd <EOL> self . new_cmd = None <EOL> try : <EOL> code = codeop . compile_command ( self . cmd [ : - <NUM_LIT:1> ] ) <EOL> if code : <EOL> self . cmd = '<STR_LIT>' <EOL> exec ( code , self . globs , self . locs ) <EOL> self . completer . update ( self . locs ) <EOL> except Exception : <EOL> traceback . print_exc ( ) <EOL> self . cmd = '<STR_LIT>' <EOL> self . ready . release ( ) <EOL> return <NUM_LIT:1> <EOL> def feed ( self , code ) : <EOL> """<STR_LIT>""" <EOL> if ( not code ) or ( code [ - <NUM_LIT:1> ] < > '<STR_LIT:\n>' ) : code = code + '<STR_LIT:\n>' <EOL> self . completer . update ( self . locs ) <EOL> self . ready . acquire ( ) <EOL> self . new_cmd = code <EOL> self . ready . wait ( ) <EOL> self . ready . release ( ) <EOL> return not self . cmd <EOL> def kill ( self ) : <EOL> """<STR_LIT>""" <EOL> self . ready . acquire ( ) <EOL> self . _kill = <NUM_LIT:1> <EOL> self . ready . release ( ) <EOL> self . join ( ) <EOL> def signal_handler ( * args ) : <EOL> print "<STR_LIT>" , args <EOL> sys . exit ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> signal . signal ( signal . SIGINT , signal_handler ) <EOL> signal . signal ( signal . SIGSEGV , signal_handler ) <EOL> prompt = '<STR_LIT>' <EOL> interpreter = GtkInterpreter ( ) <EOL> interpreter . start ( ) <EOL> interpreter . feed ( "<STR_LIT>" ) <EOL> interpreter . feed ( "<STR_LIT>" ) <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> for file in open ( sys . argv [ <NUM_LIT:1> ] ) . readlines ( ) : <EOL> interpreter . feed ( file ) <EOL> print '<STR_LIT>' <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> command = raw_input ( prompt ) + '<STR_LIT:\n>' <EOL> prompt = interpreter . feed ( command ) and '<STR_LIT>' or '<STR_LIT>' <EOL> except ( EOFError , KeyboardInterrupt ) : pass <EOL> interpreter . kill ( ) <EOL> print </s>
