<s> import sys , inspect <EOL> from django import VERSION as django_version <EOL> if django_version >= ( <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:0> ) : <EOL> import json <EOL> else : <EOL> from django . utils import simplejson as json <EOL> from exceptions import ServiceException <EOL> def servicemethod ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> options = { '<STR_LIT:name>' : None , '<STR_LIT:store>' : None , '<STR_LIT>' : True , '<STR_LIT>' : True } <EOL> method = ( ( len ( args ) == <NUM_LIT:1> ) and callable ( args [ <NUM_LIT:0> ] ) ) and args [ <NUM_LIT:0> ] or None <EOL> if method is None : <EOL> options . update ( { <EOL> '<STR_LIT:name>' : bool ( args ) and args [ <NUM_LIT:0> ] or kwargs . pop ( '<STR_LIT:name>' , None ) , <EOL> '<STR_LIT:store>' : ( len ( args ) >= <NUM_LIT:2> ) and args [ <NUM_LIT:1> ] or kwargs . pop ( '<STR_LIT:store>' , None ) , <EOL> '<STR_LIT>' : kwargs . pop ( '<STR_LIT>' , True ) , <EOL> '<STR_LIT>' : kwargs . pop ( '<STR_LIT>' , True ) , <EOL> } ) <EOL> else : <EOL> options [ '<STR_LIT:name>' ] = method . __name__ <EOL> method . __servicemethod__ = options <EOL> def method_with_args_wrapper ( method ) : <EOL> """<STR_LIT>""" <EOL> if options [ '<STR_LIT:name>' ] is None : <EOL> options [ '<STR_LIT:name>' ] = method . __name__ <EOL> method . __servicemethod__ = options <EOL> if options [ '<STR_LIT:store>' ] is not None : <EOL> options [ '<STR_LIT:store>' ] . service . add_method ( method ) <EOL> return method <EOL> return method or method_with_args_wrapper <EOL> class BaseService ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . methods = { } <EOL> self . _store = None <EOL> def _get_store ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _store <EOL> def _set_store ( self , store ) : <EOL> """<STR_LIT>""" <EOL> for method in self . methods . values ( ) : <EOL> method . __servicemethod__ [ '<STR_LIT:store>' ] = store <EOL> self . _store = store <EOL> store = property ( _get_store , _set_store ) <EOL> def _get_method_args ( self , method , request , params ) : <EOL> """<STR_LIT>""" <EOL> idx = <NUM_LIT:0> <EOL> if method . __servicemethod__ [ '<STR_LIT>' ] : <EOL> params . insert ( idx , method . __servicemethod__ [ '<STR_LIT:store>' ] ) <EOL> idx += <NUM_LIT:1> <EOL> if method . __servicemethod__ [ '<STR_LIT>' ] : <EOL> params . insert ( idx , request ) <EOL> return params <EOL> def add_method ( self , method , name = None , request_arg = True , store_arg = True ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( method , '<STR_LIT>' ) : <EOL> options = method . __servicemethod__ <EOL> else : <EOL> options = { '<STR_LIT:name>' : name or method . __name__ , '<STR_LIT:store>' : self . store , <EOL> '<STR_LIT>' : request_arg , '<STR_LIT>' : store_arg } <EOL> method . __servicemethod__ = options <EOL> self . methods [ options [ '<STR_LIT:name>' ] ] = method <EOL> def get_method ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . methods [ name ] <EOL> except KeyError : <EOL> raise ServiceException ( '<STR_LIT>' % name ) <EOL> def list_methods ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . methods . keys ( ) <EOL> def process_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def process_response ( self , id , result ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def process_error ( self , id , code , error ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def get_smd ( self , url ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> class JsonService ( BaseService ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:POST>' : <EOL> response = self . process_request ( request ) <EOL> else : <EOL> response = self . get_smd ( request . get_full_path ( ) ) <EOL> return json . dumps ( response ) <EOL> def process_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> data = json . loads ( request . raw_post_data ) <EOL> id , method_name , params = data [ "<STR_LIT:id>" ] , data [ "<STR_LIT>" ] , data [ "<STR_LIT>" ] <EOL> except : <EOL> return self . process_error ( <NUM_LIT:0> , <NUM_LIT:100> , '<STR_LIT>' ) <EOL> try : <EOL> method = self . get_method ( method_name ) <EOL> except ServiceException : <EOL> return self . process_error ( id , <NUM_LIT:100> , '<STR_LIT>' % method_name ) <EOL> params = self . _get_method_args ( method , request , params ) <EOL> try : <EOL> result = method ( * params ) <EOL> return self . process_response ( id , result ) <EOL> except BaseException : <EOL> etype , eval , etb = sys . exc_info ( ) <EOL> return self . process_error ( id , <NUM_LIT:100> , '<STR_LIT>' % ( etype . __name__ , eval ) ) <EOL> except : <EOL> etype , eval , etb = sys . exc_info ( ) <EOL> return self . process_error ( id , <NUM_LIT:100> , '<STR_LIT>' % ( etype , eval ) ) <EOL> def process_response ( self , id , result ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : id , <EOL> '<STR_LIT:result>' : result , <EOL> '<STR_LIT:error>' : None , <EOL> } <EOL> def process_error ( self , id , code , error ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT:id>' : id , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:error>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : code , <EOL> '<STR_LIT:message>' : error , <EOL> } , <EOL> } <EOL> def get_smd ( self , url ) : <EOL> """<STR_LIT>""" <EOL> smd = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : url , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> for name , method in self . methods . items ( ) : <EOL> idx = <NUM_LIT:0> <EOL> idx += method . __servicemethod__ [ '<STR_LIT>' ] and <NUM_LIT:1> or <NUM_LIT:0> <EOL> idx += method . __servicemethod__ [ '<STR_LIT>' ] and <NUM_LIT:1> or <NUM_LIT:0> <EOL> sig = inspect . getargspec ( method ) <EOL> smd [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : [ { '<STR_LIT:name>' : val } for val in sig . args [ idx : ] ] <EOL> } ) <EOL> return smd </s>
