<s> """<STR_LIT>""" <EOL> import datetime <EOL> import logging <EOL> import re <EOL> import time <EOL> import six <EOL> from staticconf . errors import ValidationError <EOL> def validate_string ( value ) : <EOL> return None if value is None else six . text_type ( value ) <EOL> def validate_bool ( value ) : <EOL> return None if value is None else bool ( value ) <EOL> def validate_numeric ( type_func , value ) : <EOL> try : <EOL> return type_func ( value ) <EOL> except ValueError : <EOL> raise ValidationError ( "<STR_LIT>" % ( type_func . __name__ , value ) ) <EOL> def validate_int ( value ) : <EOL> return validate_numeric ( int , value ) <EOL> def validate_float ( value ) : <EOL> return validate_numeric ( float , value ) <EOL> date_formats = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> def validate_datetime ( value ) : <EOL> if isinstance ( value , datetime . datetime ) : <EOL> return value <EOL> for format_ in date_formats : <EOL> try : <EOL> return datetime . datetime . strptime ( value , format_ ) <EOL> except ValueError : <EOL> pass <EOL> raise ValidationError ( "<STR_LIT>" % value ) <EOL> def validate_date ( value ) : <EOL> if isinstance ( value , datetime . date ) : <EOL> return value <EOL> return validate_datetime ( value ) . date ( ) <EOL> time_formats = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> def validate_time ( value ) : <EOL> if isinstance ( value , datetime . time ) : <EOL> return value <EOL> for format_ in time_formats : <EOL> try : <EOL> return datetime . time ( * time . strptime ( value , format_ ) [ <NUM_LIT:3> : <NUM_LIT:6> ] ) <EOL> except ValueError : <EOL> pass <EOL> raise ValidationError ( "<STR_LIT>" % value ) <EOL> def _validate_iterable ( iterable_type , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , six . string_types ) : <EOL> msg = "<STR_LIT>" <EOL> raise ValidationError ( msg % ( tuple ( value ) , value ) ) <EOL> try : <EOL> return iterable_type ( value ) <EOL> except TypeError : <EOL> raise ValidationError ( "<STR_LIT>" % ( value ) ) <EOL> def validate_list ( value ) : <EOL> return _validate_iterable ( list , value ) <EOL> def validate_set ( value ) : <EOL> return _validate_iterable ( set , value ) <EOL> def validate_tuple ( value ) : <EOL> return _validate_iterable ( tuple , value ) <EOL> def validate_regex ( value ) : <EOL> try : <EOL> return re . compile ( value ) <EOL> except ( re . error , TypeError ) as e : <EOL> raise ValidationError ( "<STR_LIT>" % ( e , value ) ) <EOL> def build_list_type_validator ( item_validator ) : <EOL> """<STR_LIT>""" <EOL> def validate_list_of_type ( value ) : <EOL> return [ item_validator ( item ) for item in validate_list ( value ) ] <EOL> return validate_list_of_type <EOL> def build_map_type_validator ( item_validator ) : <EOL> """<STR_LIT>""" <EOL> def validate_mapping ( value ) : <EOL> return dict ( item_validator ( item ) for item in validate_list ( value ) ) <EOL> return validate_mapping <EOL> def validate_log_level ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return getattr ( logging , value ) <EOL> except AttributeError : <EOL> raise ValidationError ( "<STR_LIT>" % value ) <EOL> def validate_any ( value ) : <EOL> return value <EOL> validators = { <EOL> '<STR_LIT>' : validate_any , <EOL> '<STR_LIT:bool>' : validate_bool , <EOL> '<STR_LIT:date>' : validate_date , <EOL> '<STR_LIT>' : validate_datetime , <EOL> '<STR_LIT:float>' : validate_float , <EOL> '<STR_LIT:int>' : validate_int , <EOL> '<STR_LIT:list>' : validate_list , <EOL> '<STR_LIT>' : validate_set , <EOL> '<STR_LIT:string>' : validate_string , <EOL> '<STR_LIT:time>' : validate_time , <EOL> '<STR_LIT>' : validate_tuple , <EOL> '<STR_LIT>' : validate_regex , <EOL> '<STR_LIT>' : validate_log_level , <EOL> } <EOL> def get_validators ( ) : <EOL> """<STR_LIT>""" <EOL> return six . iteritems ( validators ) </s>
