<s> """<STR_LIT>""" <EOL> from tornado . web import RequestHandler <EOL> import tornado . web <EOL> import tornado . ioloop <EOL> import tornado . httpserver <EOL> import types <EOL> import traceback <EOL> from tornadorpc . utils import getcallargs <EOL> class Config ( object ) : <EOL> verbose = True <EOL> short_errors = True <EOL> config = Config ( ) <EOL> class BaseRPCParser ( object ) : <EOL> """<STR_LIT>""" <EOL> content_type = '<STR_LIT>' <EOL> def __init__ ( self , library , encode = None , decode = None ) : <EOL> self . library = library <EOL> if not encode : <EOL> encode = getattr ( library , '<STR_LIT>' ) <EOL> if not decode : <EOL> decode = getattr ( library , '<STR_LIT>' ) <EOL> self . encode = encode <EOL> self . decode = decode <EOL> self . requests_in_progress = <NUM_LIT:0> <EOL> self . responses = [ ] <EOL> @ property <EOL> def faults ( self ) : <EOL> return Faults ( self ) <EOL> def run ( self , handler , request_body ) : <EOL> """<STR_LIT>""" <EOL> self . handler = handler <EOL> try : <EOL> requests = self . parse_request ( request_body ) <EOL> except : <EOL> self . traceback ( ) <EOL> return self . handler . result ( self . faults . parse_error ( ) ) <EOL> if not isinstance ( requests , types . TupleType ) : <EOL> if isinstance ( requests , basestring ) : <EOL> return requests <EOL> elif hasattr ( requests , '<STR_LIT>' ) : <EOL> return requests . response ( ) <EOL> elif hasattr ( requests , '<STR_LIT>' ) : <EOL> return self . handler . result ( requests ) <EOL> else : <EOL> return requests <EOL> self . handler . _requests = len ( requests ) <EOL> for request in requests : <EOL> self . dispatch ( request [ <NUM_LIT:0> ] , request [ <NUM_LIT:1> ] ) <EOL> def dispatch ( self , method_name , params ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( RequestHandler , method_name ) : <EOL> return self . handler . result ( self . faults . method_not_found ( ) ) <EOL> method = self . handler <EOL> method_list = dir ( method ) <EOL> method_list . sort ( ) <EOL> attr_tree = method_name . split ( '<STR_LIT:.>' ) <EOL> try : <EOL> for attr_name in attr_tree : <EOL> method = self . check_method ( attr_name , method ) <EOL> except AttributeError : <EOL> return self . handler . result ( self . faults . method_not_found ( ) ) <EOL> if not callable ( method ) : <EOL> return self . handler . result ( self . faults . method_not_found ( ) ) <EOL> if method_name . startswith ( '<STR_LIT:_>' ) or getattr ( method , '<STR_LIT>' , False ) is True : <EOL> return self . handler . result ( self . faults . method_not_found ( ) ) <EOL> args = [ ] <EOL> kwargs = { } <EOL> if isinstance ( params , dict ) : <EOL> kwargs = params <EOL> elif type ( params ) in ( list , tuple ) : <EOL> args = params <EOL> else : <EOL> return self . handler . result ( self . faults . invalid_params ( ) ) <EOL> try : <EOL> final_kwargs , extra_args = getcallargs ( method , * args , ** kwargs ) <EOL> except TypeError : <EOL> return self . handler . result ( self . faults . invalid_params ( ) ) <EOL> try : <EOL> response = method ( * extra_args , ** final_kwargs ) <EOL> except Exception : <EOL> self . traceback ( method_name , params ) <EOL> return self . handler . result ( self . faults . internal_error ( ) ) <EOL> if getattr ( method , '<STR_LIT>' , False ) : <EOL> if response is not None : <EOL> return self . handler . result ( self . faults . internal_error ( ) ) <EOL> else : <EOL> return self . handler . result ( response ) <EOL> def response ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> handler . _requests -= <NUM_LIT:1> <EOL> if handler . _requests > <NUM_LIT:0> : <EOL> return <EOL> if handler . _RPC_finished : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> handler . _RPC_finished = True <EOL> responses = tuple ( handler . _results ) <EOL> response_text = self . parse_responses ( responses ) <EOL> if type ( response_text ) not in types . StringTypes : <EOL> response_text = self . encode ( response_text ) <EOL> handler . on_result ( response_text ) <EOL> def traceback ( self , method_name = '<STR_LIT>' , params = [ ] ) : <EOL> err_lines = traceback . format_exc ( ) . splitlines ( ) <EOL> err_title = "<STR_LIT>" % method_name <EOL> if len ( params ) > <NUM_LIT:0> : <EOL> err_title = '<STR_LIT>' % ( err_title , repr ( params ) ) <EOL> err_sep = ( '<STR_LIT:->' * len ( err_title ) ) [ : <NUM_LIT> ] <EOL> err_lines = [ err_sep , err_title , err_sep ] + err_lines <EOL> if config . verbose : <EOL> if len ( err_lines ) >= <NUM_LIT:7> and config . short_errors : <EOL> print '<STR_LIT:\n>' . join ( err_lines [ <NUM_LIT:0> : <NUM_LIT:4> ] + err_lines [ - <NUM_LIT:3> : ] ) <EOL> else : <EOL> print '<STR_LIT:\n>' . join ( err_lines ) <EOL> return <EOL> def parse_request ( self , request_body ) : <EOL> """<STR_LIT>""" <EOL> return ( [ ] , [ ] ) <EOL> def parse_responses ( self , responses ) : <EOL> """<STR_LIT>""" <EOL> return self . encode ( responses , methodresponse = True ) <EOL> def check_method ( self , attr_name , obj ) : <EOL> """<STR_LIT>""" <EOL> if attr_name . startswith ( '<STR_LIT:_>' ) : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> attr = getattr ( obj , attr_name ) <EOL> if getattr ( attr , '<STR_LIT>' , False ) : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> return attr <EOL> class BaseRPCHandler ( RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> _RPC_ = None <EOL> _results = None <EOL> _requests = <NUM_LIT:0> <EOL> _RPC_finished = False <EOL> @ tornado . web . asynchronous <EOL> def post ( self ) : <EOL> self . _results = [ ] <EOL> request_body = self . request . body <EOL> self . _RPC_ . run ( self , request_body ) <EOL> def result ( self , result , * results ) : <EOL> """<STR_LIT>""" <EOL> if results : <EOL> results = [ result ] + results <EOL> else : <EOL> results = result <EOL> self . _results . append ( results ) <EOL> self . _RPC_ . response ( self ) <EOL> def on_result ( self , response_text ) : <EOL> """<STR_LIT>""" <EOL> self . set_header ( '<STR_LIT:Content-Type>' , self . _RPC_ . content_type ) <EOL> self . finish ( response_text ) <EOL> class FaultMethod ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fault , code , message ) : <EOL> self . fault = fault <EOL> self . code = code <EOL> self . message = message <EOL> def __call__ ( self , message = None ) : <EOL> if message : <EOL> self . message = message <EOL> return self . fault ( self . code , self . message ) <EOL> class Faults ( object ) : <EOL> """<STR_LIT>""" <EOL> codes = { <EOL> '<STR_LIT>' : - <NUM_LIT> , <EOL> '<STR_LIT>' : - <NUM_LIT> , <EOL> '<STR_LIT>' : - <NUM_LIT> , <EOL> '<STR_LIT>' : - <NUM_LIT> , <EOL> '<STR_LIT>' : - <NUM_LIT> <EOL> } <EOL> messages = { } <EOL> def __init__ ( self , parser , fault = None ) : <EOL> self . library = parser . library <EOL> self . fault = fault <EOL> if not self . fault : <EOL> self . fault = getattr ( self . library , '<STR_LIT>' ) <EOL> def __getattr__ ( self , attr ) : <EOL> message = '<STR_LIT>' <EOL> if attr in self . messages . keys ( ) : <EOL> message = self . messages [ attr ] <EOL> else : <EOL> message = '<STR_LIT:U+0020>' . join ( map ( str . capitalize , attr . split ( '<STR_LIT:_>' ) ) ) <EOL> fault = FaultMethod ( self . fault , self . codes [ attr ] , message ) <EOL> return fault <EOL> """<STR_LIT>""" <EOL> def private ( func ) : <EOL> """<STR_LIT>""" <EOL> func . private = True <EOL> return func <EOL> def async ( func ) : <EOL> """<STR_LIT>""" <EOL> func . async = True <EOL> return func <EOL> def start_server ( handlers , route = r'<STR_LIT:/>' , port = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if type ( handlers ) not in ( types . ListType , types . TupleType ) : <EOL> handler = handlers <EOL> handlers = [ ( route , handler ) ] <EOL> if route != '<STR_LIT>' : <EOL> handlers . append ( ( '<STR_LIT>' , handler ) ) <EOL> application = tornado . web . Application ( handlers ) <EOL> http_server = tornado . httpserver . HTTPServer ( application ) <EOL> http_server . listen ( port ) <EOL> loop_instance = tornado . ioloop . IOLoop . instance ( ) <EOL> """<STR_LIT>""" <EOL> for ( route , handler ) in handlers : <EOL> try : <EOL> setattr ( handler , '<STR_LIT>' , loop_instance ) <EOL> except AttributeError : <EOL> handler . _server = loop_instance <EOL> loop_instance . start ( ) <EOL> return loop_instance <EOL> """<STR_LIT>""" <EOL> class TestMethodTree ( object ) : <EOL> def power ( self , x , y = <NUM_LIT:2> ) : <EOL> return pow ( x , y ) <EOL> @ private <EOL> def private ( self ) : <EOL> return False <EOL> class TestRPCHandler ( BaseRPCHandler ) : <EOL> _RPC_ = None <EOL> def add ( self , x , y ) : <EOL> return x + y <EOL> def ping ( self , x ) : <EOL> return x <EOL> def noargs ( self ) : <EOL> return '<STR_LIT>' <EOL> tree = TestMethodTree ( ) <EOL> def _private ( self ) : <EOL> return False <EOL> @ private <EOL> def private ( self ) : <EOL> return False </s>
