<s> """<STR_LIT>""" <EOL> from ArakoonExceptions import ArakoonInvalidArguments <EOL> import ArakoonProtocol <EOL> from functools import wraps <EOL> class SignatureValidator : <EOL> def __init__ ( self , * args ) : <EOL> self . param_types = args <EOL> self . param_native_type_mapping = { <EOL> '<STR_LIT:int>' : int , <EOL> '<STR_LIT:string>' : str , <EOL> '<STR_LIT:bool>' : bool <EOL> } <EOL> def __call__ ( self , f ) : <EOL> @ wraps ( f ) <EOL> def my_new_f ( * args , ** kwargs ) : <EOL> new_args = list ( args [ <NUM_LIT:1> : ] ) <EOL> missing_args = f . func_code . co_varnames [ len ( args ) : ] <EOL> for missing_arg in missing_args : <EOL> if ( len ( new_args ) == len ( self . param_types ) ) : <EOL> break <EOL> if ( kwargs . has_key ( missing_arg ) ) : <EOL> pos = f . func_code . co_varnames . index ( missing_arg ) <EOL> new_args . insert ( pos , kwargs [ missing_arg ] ) <EOL> del kwargs [ missing_arg ] <EOL> if len ( kwargs ) > <NUM_LIT:0> : <EOL> raise ArakoonInvalidArguments ( f . func_name , list ( kwargs . iteritems ( ) ) ) <EOL> i = <NUM_LIT:0> <EOL> error_key_values = [ ] <EOL> for ( arg , arg_type ) in zip ( new_args , self . param_types ) : <EOL> if not self . validate ( arg , arg_type ) : <EOL> error_key_values . append ( ( f . func_code . co_varnames [ i + <NUM_LIT:1> ] , new_args [ i ] ) ) <EOL> i += <NUM_LIT:1> <EOL> if len ( error_key_values ) > <NUM_LIT:0> : <EOL> raise ArakoonInvalidArguments ( f . func_name , error_key_values ) <EOL> return f ( args [ <NUM_LIT:0> ] , * new_args ) <EOL> return my_new_f <EOL> def validate ( self , arg , arg_type ) : <EOL> if self . param_native_type_mapping . has_key ( arg_type ) : <EOL> return isinstance ( arg , self . param_native_type_mapping [ arg_type ] ) <EOL> elif arg_type == '<STR_LIT>' : <EOL> return isinstance ( arg , str ) or arg is None <EOL> elif arg_type == '<STR_LIT>' : <EOL> return isinstance ( arg , ArakoonProtocol . Sequence ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % arg_type ) </s>
