#NEWLINE# def add( self , name , auth_required = True , list_command = True , **validators ) : #NEWLINE# #INDENT# 'Create a decorator that registers a handler and validation rules.\n\n        Additional keyword arguments are treated as converters/validators to\n        apply to tokens converting them to proper Python types.\n\n        Requirements for valid handlers:\n\n        - must accept a context argument as the first arg.\n        - may not use variable keyword arguments, ``**kwargs``.\n        - may use variable arguments ``*args`` *or* a mix of required and\n          optional arguments.\n\n        Decorator returns the unwrapped function so that tests etc can use the\n        functions with values with correct python types instead of strings.\n\n        :param string name: Name of the command being registered.\n        :param bool auth_required: If authorization is required.\n        :param bool list_command: If command should be listed in reflection.\n        ' #NEWLINE# def wrapper( func ) : #NEWLINE# #INDENT# if ( name in self . handlers ) : #NEWLINE# #INDENT# raise ValueError ( ( '%s already registered' % name ) ) #NEWLINE# #UNINDENT# ( args , varargs , keywords , defaults ) = inspect . getargspec ( func ) #NEWLINE# defaults = dict ( zip ( args [ ( - len ( ( defaults or [ ] ) ) ) : ] , ( defaults or [ ] ) ) ) #NEWLINE# if ( ( not args ) and ( not varargs ) ) : #NEWLINE# #INDENT# raise TypeError ( 'Handler must accept at least one argument.' ) #NEWLINE# #UNINDENT# if ( ( len ( args ) > 1 ) and varargs ) : #NEWLINE# #INDENT# raise TypeError ( '*args may not be combined with regular arguments' ) #NEWLINE# #UNINDENT# if ( not set ( validators . keys ( ) ) . issubset ( args ) ) : #NEWLINE# #INDENT# raise TypeError ( 'Validator for non-existent arg passed' ) #NEWLINE# #UNINDENT# if keywords : #NEWLINE# #INDENT# raise TypeError ( '**kwargs are not permitted' ) #NEWLINE# #UNINDENT# def validate( * args , **kwargs ) : #NEWLINE# #INDENT# if varargs : #NEWLINE# #INDENT# return func ( * args , ** kwargs ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# callargs = inspect . getcallargs ( func , * args , ** kwargs ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# raise exceptions . MpdArgError ( ( 'wrong number of arguments for "%s"' % name ) ) #NEWLINE# #UNINDENT# for ( key , value ) in callargs . items ( ) : #NEWLINE# #INDENT# default = defaults . get ( key , object ( ) ) #NEWLINE# if ( ( key in validators ) and ( value != default ) ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# callargs [ key ] = validators [ key ] ( value ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# raise exceptions . MpdArgError ( 'incorrect arguments' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return func ( ** callargs ) #NEWLINE# #UNINDENT# validate . auth_required = auth_required #NEWLINE# validate . list_command = list_command #NEWLINE# self . handlers [ name ] = validate #NEWLINE# return func #NEWLINE# #UNINDENT# return wrapper