<s> """<STR_LIT>""" <EOL> from . . pyscript import py2js , undefined , window <EOL> flexx_session_id = location = root = module = typeof = None <EOL> @ py2js ( inline_stdlib = False ) <EOL> class FlexxJS : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . ws = None <EOL> self . last_msg = None <EOL> self . is_notebook = False <EOL> loc = location <EOL> self . ws_url = ( '<STR_LIT>' % ( loc . hostname , loc . port , loc . pathname ) ) <EOL> self . is_exported = False <EOL> self . classes = { } <EOL> self . instances = { } <EOL> if typeof ( window ) is '<STR_LIT>' and typeof ( module ) is '<STR_LIT:object>' : <EOL> root . window = root <EOL> self . nodejs = True <EOL> window . setTimeout ( self . init , <NUM_LIT:1> ) <EOL> window . process . on ( '<STR_LIT>' , self . exit , False ) <EOL> window . process . on ( '<STR_LIT>' , self . exit , False ) <EOL> window . setTimeout ( self . exit , <NUM_LIT> ) <EOL> else : <EOL> window . addEventListener ( '<STR_LIT>' , self . init , False ) <EOL> window . addEventListener ( '<STR_LIT>' , self . exit , False ) <EOL> def init ( self ) : <EOL> """<STR_LIT>""" <EOL> if window . flexx . is_exported : <EOL> window . flexx . runExportedApp ( ) <EOL> elif window . flexx . is_notebook and not ( window . IPython and <EOL> window . IPython . notebook and <EOL> window . IPython . notebook . session ) : <EOL> print ( '<STR_LIT>' ) <EOL> else : <EOL> window . flexx . initSocket ( ) <EOL> window . flexx . initLogging ( ) <EOL> def exit ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . ws : <EOL> self . ws . close ( ) <EOL> self . ws = None <EOL> def get ( self , id ) : <EOL> """<STR_LIT>""" <EOL> if id == '<STR_LIT:body>' : <EOL> return window . document . body <EOL> else : <EOL> return self . instances [ id ] <EOL> def initSocket ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . nodejs : <EOL> try : <EOL> WebSocket = window . require ( '<STR_LIT>' ) <EOL> except Exception : <EOL> raise "<STR_LIT>" <EOL> else : <EOL> WebSocket = window . WebSocket <EOL> if ( WebSocket is undefined ) : <EOL> window . document . body . innerHTML = '<STR_LIT>' <EOL> raise "<STR_LIT>" <EOL> self . ws = ws = WebSocket ( window . flexx . ws_url ) <EOL> ws . binaryType = "<STR_LIT>" <EOL> def on_ws_open ( evt ) : <EOL> window . console . info ( '<STR_LIT>' ) <EOL> ws . send ( '<STR_LIT>' + window . flexx_session_id ) <EOL> def on_ws_message ( evt ) : <EOL> window . flexx . last_msg = evt . data or evt <EOL> msg = window . flexx . decodeUtf8 ( window . flexx . last_msg ) <EOL> window . flexx . command ( msg ) <EOL> def on_ws_close ( evt ) : <EOL> self . ws = None <EOL> msg = '<STR_LIT>' <EOL> if evt and evt . reason : <EOL> msg += '<STR_LIT>' % ( evt . reason , evt . code ) <EOL> if ( not window . flexx . is_notebook ) and ( not self . nodejs ) : <EOL> window . document . body . innerHTML = msg <EOL> else : <EOL> window . console . info ( msg ) <EOL> def on_ws_error ( self , evt ) : <EOL> self . ws = None <EOL> window . console . error ( '<STR_LIT>' ) <EOL> if self . nodejs : <EOL> ws . on ( '<STR_LIT>' , on_ws_open ) <EOL> ws . on ( '<STR_LIT:message>' , on_ws_message ) <EOL> ws . on ( '<STR_LIT>' , on_ws_close ) <EOL> ws . on ( '<STR_LIT:error>' , on_ws_error ) <EOL> else : <EOL> ws . onopen = on_ws_open <EOL> ws . onmessage = on_ws_message <EOL> ws . onclose = on_ws_close <EOL> ws . onerror = on_ws_error <EOL> def initLogging ( self ) : <EOL> """<STR_LIT>""" <EOL> if window . console . ori_log : <EOL> return <EOL> window . console . ori_log = window . console . log <EOL> window . console . ori_info = window . console . info or window . console . log <EOL> window . console . ori_warn = window . console . warn or window . console . log <EOL> window . console . ori_error = window . console . error or window . console . log <EOL> def log ( self , msg ) : <EOL> window . console . ori_log ( msg ) <EOL> if window . flexx . ws is not None : <EOL> window . flexx . ws . send ( "<STR_LIT>" + msg ) <EOL> def info ( self , msg ) : <EOL> window . console . ori_info ( msg ) <EOL> if window . flexx . ws is not None : <EOL> window . flexx . ws . send ( "<STR_LIT>" + msg ) <EOL> def warn ( self , msg ) : <EOL> window . console . ori_warn ( msg ) <EOL> if window . flexx . ws is not None : <EOL> window . flexx . ws . send ( "<STR_LIT>" + msg ) <EOL> def error ( self , msg ) : <EOL> window . console . ori_error ( msg ) <EOL> if window . flexx . ws is not None : <EOL> window . flexx . ws . send ( "<STR_LIT>" + msg ) <EOL> def on_error ( self , evt ) : <EOL> msg = evt . message <EOL> if evt . error . stack : <EOL> stack = [ x . replace ( '<STR_LIT:@>' , '<STR_LIT>' ) if '<STR_LIT>' in x else x . split ( '<STR_LIT:@>' ) [ <NUM_LIT:0> ] <EOL> for x in evt . error . stack . splitlines ( ) ] <EOL> msg += '<STR_LIT:\n>' + '<STR_LIT:\n>' . join ( stack ) <EOL> elif evt . message and evt . lineno : <EOL> msg += "<STR_LIT>" % ( evt . filename , evt . lineno ) <EOL> window . console . error ( msg ) <EOL> evt . preventDefault ( ) <EOL> window . console . log = log <EOL> window . console . info = info <EOL> window . console . warn = warn <EOL> window . console . error = error <EOL> if self . nodejs : <EOL> window . process . on ( '<STR_LIT>' , on_error , False ) <EOL> else : <EOL> window . addEventListener ( '<STR_LIT:error>' , on_error , False ) <EOL> def command ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if msg . startswith ( '<STR_LIT>' ) : <EOL> window . console . ori_log ( msg [ <NUM_LIT:6> : ] ) <EOL> elif msg . startswith ( '<STR_LIT>' ) : <EOL> window . _ = eval ( msg [ <NUM_LIT:5> : ] ) <EOL> window . flexx . ws . send ( '<STR_LIT>' + window . _ ) <EOL> elif msg . startswith ( '<STR_LIT>' ) : <EOL> eval ( msg [ <NUM_LIT:5> : ] ) <EOL> elif msg . startswith ( '<STR_LIT>' ) : <EOL> eval ( msg [ <NUM_LIT:10> : ] ) <EOL> elif msg . startswith ( '<STR_LIT>' ) : <EOL> el = window . document . createElement ( "<STR_LIT>" ) <EOL> el . type = "<STR_LIT>" <EOL> el . innerHTML = msg [ <NUM_LIT:11> : ] <EOL> window . document . body . appendChild ( el ) <EOL> elif msg . startswith ( '<STR_LIT>' ) : <EOL> if not self . nodejs : <EOL> window . document . title = msg [ <NUM_LIT:6> : ] <EOL> elif msg . startswith ( '<STR_LIT>' ) : <EOL> if not self . nodejs : <EOL> link = window . document . createElement ( '<STR_LIT>' ) <EOL> link . rel = '<STR_LIT>' <EOL> link . href = msg [ <NUM_LIT:5> : ] <EOL> window . document . head . appendChild ( link ) <EOL> elif msg . startswith ( '<STR_LIT>' ) : <EOL> window . win1 = window . open ( msg [ <NUM_LIT:5> : ] , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> window . console . warn ( '<STR_LIT>' + msg + '<STR_LIT:">' ) <EOL> def decodeUtf8 ( self , arrayBuffer ) : <EOL> """<STR_LIT>""" </s>
