<s> from __future__ import print_function <EOL> from six . moves . xmlrpc_server import SimpleXMLRPCServer <EOL> import os <EOL> import sys <EOL> import time <EOL> from threading import Timer <EOL> import larch <EOL> from larch . interpreter import Interpreter <EOL> from larch . inputText import InputText <EOL> from larch . utils . jsonutils import encode4js <EOL> if not hasattr ( sys , '<STR_LIT>' ) : <EOL> try : <EOL> import wxversion <EOL> wxversion . ensureMinimal ( '<STR_LIT>' ) <EOL> except : <EOL> pass <EOL> try : <EOL> import wx <EOL> from larch . wxlib import inputhook <EOL> HAS_WX = True <EOL> except ImportError : <EOL> HAS_WX = False <EOL> class LarchServer ( SimpleXMLRPCServer ) : <EOL> IDLE_TIME = <NUM_LIT:3> * <NUM_LIT> <EOL> IDLE_POLL_TIME = <NUM_LIT> <EOL> def __init__ ( self , host = '<STR_LIT:127.0.0.1>' , port = <NUM_LIT> , with_wx = True , <EOL> local_echo = True , quiet = False , ** kws ) : <EOL> self . keep_alive = True <EOL> self . port = port <EOL> self . with_wx = HAS_WX and with_wx <EOL> self . local_echo = local_echo <EOL> self . quiet = quiet <EOL> self . out_buffer = [ ] <EOL> self . keep_alive_time = time . time ( ) + self . IDLE_TIME <EOL> SimpleXMLRPCServer . __init__ ( self , ( host , port ) , <EOL> logRequests = False , allow_none = True , ** kws ) <EOL> self . initialized = False <EOL> self . register_introspection_functions ( ) <EOL> self . register_function ( self . list_dir , '<STR_LIT>' ) <EOL> self . register_function ( self . change_dir , '<STR_LIT>' ) <EOL> self . register_function ( self . current_dir , '<STR_LIT>' ) <EOL> self . register_function ( self . exit , '<STR_LIT>' ) <EOL> self . register_function ( self . larch_exec , '<STR_LIT>' ) <EOL> self . register_function ( self . wx_update , '<STR_LIT>' ) <EOL> self . register_function ( self . get_data , '<STR_LIT>' ) <EOL> self . register_function ( self . get_messages , '<STR_LIT>' ) <EOL> self . register_function ( self . len_messages , '<STR_LIT>' ) <EOL> def write ( self , text , ** kws ) : <EOL> self . out_buffer . append ( text ) <EOL> if self . local_echo : <EOL> sys . stdout . write ( text ) <EOL> def flush ( self ) : <EOL> if self . local_echo : <EOL> sys . stdout . flush ( ) <EOL> def get_messages ( self ) : <EOL> if self . local_echo : <EOL> print ( '<STR_LIT>' % len ( self . out_buffer ) ) <EOL> out = '<STR_LIT:\n>' . join ( self . out_buffer ) <EOL> self . out_buffer = [ ] <EOL> return out <EOL> def len_messages ( self ) : <EOL> return len ( self . out_buffer ) <EOL> def check_activity ( self ) : <EOL> if ( time . time ( ) > self . keep_alive_time ) or not self . keep_alive : <EOL> print ( "<STR_LIT>" ) <EOL> self . keep_alive = False <EOL> sys . exit ( ) <EOL> else : <EOL> self . timer = Timer ( self . IDLE_POLL_TIME , self . check_activity ) <EOL> self . timer . start ( ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . help ( ) <EOL> self . check_activity ( ) <EOL> try : <EOL> while self . keep_alive : <EOL> self . handle_request ( ) <EOL> except KeyboardInterrupt : <EOL> self . keep_alive = False <EOL> sys . exit ( ) <EOL> def list_dir ( self , dir_name ) : <EOL> """<STR_LIT>""" <EOL> return os . listdir ( dir_name ) <EOL> def change_dir ( self , dir_name ) : <EOL> """<STR_LIT>""" <EOL> return os . chdir ( dir_name ) <EOL> def current_dir ( self ) : <EOL> """<STR_LIT>""" <EOL> ret = os . getcwd ( ) <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> ret = ret . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> return ret <EOL> def help ( self ) : <EOL> if not self . quiet : <EOL> print ( '<STR_LIT>' % repr ( self . port ) ) <EOL> print ( '<STR_LIT>' ) <EOL> fnames = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for kname in self . funcs . keys ( ) : <EOL> if not kname . startswith ( '<STR_LIT>' ) and kname not in fnames : <EOL> fnames . append ( kname ) <EOL> out = '<STR_LIT>' <EOL> for fname in sorted ( fnames ) : <EOL> if len ( out ) == <NUM_LIT:0> : <EOL> out = fname <EOL> else : <EOL> out = "<STR_LIT>" % ( out , fname ) <EOL> if len ( out ) > <NUM_LIT> : <EOL> print ( "<STR_LIT>" % out ) <EOL> out = '<STR_LIT>' <EOL> if len ( out ) > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" % out ) <EOL> def exit ( self , app = None , ** kws ) : <EOL> "<STR_LIT>" <EOL> self . keep_alive = False <EOL> return <NUM_LIT:1> <EOL> def initialize_larch ( self ) : <EOL> self . larch = Interpreter ( writer = self ) <EOL> self . input = InputText ( prompt = '<STR_LIT>' , _larch = self . larch , <EOL> interactive = False ) <EOL> self . larch . symtable . set_symbol ( '<STR_LIT>' , False ) <EOL> self . larch . run_init_scripts ( ) <EOL> self . wxapp = None <EOL> self . wx_evtloop = None <EOL> if self . with_wx : <EOL> self . larch . symtable . set_symbol ( '<STR_LIT>' , inputhook ) <EOL> self . larch . symtable . set_symbol ( '<STR_LIT>' , False ) <EOL> self . wxapp = wx . App ( redirect = False , clearSigInt = False ) <EOL> self . wx_evtloop = inputhook . EventLoopRunner ( parent = self . wxapp ) <EOL> self . larch . symtable . set_symbol ( '<STR_LIT>' , self . wxapp ) <EOL> self . larch . symtable . set_symbol ( '<STR_LIT>' , None ) <EOL> inputhook . ON_INTERRUPT = self . exit <EOL> inputhook . WXLARCH_SYM = self . larch . symtable <EOL> self . initialized = True <EOL> def wx_update ( self , timeout ) : <EOL> "<STR_LIT>" <EOL> if self . wx_evtloop is not None : <EOL> t0 = time . time ( ) <EOL> while time . time ( ) - t0 < timeout : <EOL> self . larch . symtable . set_symbol ( '<STR_LIT>' , True ) <EOL> self . wx_evtloop . run ( poll_time = <NUM_LIT:5> ) <EOL> return True <EOL> def get_data ( self , expr ) : <EOL> "<STR_LIT>" <EOL> self . keep_alive_time = time . time ( ) + self . IDLE_TIME <EOL> return encode4js ( self . larch . eval ( expr ) ) <EOL> def larch_exec ( self , text , debug = True ) : <EOL> "<STR_LIT>" <EOL> self . debug = debug <EOL> if not self . initialized : <EOL> self . initialize_larch ( ) <EOL> text = text . strip ( ) <EOL> if text in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . exit ( ) <EOL> else : <EOL> ret = None <EOL> self . input . put ( text , lineno = <NUM_LIT:0> ) <EOL> while len ( self . input ) > <NUM_LIT:0> : <EOL> block , fname , lineno = self . input . get ( ) <EOL> if len ( block ) == <NUM_LIT:0> : <EOL> continue <EOL> if self . local_echo : <EOL> print ( block ) <EOL> ret = self . larch . eval ( block , fname = fname , lineno = lineno ) <EOL> if self . larch . error : <EOL> err = self . larch . error . pop ( <NUM_LIT:0> ) <EOL> fname , lineno = err . fname , err . lineno <EOL> self . write ( "<STR_LIT>" % err . get_error ( ) [ <NUM_LIT:1> ] ) <EOL> for err in self . larch . error : <EOL> if self . debug or ( ( err . fname != fname or err . lineno != lineno ) <EOL> and err . lineno > <NUM_LIT:0> and lineno > <NUM_LIT:0> ) : <EOL> self . write ( "<STR_LIT>" % ( err . get_error ( ) [ <NUM_LIT:1> ] ) ) <EOL> self . input . clear ( ) <EOL> break <EOL> elif ret is not None : <EOL> self . write ( "<STR_LIT>" % repr ( ret ) ) <EOL> self . keep_alive_time = time . time ( ) + self . IDLE_TIME <EOL> return ret is None <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> s = LarchServer ( host = '<STR_LIT:localhost>' , port = <NUM_LIT> ) <EOL> s . run ( ) </s>
