#NEWLINE# def jsonrpc_method( name , authenticated = False , authentication_arguments = [ 'username' , 'password' ] , safe = False , validate = False , site = default_site ) : #NEWLINE# #INDENT# "\n    Wraps a function turns it into a json-rpc method. Adds several attributes\n    to the function specific to the JSON-RPC machinery and adds it to the default\n    jsonrpc_site if one isn't provided. You must import the module containing\n    these functions in your urls.py.\n\n        name\n\n            The name of your method. IE: `namespace.methodName` The method name\n            can include type information, like `ns.method(String, Array) -> Nil`.\n\n        authenticated=False\n\n            Adds `username` and `password` arguments to the beginning of your\n            method if the user hasn't already been authenticated. These will\n            be used to authenticate the user against `django.contrib.authenticate`\n            If you use HTTP auth or other authentication middleware, `username`\n            and `password` will not be added, and this method will only check\n            against `request.user.is_authenticated`.\n\n            You may pass a callable to replace `django.contrib.auth.authenticate`\n            as the authentication method. It must return either a User or `None`\n            and take the keyword arguments `username` and `password`.\n\n        safe=False\n\n            Designates whether or not your method may be accessed by HTTP GET.\n            By default this is turned off.\n\n        validate=False\n\n            Validates the arguments passed to your method based on type\n            information provided in the signature. Supply type information by\n            including types in your method declaration. Like so:\n\n            @jsonrpc_method('myapp.specialSauce(Array, String)', validate=True)\n            def special_sauce(self, ingredients, instructions):\n              return SpecialSauce(ingredients, instructions)\n\n            Calls to `myapp.specialSauce` will now check each arguments type\n            before calling `special_sauce`, throwing an `InvalidParamsError`\n            when it encounters a discrepancy. This can significantly reduce the\n            amount of code required to write JSON-RPC services.\n\n        site=default_site\n\n            Defines which site the jsonrpc method will be added to. Can be any\n            object that provides a `register(name, func)` method.\n\n    " #NEWLINE# def decorator( func ) : #NEWLINE# #INDENT# arg_names = getargspec ( func ) [ 0 ] [ 1 : ] #NEWLINE# X = { #NEWLINE# #INDENT# 'name' : name , #NEWLINE# 'arg_names' : arg_names , #NEWLINE# #UNINDENT# } #NEWLINE# if authenticated : #NEWLINE# #INDENT# if ( ( authenticated is True ) or six . callable ( authenticated ) ) : #NEWLINE# #INDENT# X [ 'arg_names' ] = ( authentication_arguments + X [ 'arg_names' ] ) #NEWLINE# X [ 'name' ] = _inject_args ( X [ 'name' ] , ( 'String' , 'String' ) ) #NEWLINE# from django.contrib.auth import authenticate as _authenticate #NEWLINE# from django.contrib.auth.models import User #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# authenticate = authenticated #NEWLINE# #UNINDENT# @ six . wraps ( func ) #NEWLINE# def _func( request , * args , **kwargs ) : #NEWLINE# #INDENT# user = getattr ( request , 'user' , None ) #NEWLINE# is_authenticated = getattr ( user , 'is_authenticated' , ( lambda : False ) ) #NEWLINE# if ( ( ( user is not None ) and six . callable ( is_authenticated ) and ( not is_authenticated ( ) ) ) or ( user is None ) ) : #NEWLINE# #INDENT# user = None #NEWLINE# try : #NEWLINE# #INDENT# creds = args [ : len ( authentication_arguments ) ] #NEWLINE# if ( len ( creds ) == 0 ) : #NEWLINE# #INDENT# raise IndexError #NEWLINE# #UNINDENT# user = _authenticate ( * creds [ 2 : ] , username = creds [ 0 ] , password = creds [ 1 ] ) #NEWLINE# if ( user is not None ) : #NEWLINE# #INDENT# args = args [ len ( authentication_arguments ) : ] #NEWLINE# #UNINDENT# #UNINDENT# except IndexError : #NEWLINE# #INDENT# auth_kwargs = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# try : #NEWLINE# #INDENT# for auth_kwarg in authentication_arguments : #NEWLINE# #INDENT# auth_kwargs [ auth_kwarg ] = kwargs [ auth_kwarg ] #NEWLINE# #UNINDENT# #UNINDENT# except KeyError : #NEWLINE# #INDENT# raise InvalidParamsError ( ( 'Authenticated methods require at least [%(arguments)s] or {%(arguments)s} arguments' % { #NEWLINE# #INDENT# 'arguments' : ', ' . join ( authentication_arguments ) , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# #UNINDENT# user = _authenticate ( ** auth_kwargs ) #NEWLINE# if ( user is not None ) : #NEWLINE# #INDENT# for auth_kwarg in authentication_arguments : #NEWLINE# #INDENT# kwargs . pop ( auth_kwarg ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( user is None ) : #NEWLINE# #INDENT# raise InvalidCredentialsError #NEWLINE# #UNINDENT# request . user = user #NEWLINE# #UNINDENT# return func ( request , * args , ** kwargs ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# _func = func #NEWLINE# #UNINDENT# @ six . wraps ( _func ) #NEWLINE# def exc_printer( * a , **kw ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# return _func ( * a , ** kw ) #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# print ( 'JSONRPC SERVICE EXCEPTION' ) #NEWLINE# import traceback #NEWLINE# traceback . print_exc ( ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# six . reraise ( * sys . exc_info ( ) ) #NEWLINE# #UNINDENT# #UNINDENT# ret_func = exc_printer #NEWLINE# ( method , arg_types , return_type ) = _parse_sig ( X [ 'name' ] , X [ 'arg_names' ] , validate ) #NEWLINE# ret_func . json_args = X [ 'arg_names' ] #NEWLINE# ret_func . json_arg_types = arg_types #NEWLINE# ret_func . json_return_type = return_type #NEWLINE# ret_func . json_method = method #NEWLINE# ret_func . json_safe = safe #NEWLINE# ret_func . json_sig = X [ 'name' ] #NEWLINE# ret_func . json_validate = validate #NEWLINE# site . register ( method , ret_func ) #NEWLINE# return ret_func #NEWLINE# #UNINDENT# return decorator