<s> """<STR_LIT>""" <EOL> PARSE_ERROR = - <NUM_LIT> <EOL> INVALID_REQUEST = - <NUM_LIT> <EOL> METHOD_NOT_FOUND = - <NUM_LIT> <EOL> INVALID_PARAMS = - <NUM_LIT> <EOL> INTERNAL_ERROR = - <NUM_LIT> <EOL> errors = { } <EOL> errors [ PARSE_ERROR ] = "<STR_LIT>" <EOL> errors [ INVALID_REQUEST ] = "<STR_LIT>" <EOL> errors [ METHOD_NOT_FOUND ] = "<STR_LIT>" <EOL> errors [ INVALID_PARAMS ] = "<STR_LIT>" <EOL> errors [ INTERNAL_ERROR ] = "<STR_LIT>" <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> try : <EOL> import django . utils . simplejson as json <EOL> except ImportError : <EOL> import simplejson as json <EOL> import sys <EOL> import logging <EOL> import itertools <EOL> from werkzeug import Request , Response <EOL> from werkzeug import exceptions <EOL> class JsonRpcApplication ( object ) : <EOL> def __init__ ( self , methods = None ) : <EOL> if methods is not None : <EOL> self . methods = methods <EOL> else : <EOL> self . methods = { } <EOL> def add_module ( self , mod , namespace = None ) : <EOL> if namespace is None : <EOL> namespace = mod . __name__ <EOL> for k , v in ( ( k , v ) for k , v in mod . __dict__ . iteritems ( ) <EOL> if not k . startswith ( '<STR_LIT:_>' ) and callable ( v ) ) : <EOL> self . add ( namespace + '<STR_LIT:.>' + k , v ) <EOL> def add ( self , name , func ) : <EOL> self . methods [ name ] = func <EOL> def process ( self , data ) : <EOL> if data . get ( '<STR_LIT>' ) != "<STR_LIT>" : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : data . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT:error>' : { '<STR_LIT:code>' : INVALID_REQUEST , <EOL> '<STR_LIT:message>' : errors [ INVALID_REQUEST ] } } <EOL> if '<STR_LIT>' not in data : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : data . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT:error>' : { '<STR_LIT:code>' : INVALID_REQUEST , <EOL> '<STR_LIT:message>' : errors [ INVALID_REQUEST ] } } <EOL> methodname = data [ '<STR_LIT>' ] <EOL> if not isinstance ( methodname , basestring ) : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : data . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT:error>' : { '<STR_LIT:code>' : INVALID_REQUEST , <EOL> '<STR_LIT:message>' : errors [ INVALID_REQUEST ] } } <EOL> if methodname . startswith ( '<STR_LIT:_>' ) : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : data . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT:error>' : { '<STR_LIT:code>' : METHOD_NOT_FOUND , <EOL> '<STR_LIT:message>' : errors [ METHOD_NOT_FOUND ] } } <EOL> if methodname not in self . methods : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : data . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT:error>' : { '<STR_LIT:code>' : METHOD_NOT_FOUND , <EOL> '<STR_LIT:message>' : errors [ METHOD_NOT_FOUND ] } } <EOL> method = self . methods [ methodname ] <EOL> try : <EOL> params = data . get ( '<STR_LIT>' , [ ] ) <EOL> if isinstance ( params , list ) : <EOL> result = method ( * params ) <EOL> elif isinstance ( params , dict ) : <EOL> result = method ( ** dict ( [ ( str ( k ) , v ) for k , v in params . iteritems ( ) ] ) ) <EOL> else : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : data . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT:error>' : { '<STR_LIT:code>' : INVALID_REQUEST , <EOL> '<STR_LIT:message>' : errors [ INVALID_REQUEST ] } } <EOL> resdata = None <EOL> if data . get ( '<STR_LIT:id>' ) : <EOL> resdata = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : data . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT:result>' : result , <EOL> } <EOL> return resdata <EOL> except Exception , e : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : data . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT:error>' : { '<STR_LIT:code>' : INTERNAL_ERROR , <EOL> '<STR_LIT:message>' : errors [ INTERNAL_ERROR ] , <EOL> '<STR_LIT:data>' : str ( e ) } } <EOL> def __call__ ( self , environ , start_response ) : <EOL> request = Request ( environ ) <EOL> if request . method != "<STR_LIT:POST>" : <EOL> raise exceptions . MethodNotAllowed <EOL> if not request . content_type . startswith ( '<STR_LIT:application/json>' ) : <EOL> raise exceptions . BadRequest <EOL> try : <EOL> data = json . loads ( request . data ) <EOL> except ValueError , e : <EOL> resdata = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : None , <EOL> '<STR_LIT:error>' : { '<STR_LIT:code>' : PARSE_ERROR , <EOL> '<STR_LIT:message>' : errors [ PARSE_ERROR ] } } <EOL> else : <EOL> if isinstance ( data , dict ) : <EOL> resdata = self . process ( data ) <EOL> elif isinstance ( data , list ) : <EOL> if len ( [ x for x in data if not isinstance ( x , dict ) ] ) : <EOL> resdata = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : None , <EOL> '<STR_LIT:error>' : { '<STR_LIT:code>' : INVALID_REQUEST , <EOL> '<STR_LIT:message>' : errors [ INVALID_REQUEST ] } } <EOL> else : <EOL> resdata = [ d for d in ( self . process ( d ) for d in data ) <EOL> if d is not None ] <EOL> response = Response ( content_type = "<STR_LIT:application/json>" ) <EOL> if resdata : <EOL> response . headers [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> response . headers [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> response . headers [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> response . data = json . dumps ( resdata ) <EOL> return response ( environ , start_response ) <EOL> def getmod ( modname ) : <EOL> try : <EOL> __import__ ( modname ) <EOL> except ImportError , e : <EOL> logging . warn ( "<STR_LIT>" % e ) <EOL> return None <EOL> mod = sys . modules [ modname ] <EOL> return mod <EOL> def HTTPExceptionMiddleware ( app ) : <EOL> def wrap ( environ , start_response ) : <EOL> try : <EOL> return app ( environ , start_response ) <EOL> except exceptions . HTTPException , e : <EOL> return e ( environ , start_response ) <EOL> return wrap <EOL> def make_application ( methods ) : <EOL> app = JsonRpcApplication ( ) <EOL> for name , value in methods . iteritems ( ) : <EOL> if "<STR_LIT::>" in value : <EOL> modname , funcname = value . split ( "<STR_LIT::>" , <NUM_LIT:1> ) <EOL> mod = getmod ( modname ) <EOL> if mod : <EOL> app . add ( name , getattr ( mod , funcname ) ) <EOL> else : <EOL> modname = value <EOL> mod = getmod ( modname ) <EOL> if mod : <EOL> app . add_module ( mod , name ) <EOL> app = HTTPExceptionMiddleware ( app ) <EOL> return app </s>
