<s> import sys <EOL> from IPython . iplib import InteractiveShell , softspace <EOL> from IPython . Shell import IPShellEmbed , make_IPython , ipapi , kill_embedded , ultraTB <EOL> from traceback import print_exc <EOL> from StringIO import StringIO <EOL> from pyjs import translator <EOL> class REPL ( object ) : <EOL> def __init__ ( self , compiler , linker , translator_arguments , <EOL> jsglobal , jscontext ) : <EOL> self . compiler = compiler <EOL> self . linker = linker <EOL> self . translator_arguments = translator_arguments <EOL> self . jsglobal = jsglobal <EOL> self . jscontext = jscontext <EOL> self . jscontext . eval ( js_interactive_func ) <EOL> self . jscontext . eval ( set_main ) <EOL> def __call__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . shell = V8ShellEmbed ( argv = '<STR_LIT>' ) <EOL> self . shell . IP . _set_js_int ( self ) <EOL> self . shell . set_banner ( self . shell . IP . BANNER + '<STR_LIT>' + banner ) <EOL> self . shell . IP . hooks . generate_prompt = generate_prompt <EOL> locs = dict ( <EOL> shell = self , <EOL> jsctxt = self . jscontext , <EOL> jsglob = self . jsglobal <EOL> ) <EOL> self . shell ( local_ns = locs ) <EOL> def translate ( self , src ) : <EOL> output = StringIO ( ) <EOL> tree = self . compiler . parse ( src + "<STR_LIT:\n>" ) <EOL> t = InteractiveTranslator ( self . compiler , <EOL> '<STR_LIT>' , '<STR_LIT>' , src , tree , output , <EOL> ** self . translator_arguments ) <EOL> jssrc = output . getvalue ( ) <EOL> output . close ( ) <EOL> return jssrc , t . imported_modules , t . imported_js <EOL> def eval ( self , code , print_result = False ) : <EOL> try : <EOL> res = self . jscontext . eval ( code ) <EOL> if print_result : <EOL> print res <EOL> except Exception , e : <EOL> print e <EOL> return <NUM_LIT:0> <EOL> js_interactive_func = """<STR_LIT>""" <EOL> set_main = """<STR_LIT>""" <EOL> banner = """<STR_LIT>""" <EOL> class V8InteractiveShell ( InteractiveShell ) : <EOL> _js_int = None <EOL> _is_native = False <EOL> _show_js = False <EOL> def runsource ( self , source , filename = '<STR_LIT>' , symbol = '<STR_LIT>' ) : <EOL> if self . _is_native : <EOL> return InteractiveShell . runsource ( self , source , filename , symbol ) <EOL> source = source . encode ( self . stdin_encoding ) <EOL> if source [ : <NUM_LIT:1> ] in [ '<STR_LIT:U+0020>' , '<STR_LIT:\t>' ] : <EOL> source = '<STR_LIT>' % source <EOL> try : <EOL> code = self . compile ( source , filename , symbol ) <EOL> except ( OverflowError , SyntaxError , ValueError , TypeError , MemoryError ) : <EOL> self . showsyntaxerror ( filename ) <EOL> return None <EOL> if code is None : <EOL> return True <EOL> if source . startswith ( '<STR_LIT>' ) : <EOL> return InteractiveShell . runsource ( self , source , filename , symbol ) <EOL> try : <EOL> jscode , imppy , impjs = self . _js_int . translate ( source ) <EOL> self . code_to_run = jscode <EOL> except Exception , e : <EOL> print_exc ( ) <EOL> return None <EOL> if self . _show_js : <EOL> print "<STR_LIT>" + jscode + "<STR_LIT:\n>" <EOL> if self . _js_int . eval ( jscode ) == <NUM_LIT:0> : <EOL> return False <EOL> else : <EOL> return None <EOL> def runcode ( self , code_obj ) : <EOL> old_excepthook , sys . excepthook = sys . excepthook , self . excepthook <EOL> self . sys_excepthook = old_excepthook <EOL> outflag = <NUM_LIT:1> <EOL> try : <EOL> try : <EOL> self . hooks . pre_runcode_hook ( ) <EOL> exec code_obj in self . user_global_ns , self . user_ns <EOL> finally : <EOL> sys . excepthook = old_excepthook <EOL> except SystemExit : <EOL> self . resetbuffer ( ) <EOL> self . showtraceback ( ) <EOL> warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , level = <NUM_LIT:1> ) <EOL> except self . custom_exceptions : <EOL> etype , value , tb = sys . exc_info ( ) <EOL> self . CustomTB ( etype , value , tb ) <EOL> except : <EOL> self . showtraceback ( ) <EOL> else : <EOL> outflag = <NUM_LIT:0> <EOL> if softspace ( sys . stdout , <NUM_LIT:0> ) : <EOL> print <EOL> self . code_to_run = None <EOL> return outflag <EOL> def _set_js_int ( self , js_int ) : <EOL> self . _js_int = js_int <EOL> def magic_native ( self , p ) : <EOL> self . _is_native = True <EOL> print "<STR_LIT>" <EOL> def magic_pyjs ( self , p ) : <EOL> self . _is_native = False <EOL> print "<STR_LIT>" <EOL> def magic_showjs ( self , p ) : <EOL> f = False <EOL> if p . lower ( ) == '<STR_LIT:true>' : <EOL> f = True <EOL> elif p == '<STR_LIT:1>' : <EOL> f = True <EOL> elif not p : <EOL> f = True <EOL> self . _show_js = f <EOL> if f : <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> def generate_prompt ( is_continuation ) : <EOL> """<STR_LIT>""" <EOL> if not is_continuation : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> class V8ShellEmbed ( IPShellEmbed ) : <EOL> def __init__ ( self , argv = None , banner = '<STR_LIT>' , exit_msg = None , rc_override = None , <EOL> user_ns = None ) : <EOL> self . set_banner ( banner ) <EOL> self . set_exit_msg ( exit_msg ) <EOL> self . set_dummy_mode ( <NUM_LIT:0> ) <EOL> self . sys_displayhook_ori = sys . displayhook <EOL> try : <EOL> self . sys_ipcompleter_ori = sys . ipcompleter <EOL> except : <EOL> pass <EOL> self . IP = make_IPython ( argv , rc_override = rc_override , <EOL> embedded = True , <EOL> user_ns = user_ns , <EOL> shell_class = V8InteractiveShell ) <EOL> ip = ipapi . IPApi ( self . IP ) <EOL> ip . expose_magic ( "<STR_LIT>" , kill_embedded ) <EOL> self . sys_displayhook_embed = sys . displayhook <EOL> sys . displayhook = self . sys_displayhook_ori <EOL> sys . excepthook = ultraTB . FormattedTB ( color_scheme = self . IP . rc . colors , <EOL> mode = self . IP . rc . xmode , <EOL> call_pdb = self . IP . rc . pdb ) <EOL> self . restore_system_completer ( ) <EOL> class InteractiveTranslator ( translator . Translator ) : <EOL> def __init__ ( self , compiler , <EOL> module_name , module_file_name , src , mod , output , <EOL> dynamic = <NUM_LIT:0> , findFile = None , ** kw ) : <EOL> self . compiler = compiler <EOL> self . ast = compiler . ast <EOL> self . js_module_name = self . jsname ( "<STR_LIT>" , module_name ) <EOL> if module_name : <EOL> self . module_prefix = "<STR_LIT>" <EOL> else : <EOL> self . module_prefix = "<STR_LIT>" <EOL> self . module_name = module_name <EOL> src = src . replace ( "<STR_LIT:\r\n>" , "<STR_LIT:\n>" ) <EOL> src = src . replace ( "<STR_LIT>" , "<STR_LIT:\n>" ) <EOL> src = src . replace ( "<STR_LIT:\r>" , "<STR_LIT:\n>" ) <EOL> self . src = src . split ( "<STR_LIT:\n>" ) <EOL> self . output = output <EOL> self . dynamic = dynamic <EOL> self . findFile = findFile <EOL> self . set_compile_options ( kw ) <EOL> self . future_division = False <EOL> self . imported_modules = [ ] <EOL> self . imported_js = [ ] <EOL> self . is_class_definition = False <EOL> self . local_prefix = None <EOL> self . track_lines = { } <EOL> self . stacksize_depth = <NUM_LIT:0> <EOL> self . option_stack = [ ] <EOL> self . lookup_stack = [ { } ] <EOL> self . indent_level = <NUM_LIT:0> <EOL> self . __unique_ids__ = { } <EOL> self . try_depth = - <NUM_LIT:1> <EOL> self . is_generator = False <EOL> self . generator_states = [ ] <EOL> self . state_max_depth = len ( self . generator_states ) <EOL> self . constant_int = { } <EOL> self . constant_long = { } <EOL> self . top_level = True <EOL> translator . PYJSLIB_BUILTIN_MAPPING [ '<STR_LIT>' ] = "<STR_LIT>" % module_file_name <EOL> self . import_context = "<STR_LIT:null>" <EOL> self . w ( self . indent ( ) + "<STR_LIT>" ) <EOL> self . w ( self . spacing ( ) + "<STR_LIT>" ) <EOL> self . w ( self . spacing ( ) + "<STR_LIT>" ) <EOL> self . w ( self . spacing ( ) + "<STR_LIT>" % self . js_module_name ) <EOL> if self . attribute_checking and not module_name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> attribute_checking = True <EOL> self . w ( self . indent ( ) + '<STR_LIT>' ) <EOL> else : <EOL> attribute_checking = False <EOL> save_output = self . output <EOL> self . output = StringIO ( ) <EOL> mod . lineno = <NUM_LIT:1> <EOL> self . track_lineno ( mod , True ) <EOL> for child in mod . node : <EOL> self . has_js_return = False <EOL> self . has_yield = False <EOL> self . is_generator = False <EOL> self . track_lineno ( child ) <EOL> assert self . top_level <EOL> if isinstance ( child , self . ast . Function ) : <EOL> self . _function ( child , None ) <EOL> elif isinstance ( child , self . ast . Class ) : <EOL> self . _class ( child ) <EOL> elif isinstance ( child , self . ast . Import ) : <EOL> self . _import ( child , None , True ) <EOL> elif isinstance ( child , self . ast . From ) : <EOL> self . _from ( child , None , True ) <EOL> elif isinstance ( child , self . ast . Discard ) : <EOL> self . _discard ( child , None ) <EOL> elif isinstance ( child , self . ast . Assign ) : <EOL> self . _assign ( child , None ) <EOL> elif isinstance ( child , self . ast . AugAssign ) : <EOL> self . _augassign ( child , None ) <EOL> elif isinstance ( child , self . ast . If ) : <EOL> self . _if ( child , None ) <EOL> elif isinstance ( child , self . ast . For ) : <EOL> self . _for ( child , None ) <EOL> elif isinstance ( child , self . ast . While ) : <EOL> self . _while ( child , None ) <EOL> elif isinstance ( child , self . ast . Subscript ) : <EOL> self . _subscript_stmt ( child , None ) <EOL> elif isinstance ( child , self . ast . Global ) : <EOL> self . _global ( child , None ) <EOL> elif isinstance ( child , self . ast . Printnl ) : <EOL> self . _print ( child , None ) <EOL> elif isinstance ( child , self . ast . Print ) : <EOL> self . _print ( child , None ) <EOL> elif isinstance ( child , self . ast . TryExcept ) : <EOL> self . _tryExcept ( child , None ) <EOL> elif isinstance ( child , self . ast . TryFinally ) : <EOL> self . _tryFinally ( child , None ) <EOL> elif isinstance ( child , self . ast . Raise ) : <EOL> self . _raise ( child , None ) <EOL> elif isinstance ( child , self . ast . Stmt ) : <EOL> self . _stmt ( child , None , True ) <EOL> elif isinstance ( child , self . ast . With ) : <EOL> self . _with ( child , None ) <EOL> elif isinstance ( child , self . ast . AssAttr ) : <EOL> self . _assattr ( child , None ) <EOL> elif isinstance ( child , self . ast . AssName ) : <EOL> self . _assname ( child , None ) <EOL> elif isinstance ( child , self . ast . AssTuple ) : <EOL> for node in child . nodes : <EOL> self . _stmt ( node , None ) <EOL> elif isinstance ( child , self . ast . Slice ) : <EOL> self . w ( self . spacing ( ) + self . _slice ( child , None ) ) <EOL> else : <EOL> raise translator . TranslationError ( <EOL> "<STR_LIT>" , <EOL> child , self . module_name ) <EOL> captured_output = self . output . getvalue ( ) <EOL> self . output = save_output <EOL> if self . source_tracking and self . store_source : <EOL> for l in self . track_lines . keys ( ) : <EOL> self . w ( self . spacing ( ) + '''<STR_LIT>''' % ( self . js_module_name , l , self . track_lines [ l ] . replace ( '<STR_LIT:">' , '<STR_LIT>' ) ) , translate = False ) <EOL> self . w ( self . local_js_vars_decl ( [ ] ) ) <EOL> if captured_output . find ( "<STR_LIT>" ) >= <NUM_LIT:0> : <EOL> captured_output = captured_output . replace ( "<STR_LIT>" , self . constant_decl ( ) ) <EOL> else : <EOL> self . w ( self . constant_decl ( ) ) <EOL> if captured_output . find ( "<STR_LIT>" ) >= <NUM_LIT:0> : <EOL> captured_output = captured_output . replace ( "<STR_LIT>" , self . attrib_remap_decl ( ) ) <EOL> self . w ( captured_output , False ) <EOL> if attribute_checking : <EOL> self . w ( self . dedent ( ) + "<STR_LIT>" ) <EOL> self . w ( self . dedent ( ) + "<STR_LIT>" ) </s>
