<s> import copy <EOL> from importlib import import_module <EOL> from django . core . exceptions import ValidationError <EOL> from . exceptions import InvalidRuleNameError <EOL> from . exceptions import InvalidValidateDataError <EOL> from . utils import format_args_split , check_errors , error_message_generate <EOL> from . rules import WITH_PARAMETERS_VALIDATOR <EOL> class BaseValidator ( type ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( cls , name , bases , dct ) : <EOL> new_attrs = dict ( ) <EOL> error_list = dict ( ) <EOL> error_list_ext = dict ( ) <EOL> if name is not '<STR_LIT>' : <EOL> for k in dct : <EOL> if k != '<STR_LIT>' and k != '<STR_LIT:__main__>' : <EOL> pattern = dct . get ( k , None ) <EOL> if pattern and not callable ( pattern ) : <EOL> new_attrs . update ( ** { k : pattern } ) <EOL> error_list . update ( ** { k : { } } ) <EOL> setattr ( cls , '<STR_LIT>' , new_attrs ) <EOL> setattr ( cls , '<STR_LIT>' , error_list ) <EOL> setattr ( cls , '<STR_LIT>' , error_list_ext ) <EOL> super ( BaseValidator , cls ) . __init__ ( name , bases , dct ) <EOL> def __new__ ( mcs , name , bases , dct ) : <EOL> return type . __new__ ( mcs , name , bases , dct ) <EOL> class Validator ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = BaseValidator <EOL> def __init__ ( self , data , message = None ) : <EOL> if not data : <EOL> raise InvalidValidateDataError ( ) <EOL> self . data = data <EOL> self . message = message <EOL> self . validate_flag = True <EOL> self . error_list = copy . deepcopy ( self . error_list ) <EOL> self . error_list_ext = copy . deepcopy ( self . error_list_ext ) <EOL> def add_error ( self , error ) : <EOL> self . error_list_ext . update ( error ) <EOL> def get ( self , item ) : <EOL> return self . data . get ( item , None ) <EOL> def get_extra ( self , item ) : <EOL> if self . extra_data : <EOL> return self . extra_data . get ( item ) <EOL> else : <EOL> return None <EOL> def set_extra ( self , key , value ) : <EOL> if not getattr ( self , '<STR_LIT>' , None ) : <EOL> setattr ( self , '<STR_LIT>' , dict ( ) ) <EOL> update = { key : value } <EOL> self . extra_data . update ( update ) <EOL> def fails ( self ) : <EOL> validate_data = getattr ( self , '<STR_LIT>' ) <EOL> for k in validate_data : <EOL> rules = validate_data . get ( k , None ) <EOL> if not rules : <EOL> raise InvalidValidateDataError ( ) <EOL> else : <EOL> rules_list = rules . split ( '<STR_LIT:|>' ) <EOL> for rule in rules_list : <EOL> rule_origin = rule <EOL> rule = rule . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> if rule is None or rule == '<STR_LIT>' : <EOL> raise InvalidRuleNameError ( rule = rule ) <EOL> rule_validator = import_module ( '<STR_LIT>' , package = '<STR_LIT>' ) <EOL> try : <EOL> regex = getattr ( rule_validator , rule . upper ( ) ) <EOL> except AttributeError : <EOL> raise InvalidRuleNameError ( rule = rule ) <EOL> if rule . upper ( ) in WITH_PARAMETERS_VALIDATOR : <EOL> rule_args = format_args_split ( rule_origin ) <EOL> regex = regex ( rule_args ) <EOL> else : <EOL> regex = regex ( ) <EOL> try : <EOL> setattr ( regex , '<STR_LIT>' , self ) <EOL> regex ( self . data . get ( k , None ) ) <EOL> except ValidationError as e : <EOL> error_message = error_message_generate ( k , rule , self . message , e ) <EOL> error_dict = self . error_list . get ( k ) <EOL> error_dict . update ( { rule : error_message } ) <EOL> self . error_list . get ( k ) . update ( error_dict ) <EOL> self . validate_flag = False <EOL> check = getattr ( self , '<STR_LIT>' , None ) <EOL> if check and callable ( check ) and self . validate_flag : <EOL> check ( ) <EOL> return check_errors ( self . error_list , self . error_list_ext ) <EOL> def errors ( self ) : <EOL> self . error_list . update ( extra = self . error_list_ext ) <EOL> return self . error_list </s>
