<s> from __future__ import unicode_literals <EOL> import re <EOL> from wtforms . compat import string_types , text_type <EOL> __all__ = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:email>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:url>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> class ValidationError ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message = '<STR_LIT>' , * args , ** kwargs ) : <EOL> ValueError . __init__ ( self , message , * args , ** kwargs ) <EOL> class StopValidation ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message = '<STR_LIT>' , * args , ** kwargs ) : <EOL> Exception . __init__ ( self , message , * args , ** kwargs ) <EOL> class EqualTo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fieldname , message = None ) : <EOL> self . fieldname = fieldname <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> try : <EOL> other = form [ self . fieldname ] <EOL> except KeyError : <EOL> raise ValidationError ( field . gettext ( "<STR_LIT>" ) % self . fieldname ) <EOL> if field . data != other . data : <EOL> d = { <EOL> '<STR_LIT>' : hasattr ( other , '<STR_LIT:label>' ) and other . label . text or self . fieldname , <EOL> '<STR_LIT>' : self . fieldname <EOL> } <EOL> message = self . message <EOL> if message is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> raise ValidationError ( message % d ) <EOL> class Length ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , min = - <NUM_LIT:1> , max = - <NUM_LIT:1> , message = None ) : <EOL> assert min != - <NUM_LIT:1> or max != - <NUM_LIT:1> , '<STR_LIT>' <EOL> assert max == - <NUM_LIT:1> or min <= max , '<STR_LIT>' <EOL> self . min = min <EOL> self . max = max <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> l = field . data and len ( field . data ) or <NUM_LIT:0> <EOL> if l < self . min or self . max != - <NUM_LIT:1> and l > self . max : <EOL> message = self . message <EOL> if message is None : <EOL> if self . max == - <NUM_LIT:1> : <EOL> message = field . ngettext ( '<STR_LIT>' , <EOL> '<STR_LIT>' , self . min ) <EOL> elif self . min == - <NUM_LIT:1> : <EOL> message = field . ngettext ( '<STR_LIT>' , <EOL> '<STR_LIT>' , self . max ) <EOL> else : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> raise ValidationError ( message % dict ( min = self . min , max = self . max ) ) <EOL> class NumberRange ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , min = None , max = None , message = None ) : <EOL> self . min = min <EOL> self . max = max <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> data = field . data <EOL> if data is None or ( self . min is not None and data < self . min ) or ( self . max is not None and data > self . max ) : <EOL> message = self . message <EOL> if message is None : <EOL> if self . max is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> elif self . min is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> else : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> raise ValidationError ( message % dict ( min = self . min , max = self . max ) ) <EOL> class Optional ( object ) : <EOL> """<STR_LIT>""" <EOL> field_flags = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , strip_whitespace = True ) : <EOL> if strip_whitespace : <EOL> self . string_check = lambda s : s . strip ( ) <EOL> else : <EOL> self . string_check = lambda s : s <EOL> def __call__ ( self , form , field ) : <EOL> if not field . raw_data or isinstance ( field . raw_data [ <NUM_LIT:0> ] , string_types ) and not self . string_check ( field . raw_data [ <NUM_LIT:0> ] ) : <EOL> field . errors [ : ] = [ ] <EOL> raise StopValidation ( ) <EOL> class DataRequired ( object ) : <EOL> """<STR_LIT>""" <EOL> field_flags = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , message = None ) : <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> if not field . data or isinstance ( field . data , string_types ) and not field . data . strip ( ) : <EOL> if self . message is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> else : <EOL> message = self . message <EOL> field . errors [ : ] = [ ] <EOL> raise StopValidation ( message ) <EOL> class Required ( DataRequired ) : <EOL> """<STR_LIT>""" <EOL> class InputRequired ( object ) : <EOL> """<STR_LIT>""" <EOL> field_flags = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , message = None ) : <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> if not field . raw_data or not field . raw_data [ <NUM_LIT:0> ] : <EOL> if self . message is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> else : <EOL> message = self . message <EOL> field . errors [ : ] = [ ] <EOL> raise StopValidation ( message ) <EOL> class Regexp ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , regex , flags = <NUM_LIT:0> , message = None ) : <EOL> if isinstance ( regex , string_types ) : <EOL> regex = re . compile ( regex , flags ) <EOL> self . regex = regex <EOL> self . message = message <EOL> def __call__ ( self , form , field , message = None ) : <EOL> if not self . regex . match ( field . data or '<STR_LIT>' ) : <EOL> if message is None : <EOL> if self . message is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> else : <EOL> message = self . message <EOL> raise ValidationError ( message ) <EOL> class Email ( Regexp ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message = None ) : <EOL> super ( Email , self ) . __init__ ( r'<STR_LIT>' , re . IGNORECASE , message ) <EOL> def __call__ ( self , form , field ) : <EOL> message = self . message <EOL> if message is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> super ( Email , self ) . __call__ ( form , field , message ) <EOL> class IPAddress ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ipv4 = True , ipv6 = False , message = None ) : <EOL> if not ipv4 and not ipv6 : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . ipv4 = ipv4 <EOL> self . ipv6 = ipv6 <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> value = field . data <EOL> valid = False <EOL> if value : <EOL> valid = ( self . ipv4 and self . check_ipv4 ( value ) ) or ( self . ipv6 and self . check_ipv6 ( value ) ) <EOL> if not valid : <EOL> message = self . message <EOL> if message is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> raise ValidationError ( message ) <EOL> def check_ipv4 ( self , value ) : <EOL> parts = value . split ( '<STR_LIT:.>' ) <EOL> if len ( parts ) == <NUM_LIT:4> and all ( x . isdigit ( ) for x in parts ) : <EOL> numbers = list ( int ( x ) for x in parts ) <EOL> return all ( num >= <NUM_LIT:0> and num < <NUM_LIT> for num in numbers ) <EOL> return False <EOL> def check_ipv6 ( self , value ) : <EOL> parts = value . split ( '<STR_LIT::>' ) <EOL> if len ( parts ) > <NUM_LIT:8> : <EOL> return False <EOL> num_blank = <NUM_LIT:0> <EOL> for part in parts : <EOL> if not part : <EOL> num_blank += <NUM_LIT:1> <EOL> else : <EOL> try : <EOL> value = int ( part , <NUM_LIT:16> ) <EOL> except ValueError : <EOL> return False <EOL> else : <EOL> if value < <NUM_LIT:0> or value >= <NUM_LIT> : <EOL> return False <EOL> if num_blank < <NUM_LIT:2> : <EOL> return True <EOL> elif num_blank == <NUM_LIT:2> and not parts [ <NUM_LIT:0> ] and not parts [ <NUM_LIT:1> ] : <EOL> return True <EOL> return False <EOL> class MacAddress ( Regexp ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message = None ) : <EOL> pattern = r'<STR_LIT>' <EOL> super ( MacAddress , self ) . __init__ ( pattern , message = message ) <EOL> def __call__ ( self , form , field ) : <EOL> message = self . message <EOL> if message is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> super ( MacAddress , self ) . __call__ ( form , field , message ) <EOL> class URL ( Regexp ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , require_tld = True , message = None ) : <EOL> tld_part = ( require_tld and r'<STR_LIT>' or '<STR_LIT>' ) <EOL> regex = r'<STR_LIT>' % tld_part <EOL> super ( URL , self ) . __init__ ( regex , re . IGNORECASE , message ) <EOL> def __call__ ( self , form , field ) : <EOL> message = self . message <EOL> if message is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> super ( URL , self ) . __call__ ( form , field , message ) <EOL> class UUID ( Regexp ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message = None ) : <EOL> pattern = r'<STR_LIT>' <EOL> super ( UUID , self ) . __init__ ( pattern , message = message ) <EOL> def __call__ ( self , form , field ) : <EOL> message = self . message <EOL> if message is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> super ( UUID , self ) . __call__ ( form , field , message ) <EOL> class AnyOf ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , values , message = None , values_formatter = None ) : <EOL> self . values = values <EOL> self . message = message <EOL> if values_formatter is None : <EOL> values_formatter = self . default_values_formatter <EOL> self . values_formatter = values_formatter <EOL> def __call__ ( self , form , field ) : <EOL> if field . data not in self . values : <EOL> message = self . message <EOL> if message is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> raise ValidationError ( message % dict ( values = self . values_formatter ( self . values ) ) ) <EOL> @ staticmethod <EOL> def default_values_formatter ( values ) : <EOL> return '<STR_LIT:U+002CU+0020>' . join ( text_type ( x ) for x in values ) <EOL> class NoneOf ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , values , message = None , values_formatter = None ) : <EOL> self . values = values <EOL> self . message = message <EOL> if values_formatter is None : <EOL> values_formatter = lambda v : '<STR_LIT:U+002CU+0020>' . join ( text_type ( x ) for x in v ) <EOL> self . values_formatter = values_formatter <EOL> def __call__ ( self , form , field ) : <EOL> if field . data in self . values : <EOL> message = self . message <EOL> if message is None : <EOL> message = field . gettext ( '<STR_LIT>' ) <EOL> raise ValidationError ( message % dict ( values = self . values_formatter ( self . values ) ) ) <EOL> email = Email <EOL> equal_to = EqualTo <EOL> ip_address = IPAddress <EOL> mac_address = MacAddress <EOL> length = Length <EOL> number_range = NumberRange <EOL> optional = Optional <EOL> required = Required <EOL> input_required = InputRequired <EOL> data_required = DataRequired <EOL> regexp = Regexp <EOL> url = URL <EOL> any_of = AnyOf <EOL> none_of = NoneOf </s>
