<s> from __future__ import print_function <EOL> from collections import defaultdict <EOL> import json <EOL> import optparse <EOL> import platform <EOL> import sys <EOL> import time <EOL> import copy <EOL> timeouts = defaultdict ( list ) <EOL> top_timeout_id = <NUM_LIT:0> <EOL> cancelled_timeouts = set ( ) <EOL> calling_timeouts = False <EOL> def name ( ) : <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> py_version = <NUM_LIT:2> <EOL> else : <EOL> py_version = <NUM_LIT:3> <EOL> return '<STR_LIT>' % py_version <EOL> def ok_cancel_dialog ( dialog ) : <EOL> print ( '<STR_LIT>' , dialog ) <EOL> def error_message ( msg ) : <EOL> print ( msg , file = sys . stderr ) <EOL> def status_message ( msg ) : <EOL> print ( msg ) <EOL> def _platform ( ) : <EOL> return platform . platform ( ) <EOL> def set_timeout ( func , timeout , * args , ** kwargs ) : <EOL> global top_timeout_id <EOL> timeout_id = top_timeout_id <EOL> top_timeout_id + <NUM_LIT:1> <EOL> if top_timeout_id > <NUM_LIT> : <EOL> top_timeout_id = <NUM_LIT:0> <EOL> def timeout_func ( ) : <EOL> if timeout_id in cancelled_timeouts : <EOL> cancelled_timeouts . remove ( timeout_id ) <EOL> return <EOL> func ( * args , ** kwargs ) <EOL> then = time . time ( ) + ( timeout / <NUM_LIT> ) <EOL> timeouts [ then ] . append ( timeout_func ) <EOL> return timeout_id <EOL> def cancel_timeout ( timeout_id ) : <EOL> if timeout_id in timeouts : <EOL> cancelled_timeouts . add ( timeout_id ) <EOL> def call_timeouts ( ) : <EOL> global calling_timeouts <EOL> if calling_timeouts : <EOL> return <EOL> calling_timeouts = True <EOL> now = time . time ( ) <EOL> for t , tos in copy . copy ( timeouts ) . items ( ) : <EOL> if now >= t : <EOL> for timeout in tos : <EOL> timeout ( ) <EOL> del timeouts [ t ] <EOL> calling_timeouts = False <EOL> def open_file ( file ) : <EOL> pass <EOL> try : <EOL> from . common import api , msg , shared as G , utils , reactor , event_emitter <EOL> from . common . handlers import base <EOL> from . common . protocols import floo_proto <EOL> from . import editor <EOL> except ( ImportError , ValueError ) : <EOL> from common import api , msg , shared as G , utils , reactor , event_emitter <EOL> from common . handlers import base <EOL> from common . protocols import floo_proto <EOL> import editor <EOL> def editor_log ( msg ) : <EOL> print ( msg ) <EOL> sys . stdout . flush ( ) <EOL> editor . name = name <EOL> editor . ok_cancel_dialog = ok_cancel_dialog <EOL> editor . error_message = error_message <EOL> editor . status_message = status_message <EOL> editor . platform = _platform <EOL> editor . set_timeout = set_timeout <EOL> editor . cancel_timeout = cancel_timeout <EOL> editor . call_timeouts = call_timeouts <EOL> editor . open_file = open_file <EOL> msg . editor_log = editor_log <EOL> utils . reload_settings ( ) <EOL> eventStream = event_emitter . EventEmitter ( ) <EOL> def conn_log ( action , item ) : <EOL> try : <EOL> item = item . decode ( '<STR_LIT:utf-8>' ) <EOL> except Exception : <EOL> pass <EOL> if G . SOCK_DEBUG : <EOL> msg . log ( action , '<STR_LIT>' , item ) <EOL> sys . stdout . flush ( ) <EOL> eventStream . on ( '<STR_LIT>' , lambda x : conn_log ( '<STR_LIT>' , x ) ) <EOL> eventStream . on ( '<STR_LIT>' , lambda x : conn_log ( '<STR_LIT>' , x ) ) <EOL> class RemoteProtocol ( floo_proto . FlooProtocol ) : <EOL> '''<STR_LIT>''' <EOL> MAX_RETRIES = - <NUM_LIT:1> <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( RemoteProtocol , self ) . __init__ ( * args , ** kwargs ) <EOL> eventStream . on ( '<STR_LIT>' , self . _q . append ) <EOL> def _handle ( self , data ) : <EOL> data = data . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:ignore>' ) <EOL> eventStream . emit ( '<STR_LIT>' , data ) <EOL> def reconnect ( self ) : <EOL> msg . error ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> class FlooConn ( base . BaseHandler ) : <EOL> PROTOCOL = RemoteProtocol <EOL> def __init__ ( self , server ) : <EOL> super ( FlooConn , self ) . __init__ ( ) <EOL> def tick ( self ) : <EOL> pass <EOL> def on_connect ( self ) : <EOL> msg . log ( '<STR_LIT>' ) <EOL> eventStream . emit ( '<STR_LIT>' ) <EOL> class LocalProtocol ( floo_proto . FlooProtocol ) : <EOL> '''<STR_LIT>''' <EOL> MAX_RETRIES = - <NUM_LIT:1> <EOL> INITIAL_RECONNECT_DELAY = <NUM_LIT:0> <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( LocalProtocol , self ) . __init__ ( * args , ** kwargs ) <EOL> eventStream . on ( '<STR_LIT>' , self . _q . append ) <EOL> self . to_proxy = [ ] <EOL> self . remote_conn = False <EOL> eventStream . on ( '<STR_LIT>' , self . on_remote_conn ) <EOL> def connect ( self , sock = None ) : <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . _sock = sock <EOL> self . connected = True <EOL> def reconnect ( self ) : <EOL> msg . error ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def stop ( self ) : <EOL> self . cleanup ( ) <EOL> def on_remote_conn ( self ) : <EOL> self . remote_conn = True <EOL> while self . to_proxy : <EOL> item = self . to_proxy . pop ( <NUM_LIT:0> ) <EOL> eventStream . emit ( '<STR_LIT>' , item . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def _handle ( self , data ) : <EOL> if self . remote_conn : <EOL> eventStream . emit ( '<STR_LIT>' , data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> else : <EOL> self . to_proxy . append ( data ) <EOL> remote_host = G . DEFAULT_HOST <EOL> remote_port = G . DEFAULT_PORT <EOL> remote_ssl = True <EOL> class Server ( base . BaseHandler ) : <EOL> PROTOCOL = LocalProtocol <EOL> def on_connect ( self ) : <EOL> self . conn = FlooConn ( self ) <EOL> reactor . reactor . connect ( self . conn , remote_host , remote_port , remote_ssl ) <EOL> try : <EOL> import urllib <EOL> HTTPError = urllib . error . HTTPError <EOL> URLError = urllib . error . URLError <EOL> except ( AttributeError , ImportError , ValueError ) : <EOL> import urllib2 <EOL> HTTPError = urllib2 . HTTPError <EOL> URLError = urllib2 . URLError <EOL> def main ( ) : <EOL> global remote_host , remote_port , remote_ssl <EOL> msg . LOG_LEVEL = msg . LOG_LEVELS [ '<STR_LIT>' ] <EOL> usage = '<STR_LIT>' <EOL> parser = optparse . OptionParser ( usage = usage ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT:url>' , <EOL> default = None <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT:data>' , <EOL> default = None <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> default = None <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT:host>' , <EOL> default = None <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT:port>' , <EOL> default = None <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> default = None <EOL> ) <EOL> options , args = parser . parse_args ( ) <EOL> if options . url : <EOL> data = None <EOL> err = False <EOL> if options . data : <EOL> data = json . loads ( options . data ) <EOL> try : <EOL> r = api . hit_url ( options . host , options . url , data , options . method ) <EOL> except HTTPError as e : <EOL> r = e <EOL> except URLError as e : <EOL> r = e <EOL> err = True <EOL> try : <EOL> print ( r . code ) <EOL> except Exception : <EOL> err = True <EOL> if err : <EOL> print ( r . reason ) <EOL> else : <EOL> print ( r . read ( ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> sys . exit ( err ) <EOL> if not options . host : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> remote_host = options . host <EOL> remote_port = int ( options . port ) or remote_port <EOL> remote_ssl = bool ( options . ssl ) or remote_ssl <EOL> proxy = Server ( ) <EOL> _ , port = reactor . reactor . listen ( proxy , port = int ( G . PROXY_PORT ) ) <EOL> def on_ready ( ) : <EOL> print ( '<STR_LIT>' % port ) <EOL> sys . stdout . flush ( ) <EOL> utils . set_timeout ( on_ready , <NUM_LIT:100> ) <EOL> try : <EOL> reactor . reactor . block ( ) <EOL> except KeyboardInterrupt : <EOL> print ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
