<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import cgi <EOL> import locale <EOL> import re <EOL> import warnings <EOL> from encodings import idna <EOL> import six <EOL> from six . moves import map <EOL> from six . moves import range <EOL> try : <EOL> import dns . resolver <EOL> import dns . exception <EOL> except ( IOError , ImportError ) : <EOL> have_dns = False <EOL> else : <EOL> have_dns = True <EOL> http_client = None <EOL> random = None <EOL> sha1 = None <EOL> socket = None <EOL> urlparse = None <EOL> from . api import ( FancyValidator , Identity , Invalid , NoDefault , Validator , <EOL> deprecation_warning , is_empty ) <EOL> assert Identity and Invalid and NoDefault <EOL> _ = lambda s : s <EOL> datetime_module = None <EOL> mxDateTime_module = None <EOL> def import_datetime ( module_type ) : <EOL> global datetime_module , mxDateTime_module <EOL> module_type = module_type . lower ( ) if module_type else '<STR_LIT>' <EOL> if module_type == '<STR_LIT>' : <EOL> if datetime_module is None : <EOL> import datetime as datetime_module <EOL> return datetime_module <EOL> elif module_type == '<STR_LIT>' : <EOL> if mxDateTime_module is None : <EOL> from mx import DateTime as mxDateTime_module <EOL> return mxDateTime_module <EOL> else : <EOL> raise ImportError ( '<STR_LIT>' % module_type ) <EOL> def datetime_now ( module ) : <EOL> if module . __name__ == '<STR_LIT>' : <EOL> return module . datetime . now ( ) <EOL> else : <EOL> return module . now ( ) <EOL> def datetime_makedate ( module , year , month , day ) : <EOL> if module . __name__ == '<STR_LIT>' : <EOL> return module . date ( year , month , day ) <EOL> else : <EOL> try : <EOL> return module . DateTime ( year , month , day ) <EOL> except module . RangeError as e : <EOL> raise ValueError ( str ( e ) ) <EOL> def datetime_time ( module ) : <EOL> if module . __name__ == '<STR_LIT>' : <EOL> return module . time <EOL> else : <EOL> return module . Time <EOL> def datetime_isotime ( module ) : <EOL> if module . __name__ == '<STR_LIT>' : <EOL> return module . time . isoformat <EOL> else : <EOL> return module . ISO . Time <EOL> class ConfirmType ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> accept_iterator = True <EOL> subclass = None <EOL> type = None <EOL> messages = dict ( <EOL> subclass = _ ( '<STR_LIT>' ) , <EOL> inSubclass = _ ( '<STR_LIT>' ) , <EOL> inType = _ ( '<STR_LIT>' ) , <EOL> type = _ ( '<STR_LIT>' ) ) <EOL> def __init__ ( self , * args , ** kw ) : <EOL> FancyValidator . __init__ ( self , * args , ** kw ) <EOL> if self . subclass : <EOL> if isinstance ( self . subclass , list ) : <EOL> self . subclass = tuple ( self . subclass ) <EOL> elif not isinstance ( self . subclass , tuple ) : <EOL> self . subclass = ( self . subclass , ) <EOL> self . _validate_python = self . confirm_subclass <EOL> if self . type : <EOL> if isinstance ( self . type , list ) : <EOL> self . type = tuple ( self . type ) <EOL> elif not isinstance ( self . type , tuple ) : <EOL> self . type = ( self . type , ) <EOL> self . _validate_python = self . confirm_type <EOL> def confirm_subclass ( self , value , state ) : <EOL> if not isinstance ( value , self . subclass ) : <EOL> if len ( self . subclass ) == <NUM_LIT:1> : <EOL> msg = self . message ( '<STR_LIT>' , state , object = value , <EOL> subclass = self . subclass [ <NUM_LIT:0> ] ) <EOL> else : <EOL> subclass_list = '<STR_LIT:U+002CU+0020>' . join ( map ( str , self . subclass ) ) <EOL> msg = self . message ( '<STR_LIT>' , state , object = value , <EOL> subclassList = subclass_list ) <EOL> raise Invalid ( msg , value , state ) <EOL> def confirm_type ( self , value , state ) : <EOL> for t in self . type : <EOL> if type ( value ) is t : <EOL> break <EOL> else : <EOL> if len ( self . type ) == <NUM_LIT:1> : <EOL> msg = self . message ( '<STR_LIT:type>' , state , object = value , <EOL> type = self . type [ <NUM_LIT:0> ] ) <EOL> else : <EOL> msg = self . message ( '<STR_LIT>' , state , object = value , <EOL> typeList = '<STR_LIT:U+002CU+0020>' . join ( map ( str , self . type ) ) ) <EOL> raise Invalid ( msg , value , state ) <EOL> return value <EOL> def is_empty ( self , value ) : <EOL> return False <EOL> class Wrapper ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> func_convert_to_python = None <EOL> func_convert_from_python = None <EOL> func_validate_python = None <EOL> func_validate_other = None <EOL> _deprecated_methods = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def __init__ ( self , * args , ** kw ) : <EOL> if '<STR_LIT>' in kw and '<STR_LIT>' not in kw : <EOL> kw [ '<STR_LIT>' ] = kw . pop ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in kw and '<STR_LIT>' not in kw : <EOL> kw [ '<STR_LIT>' ] = kw . pop ( '<STR_LIT>' ) <EOL> for n in ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if n in kw : <EOL> kw [ '<STR_LIT>' % n ] = kw . pop ( n ) <EOL> FancyValidator . __init__ ( self , * args , ** kw ) <EOL> self . _convert_to_python = self . wrap ( self . func_convert_to_python ) <EOL> self . _convert_from_python = self . wrap ( self . func_convert_from_python ) <EOL> self . _validate_python = self . wrap ( self . func_validate_python ) <EOL> self . _validate_other = self . wrap ( self . func_validate_other ) <EOL> def wrap ( self , func ) : <EOL> if not func : <EOL> return None <EOL> def result ( value , state , func = func ) : <EOL> try : <EOL> return func ( value ) <EOL> except Exception as e : <EOL> raise Invalid ( str ( e ) , value , state ) <EOL> return result <EOL> class Constant ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> __unpackargs__ = ( '<STR_LIT:value>' , ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> return self . value <EOL> _convert_from_python = _convert_to_python <EOL> class MaxLength ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> __unpackargs__ = ( '<STR_LIT>' , ) <EOL> messages = dict ( <EOL> tooLong = _ ( '<STR_LIT>' ) , <EOL> invalid = _ ( '<STR_LIT>' ) ) <EOL> def _validate_python ( self , value , state ) : <EOL> try : <EOL> if value and len ( value ) > self . maxLength : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> maxLength = self . maxLength ) , value , state ) <EOL> else : <EOL> return None <EOL> except TypeError : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> class MinLength ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> __unpackargs__ = ( '<STR_LIT>' , ) <EOL> messages = dict ( <EOL> tooShort = _ ( '<STR_LIT>' ) , <EOL> invalid = _ ( '<STR_LIT>' ) ) <EOL> def _validate_python ( self , value , state ) : <EOL> try : <EOL> if len ( value ) < self . minLength : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> minLength = self . minLength ) , value , state ) <EOL> except TypeError : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> class NotEmpty ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> not_empty = True <EOL> messages = dict ( <EOL> empty = _ ( '<STR_LIT>' ) ) <EOL> def _validate_python ( self , value , state ) : <EOL> if value == <NUM_LIT:0> : <EOL> return value <EOL> if not value : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> class Empty ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> messages = dict ( <EOL> notEmpty = _ ( '<STR_LIT>' ) ) <EOL> def _validate_python ( self , value , state ) : <EOL> if value or value == <NUM_LIT:0> : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> class Regex ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> regexOps = ( ) <EOL> strip = False <EOL> regex = None <EOL> __unpackargs__ = ( '<STR_LIT>' , ) <EOL> messages = dict ( <EOL> invalid = _ ( '<STR_LIT>' ) ) <EOL> def __init__ ( self , * args , ** kw ) : <EOL> FancyValidator . __init__ ( self , * args , ** kw ) <EOL> if isinstance ( self . regex , six . string_types ) : <EOL> ops = <NUM_LIT:0> <EOL> assert not isinstance ( self . regexOps , six . string_types ) , ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for op in self . regexOps : <EOL> if isinstance ( op , six . string_types ) : <EOL> ops |= getattr ( re , op ) <EOL> else : <EOL> ops |= op <EOL> self . regex = re . compile ( self . regex , ops ) <EOL> def _validate_python ( self , value , state ) : <EOL> self . assert_string ( value , state ) <EOL> if self . strip and isinstance ( value , six . string_types ) : <EOL> value = value . strip ( ) <EOL> if not self . regex . search ( value ) : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> if self . strip and isinstance ( value , six . string_types ) : <EOL> return value . strip ( ) <EOL> return value <EOL> class PlainText ( Regex ) : <EOL> """<STR_LIT>""" <EOL> regex = r"<STR_LIT>" <EOL> messages = dict ( <EOL> invalid = _ ( '<STR_LIT>' ) ) <EOL> class OneOf ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> list = None <EOL> testValueList = False <EOL> hideList = False <EOL> __unpackargs__ = ( '<STR_LIT:list>' , ) <EOL> messages = dict ( <EOL> invalid = _ ( '<STR_LIT>' ) , <EOL> notIn = _ ( '<STR_LIT>' ) ) <EOL> def _validate_python ( self , value , state ) : <EOL> if self . testValueList and isinstance ( value , ( list , tuple ) ) : <EOL> for v in value : <EOL> self . _validate_python ( v , state ) <EOL> else : <EOL> if not value in self . list : <EOL> if self . hideList : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> else : <EOL> try : <EOL> items = '<STR_LIT>' . join ( map ( str , self . list ) ) <EOL> except UnicodeError : <EOL> items = '<STR_LIT>' . join ( map ( six . text_type , self . list ) ) <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> items = items , value = value ) , value , state ) <EOL> @ property <EOL> def accept_iterator ( self ) : <EOL> return self . testValueList <EOL> class DictConverter ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> messages = dict ( <EOL> keyNotFound = _ ( '<STR_LIT>' ) , <EOL> chooseKey = _ ( '<STR_LIT>' ) , <EOL> valueNotFound = _ ( '<STR_LIT>' ) , <EOL> chooseValue = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> dict = None <EOL> hideDict = False <EOL> __unpackargs__ = ( '<STR_LIT>' , ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> try : <EOL> return self . dict [ value ] <EOL> except KeyError : <EOL> if self . hideDict : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> else : <EOL> items = sorted ( self . dict ) <EOL> items = '<STR_LIT>' . join ( map ( repr , items ) ) <EOL> raise Invalid ( self . message ( '<STR_LIT>' , <EOL> state , items = items ) , value , state ) <EOL> def _convert_from_python ( self , value , state ) : <EOL> for k , v in six . iteritems ( self . dict ) : <EOL> if value == v : <EOL> return k <EOL> if self . hideDict : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> else : <EOL> items = '<STR_LIT>' . join ( map ( repr , six . itervalues ( self . dict ) ) ) <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> value = repr ( value ) , items = items ) , value , state ) <EOL> class IndexListConverter ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> list = None <EOL> __unpackargs__ = ( '<STR_LIT:list>' , ) <EOL> messages = dict ( <EOL> integer = _ ( '<STR_LIT>' ) , <EOL> outOfRange = _ ( '<STR_LIT>' ) , <EOL> notFound = _ ( '<STR_LIT>' ) ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> try : <EOL> value = int ( value ) <EOL> except ( ValueError , TypeError ) : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> try : <EOL> return self . list [ value ] <EOL> except IndexError : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> def _convert_from_python ( self , value , state ) : <EOL> for i , v in enumerate ( self . list ) : <EOL> if v == value : <EOL> return i <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , value = repr ( value ) ) , value , state ) <EOL> class DateValidator ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> earliest_date = None <EOL> latest_date = None <EOL> after_now = False <EOL> today_or_after = False <EOL> datetime_module = None <EOL> messages = dict ( <EOL> after = _ ( '<STR_LIT>' ) , <EOL> before = _ ( '<STR_LIT>' ) , <EOL> date_format = _ ( '<STR_LIT>' ) , <EOL> future = _ ( '<STR_LIT>' ) ) <EOL> def _validate_python ( self , value , state ) : <EOL> date_format = self . message ( '<STR_LIT>' , state ) <EOL> if ( str is not six . text_type <EOL> and isinstance ( date_format , six . text_type ) ) : <EOL> encoding = locale . getlocale ( locale . LC_TIME ) [ <NUM_LIT:1> ] or '<STR_LIT:utf-8>' <EOL> date_format = date_format . encode ( encoding ) <EOL> else : <EOL> encoding = None <EOL> if self . earliest_date : <EOL> if callable ( self . earliest_date ) : <EOL> earliest_date = self . earliest_date ( ) <EOL> else : <EOL> earliest_date = self . earliest_date <EOL> if value < earliest_date : <EOL> date_formatted = earliest_date . strftime ( date_format ) <EOL> if encoding : <EOL> date_formatted = date_formatted . decode ( encoding ) <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , date = date_formatted ) , <EOL> value , state ) <EOL> if self . latest_date : <EOL> if callable ( self . latest_date ) : <EOL> latest_date = self . latest_date ( ) <EOL> else : <EOL> latest_date = self . latest_date <EOL> if value > latest_date : <EOL> date_formatted = latest_date . strftime ( date_format ) <EOL> if encoding : <EOL> date_formatted = date_formatted . decode ( encoding ) <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , date = date_formatted ) , <EOL> value , state ) <EOL> if self . after_now : <EOL> dt_mod = import_datetime ( self . datetime_module ) <EOL> now = datetime_now ( dt_mod ) <EOL> if value < now : <EOL> date_formatted = now . strftime ( date_format ) <EOL> if encoding : <EOL> date_formatted = date_formatted . decode ( encoding ) <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , date = date_formatted ) , <EOL> value , state ) <EOL> if self . today_or_after : <EOL> dt_mod = import_datetime ( self . datetime_module ) <EOL> now = datetime_now ( dt_mod ) <EOL> today = datetime_makedate ( dt_mod , <EOL> now . year , now . month , now . day ) <EOL> value_as_date = datetime_makedate ( <EOL> dt_mod , value . year , value . month , value . day ) <EOL> if value_as_date < today : <EOL> date_formatted = now . strftime ( date_format ) <EOL> if encoding : <EOL> date_formatted = date_formatted . decode ( encoding ) <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , date = date_formatted ) , <EOL> value , state ) <EOL> class Bool ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> if_missing = False <EOL> def _convert_to_python ( self , value , state ) : <EOL> return bool ( value ) <EOL> _convert_from_python = _convert_to_python <EOL> def empty_value ( self , value ) : <EOL> return False <EOL> class RangeValidator ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> messages = dict ( <EOL> tooLow = _ ( '<STR_LIT>' ) , <EOL> tooHigh = _ ( '<STR_LIT>' ) ) <EOL> min = None <EOL> max = None <EOL> def _validate_python ( self , value , state ) : <EOL> if self . min is not None : <EOL> if value < self . min : <EOL> msg = self . message ( '<STR_LIT>' , state , min = self . min ) <EOL> raise Invalid ( msg , value , state ) <EOL> if self . max is not None : <EOL> if value > self . max : <EOL> msg = self . message ( '<STR_LIT>' , state , max = self . max ) <EOL> raise Invalid ( msg , value , state ) <EOL> class Int ( RangeValidator ) : <EOL> """<STR_LIT>""" <EOL> messages = dict ( <EOL> integer = _ ( '<STR_LIT>' ) ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> try : <EOL> return int ( value ) <EOL> except ( ValueError , TypeError ) : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> _convert_from_python = _convert_to_python <EOL> class Number ( RangeValidator ) : <EOL> """<STR_LIT>""" <EOL> messages = dict ( <EOL> number = _ ( '<STR_LIT>' ) ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> try : <EOL> value = float ( value ) <EOL> try : <EOL> int_value = int ( value ) <EOL> except OverflowError : <EOL> int_value = None <EOL> if value == int_value : <EOL> return int_value <EOL> return value <EOL> except ( ValueError , TypeError ) : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> class ByteString ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> min = None <EOL> max = None <EOL> not_empty = None <EOL> encoding = None <EOL> list_joiner = '<STR_LIT:U+002CU+0020>' <EOL> messages = dict ( <EOL> tooLong = _ ( '<STR_LIT>' ) , <EOL> tooShort = _ ( '<STR_LIT>' ) ) <EOL> def __initargs__ ( self , new_attrs ) : <EOL> if self . not_empty is None and self . min : <EOL> self . not_empty = True <EOL> def _convert_to_python ( self , value , state ) : <EOL> if value is None : <EOL> value = '<STR_LIT>' <EOL> elif not isinstance ( value , six . string_types ) : <EOL> try : <EOL> value = bytes ( value ) <EOL> except UnicodeEncodeError : <EOL> value = six . text_type ( value ) <EOL> if self . encoding is not None and isinstance ( value , six . text_type ) : <EOL> value = value . encode ( self . encoding ) <EOL> return value <EOL> def _convert_from_python ( self , value , state ) : <EOL> if value is None : <EOL> value = '<STR_LIT>' <EOL> elif not isinstance ( value , six . string_types ) : <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> value = self . list_joiner . join ( <EOL> self . _convert_from_python ( v , state ) for v in value ) <EOL> try : <EOL> value = str ( value ) <EOL> except UnicodeEncodeError : <EOL> value = six . text_type ( value ) <EOL> if self . encoding is not None and isinstance ( value , six . text_type ) : <EOL> value = value . encode ( self . encoding ) <EOL> if self . strip : <EOL> value = value . strip ( ) <EOL> return value <EOL> def _validate_other ( self , value , state ) : <EOL> if self . max is None and self . min is None : <EOL> return <EOL> if value is None : <EOL> value = '<STR_LIT>' <EOL> elif not isinstance ( value , six . string_types ) : <EOL> try : <EOL> value = str ( value ) <EOL> except UnicodeEncodeError : <EOL> value = six . text_type ( value ) <EOL> if self . max is not None and len ( value ) > self . max : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , max = self . max ) , value , state ) <EOL> if self . min is not None and len ( value ) < self . min : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , min = self . min ) , value , state ) <EOL> def empty_value ( self , value ) : <EOL> return '<STR_LIT>' <EOL> class UnicodeString ( ByteString ) : <EOL> """<STR_LIT>""" <EOL> encoding = '<STR_LIT:utf-8>' <EOL> inputEncoding = NoDefault <EOL> outputEncoding = NoDefault <EOL> messages = dict ( <EOL> badEncoding = _ ( '<STR_LIT>' ) ) <EOL> def __init__ ( self , ** kw ) : <EOL> ByteString . __init__ ( self , ** kw ) <EOL> if self . inputEncoding is NoDefault : <EOL> self . inputEncoding = self . encoding <EOL> if self . outputEncoding is NoDefault : <EOL> self . outputEncoding = self . encoding <EOL> def _convert_to_python ( self , value , state ) : <EOL> if not value : <EOL> return u'<STR_LIT>' <EOL> if isinstance ( value , six . text_type ) : <EOL> return value <EOL> if not isinstance ( value , six . text_type ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = six . text_type ( value ) <EOL> return value <EOL> if not ( six . text_type is str <EOL> and isinstance ( value , bytes ) and self . inputEncoding ) : <EOL> value = str ( value ) <EOL> if self . inputEncoding and not isinstance ( value , six . text_type ) : <EOL> try : <EOL> value = six . text_type ( value , self . inputEncoding ) <EOL> except UnicodeDecodeError : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> except TypeError : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> type = type ( value ) , value = value ) , value , state ) <EOL> return value <EOL> def _convert_from_python ( self , value , state ) : <EOL> if not isinstance ( value , six . text_type ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = six . text_type ( value ) <EOL> else : <EOL> value = str ( value ) <EOL> if self . outputEncoding and isinstance ( value , six . text_type ) : <EOL> value = value . encode ( self . outputEncoding ) <EOL> return value <EOL> def empty_value ( self , value ) : <EOL> return u'<STR_LIT>' <EOL> String = UnicodeString if str is six . text_type else ByteString <EOL> class Set ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> use_set = False <EOL> if_missing = ( ) <EOL> accept_iterator = True <EOL> def _convert_to_python ( self , value , state ) : <EOL> if self . use_set : <EOL> if isinstance ( value , set ) : <EOL> return value <EOL> elif isinstance ( value , ( list , tuple ) ) : <EOL> return set ( value ) <EOL> elif value is None : <EOL> return set ( ) <EOL> else : <EOL> return set ( [ value ] ) <EOL> else : <EOL> if isinstance ( value , list ) : <EOL> return value <EOL> elif isinstance ( value , set ) : <EOL> return list ( value ) <EOL> elif isinstance ( value , tuple ) : <EOL> return list ( value ) <EOL> elif value is None : <EOL> return [ ] <EOL> else : <EOL> return [ value ] <EOL> def empty_value ( self , value ) : <EOL> if self . use_set : <EOL> return set ( ) <EOL> else : <EOL> return [ ] <EOL> class Email ( FancyValidator ) : <EOL> r"""<STR_LIT>""" <EOL> resolve_domain = False <EOL> resolve_timeout = <NUM_LIT:10> <EOL> usernameRE = re . compile ( r"<STR_LIT>" ) <EOL> domainRE = re . compile ( r'''<STR_LIT>''' , re . I | re . VERBOSE ) <EOL> messages = dict ( <EOL> empty = _ ( '<STR_LIT>' ) , <EOL> noAt = _ ( '<STR_LIT>' ) , <EOL> badUsername = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> socketError = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> badDomain = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> domainDoesNotExist = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> def __init__ ( self , * args , ** kw ) : <EOL> FancyValidator . __init__ ( self , * args , ** kw ) <EOL> if self . resolve_domain : <EOL> if not have_dns : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> def _validate_python ( self , value , state ) : <EOL> if not value : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> value = value . strip ( ) <EOL> splitted = value . split ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> try : <EOL> username , domain = splitted <EOL> except ValueError : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> if not self . usernameRE . search ( username ) : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , username = username ) , <EOL> value , state ) <EOL> try : <EOL> idna_domain = [ idna . ToASCII ( p ) for p in domain . split ( '<STR_LIT:.>' ) ] <EOL> if six . text_type is str : <EOL> idna_domain = [ p . decode ( '<STR_LIT:ascii>' ) for p in idna_domain ] <EOL> idna_domain = '<STR_LIT:.>' . join ( idna_domain ) <EOL> except UnicodeError : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , domain = domain ) , <EOL> value , state ) <EOL> if not self . domainRE . search ( idna_domain ) : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , domain = domain ) , <EOL> value , state ) <EOL> if self . resolve_domain : <EOL> assert have_dns , "<STR_LIT>" <EOL> global socket <EOL> if socket is None : <EOL> import socket <EOL> try : <EOL> try : <EOL> dns . resolver . query ( domain , '<STR_LIT>' ) <EOL> except ( dns . resolver . NXDOMAIN , dns . resolver . NoAnswer ) : <EOL> try : <EOL> dns . resolver . query ( domain , '<STR_LIT:A>' ) <EOL> except ( dns . resolver . NXDOMAIN , dns . resolver . NoAnswer ) : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , <EOL> state , domain = domain ) , value , state ) <EOL> except ( socket . error , dns . exception . DNSException ) as e : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , error = e ) , value , state ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> return value . strip ( ) <EOL> class URL ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> add_http = True <EOL> allow_idna = True <EOL> check_exists = False <EOL> require_tld = True <EOL> url_re = re . compile ( r'''<STR_LIT>''' , re . I | re . VERBOSE ) <EOL> scheme_re = re . compile ( r'<STR_LIT>' ) <EOL> messages = dict ( <EOL> noScheme = _ ( '<STR_LIT>' ) , <EOL> badURL = _ ( '<STR_LIT>' ) , <EOL> httpError = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> socketError = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> notFound = _ ( '<STR_LIT>' ) , <EOL> status = _ ( '<STR_LIT>' ) , <EOL> noTLD = _ ( '<STR_LIT>' ) ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> value = value . strip ( ) <EOL> if self . add_http : <EOL> if not self . scheme_re . search ( value ) : <EOL> value = '<STR_LIT>' + value <EOL> if self . allow_idna : <EOL> value = self . _encode_idna ( value ) <EOL> match = self . scheme_re . search ( value ) <EOL> if not match : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> value = match . group ( <NUM_LIT:0> ) . lower ( ) + value [ len ( match . group ( <NUM_LIT:0> ) ) : ] <EOL> match = self . url_re . search ( value ) <EOL> if not match : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> if self . require_tld and not match . group ( '<STR_LIT>' ) : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , domain = match . group ( '<STR_LIT>' ) ) , <EOL> value , state ) <EOL> if self . check_exists and value . startswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> self . _check_url_exists ( value , state ) <EOL> return value <EOL> def _encode_idna ( self , url ) : <EOL> global urlparse <EOL> if urlparse is None : <EOL> from six . moves . urllib import parse as urlparse <EOL> try : <EOL> scheme , netloc , path , params , query , fragment = urlparse . urlparse ( <EOL> url ) <EOL> except ValueError : <EOL> return url <EOL> try : <EOL> netloc = netloc . encode ( '<STR_LIT>' ) <EOL> if six . text_type is str : <EOL> netloc = netloc . decode ( '<STR_LIT:ascii>' ) <EOL> return str ( urlparse . urlunparse ( ( scheme , netloc , <EOL> path , params , query , fragment ) ) ) <EOL> except UnicodeError : <EOL> return url <EOL> def _check_url_exists ( self , url , state ) : <EOL> global http_client , urlparse , socket <EOL> if http_client is None : <EOL> from six . moves import http_client <EOL> if urlparse is None : <EOL> from six . moves . urllib import parse as urlparse <EOL> if socket is None : <EOL> import socket <EOL> scheme , netloc , path , params , query , fragment = urlparse . urlparse ( <EOL> url , '<STR_LIT:http>' ) <EOL> if params : <EOL> path += '<STR_LIT:;>' + params <EOL> if query : <EOL> path += '<STR_LIT:?>' + query <EOL> try : <EOL> conn = ( http_client . HTTPSConnection if scheme == '<STR_LIT>' <EOL> else http_client . HTTPConnection ) ( netloc ) <EOL> try : <EOL> conn . request ( '<STR_LIT>' , path ) <EOL> res = conn . getresponse ( ) <EOL> finally : <EOL> conn . close ( ) <EOL> except http_client . HTTPException as e : <EOL> e = str ( e ) <EOL> if str is not six . text_type : <EOL> try : <EOL> e = e . decode ( '<STR_LIT:utf-8>' ) <EOL> except UnicodeDecodeError : <EOL> try : <EOL> e = e . decode ( '<STR_LIT>' ) <EOL> except UnicodeDecodeError : <EOL> e = e . decode ( '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , error = e ) , state , url ) <EOL> except socket . error as e : <EOL> e = str ( e ) <EOL> if str is not six . text_type : <EOL> try : <EOL> e = e . decode ( '<STR_LIT:utf-8>' ) <EOL> except UnicodeDecodeError : <EOL> try : <EOL> e = e . decode ( '<STR_LIT>' ) <EOL> except UnicodeDecodeError : <EOL> e = e . decode ( '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , error = e ) , state , url ) <EOL> else : <EOL> if res . status == <NUM_LIT> : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state ) , state , url ) <EOL> if not <NUM_LIT:200> <= res . status < <NUM_LIT> : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT:status>' , state , status = res . status ) , <EOL> state , url ) <EOL> class XRI ( FancyValidator ) : <EOL> r"""<STR_LIT>""" <EOL> iname_valid_pattern = re . compile ( r"""<STR_LIT>""" , re . VERBOSE | re . UNICODE ) <EOL> iname_invalid_start = re . compile ( r"<STR_LIT>" , re . UNICODE ) <EOL> """<STR_LIT>""" <EOL> inumber_pattern = re . compile ( r"""<STR_LIT>""" , re . VERBOSE | re . IGNORECASE ) <EOL> messages = dict ( <EOL> noType = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> repeatedChar = _ ( '<STR_LIT>' ) , <EOL> badIname = _ ( '<STR_LIT>' ) , <EOL> badInameStart = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> badInumber = _ ( '<STR_LIT>' ) , <EOL> badType = _ ( '<STR_LIT>' ) , <EOL> badXri = _ ( '<STR_LIT>' ) ) <EOL> def __init__ ( self , add_xri = False , xri_type = "<STR_LIT>" , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . add_xri = add_xri <EOL> assert xri_type in ( '<STR_LIT>' , '<STR_LIT>' ) , ( <EOL> '<STR_LIT>' ) <EOL> self . xri_type = xri_type <EOL> super ( XRI , self ) . __init__ ( ** kwargs ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> """<STR_LIT>""" <EOL> value = value . strip ( ) <EOL> if self . add_xri and not value . startswith ( '<STR_LIT>' ) : <EOL> value = '<STR_LIT>' + value <EOL> return value <EOL> def _validate_python ( self , value , state = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , six . string_types ) : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> type = str ( type ( value ) ) , value = value ) , value , state ) <EOL> if value . startswith ( '<STR_LIT>' ) : <EOL> value = value [ <NUM_LIT:6> : ] <EOL> if not value [ <NUM_LIT:0> ] in ( '<STR_LIT:@>' , '<STR_LIT:=>' ) and not ( <EOL> self . xri_type == '<STR_LIT>' and value [ <NUM_LIT:0> ] == '<STR_LIT:!>' ) : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> if self . xri_type == '<STR_LIT>' : <EOL> self . _validate_iname ( value , state ) <EOL> else : <EOL> self . _validate_inumber ( value , state ) <EOL> def _validate_iname ( self , iname , state ) : <EOL> """<STR_LIT>""" <EOL> iname = iname [ <NUM_LIT:1> : ] <EOL> if '<STR_LIT:..>' in iname or '<STR_LIT>' in iname : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , iname , state ) <EOL> if self . iname_invalid_start . match ( iname ) : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , iname , state ) <EOL> if not self . iname_valid_pattern . match ( iname ) or '<STR_LIT:_>' in iname : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , iname = iname ) , iname , state ) <EOL> def _validate_inumber ( self , inumber , state ) : <EOL> """<STR_LIT>""" <EOL> if not self . __class__ . inumber_pattern . match ( inumber ) : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> inumber = inumber , value = inumber ) , inumber , state ) <EOL> class OpenId ( FancyValidator ) : <EOL> r"""<STR_LIT>""" <EOL> messages = dict ( <EOL> badId = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> def __init__ ( self , add_schema = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . url_validator = URL ( add_http = add_schema ) <EOL> self . iname_validator = XRI ( add_schema , xri_type = "<STR_LIT>" ) <EOL> self . inumber_validator = XRI ( add_schema , xri_type = "<STR_LIT>" ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> value = value . strip ( ) <EOL> try : <EOL> return self . url_validator . to_python ( value , state ) <EOL> except Invalid : <EOL> try : <EOL> return self . iname_validator . to_python ( value , state ) <EOL> except Invalid : <EOL> try : <EOL> return self . inumber_validator . to_python ( value , state ) <EOL> except Invalid : <EOL> pass <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state , id = value ) , value , state ) <EOL> def _validate_python ( self , value , state ) : <EOL> self . _convert_to_python ( value , state ) <EOL> def StateProvince ( * kw , ** kwargs ) : <EOL> deprecation_warning ( "<STR_LIT>" ) <EOL> from formencode . national import USStateProvince <EOL> return USStateProvince ( * kw , ** kwargs ) <EOL> def PhoneNumber ( * kw , ** kwargs ) : <EOL> deprecation_warning ( "<STR_LIT>" ) <EOL> from formencode . national import USPhoneNumber <EOL> return USPhoneNumber ( * kw , ** kwargs ) <EOL> def IPhoneNumberValidator ( * kw , ** kwargs ) : <EOL> deprecation_warning ( <EOL> "<STR_LIT>" ) <EOL> from formencode . national import InternationalPhoneNumber <EOL> return InternationalPhoneNumber ( * kw , ** kwargs ) <EOL> class FieldStorageUploadConverter ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> def _convert_to_python ( self , value , state = None ) : <EOL> if isinstance ( value , cgi . FieldStorage ) : <EOL> if getattr ( value , '<STR_LIT:filename>' , None ) : <EOL> return value <EOL> raise Invalid ( '<STR_LIT>' , value , state ) <EOL> else : <EOL> return value <EOL> def is_empty ( self , value ) : <EOL> if isinstance ( value , cgi . FieldStorage ) : <EOL> return not bool ( getattr ( value , '<STR_LIT:filename>' , None ) ) <EOL> return FancyValidator . is_empty ( self , value ) <EOL> class FileUploadKeeper ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> upload_key = '<STR_LIT>' <EOL> static_key = '<STR_LIT>' <EOL> def _convert_to_python ( self , value , state ) : <EOL> upload = value . get ( self . upload_key ) <EOL> static = value . get ( self . static_key , '<STR_LIT>' ) . strip ( ) <EOL> filename = content = None <EOL> if isinstance ( upload , cgi . FieldStorage ) : <EOL> filename = upload . filename <EOL> content = upload . value <EOL> elif isinstance ( upload , six . string_types ) and upload : <EOL> filename = None <EOL> content = upload <EOL> if not content and static : <EOL> filename , content = static . split ( None , <NUM_LIT:1> ) <EOL> filename = '<STR_LIT>' if filename == '<STR_LIT:->' else filename . decode ( '<STR_LIT>' ) <EOL> content = content . decode ( '<STR_LIT>' ) <EOL> return { '<STR_LIT:filename>' : filename , '<STR_LIT:content>' : content } <EOL> def _convert_from_python ( self , value , state ) : <EOL> filename = value . get ( '<STR_LIT:filename>' , '<STR_LIT>' ) <EOL> content = value . get ( '<STR_LIT:content>' , '<STR_LIT>' ) <EOL> if filename or content : <EOL> result = self . pack_content ( filename , content ) <EOL> return { self . upload_key : '<STR_LIT>' , <EOL> self . static_key : result , <EOL> '<STR_LIT>' : filename , <EOL> '<STR_LIT>' : content } <EOL> else : <EOL> return { self . upload_key : '<STR_LIT>' , <EOL> self . static_key : '<STR_LIT>' } <EOL> def pack_content ( self , filename , content ) : <EOL> enc_filename = self . base64encode ( filename ) or '<STR_LIT:->' <EOL> enc_content = ( content or '<STR_LIT>' ) . encode ( '<STR_LIT>' ) <EOL> result = '<STR_LIT>' % ( enc_filename , enc_content ) <EOL> return result <EOL> class DateConverter ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> accept_day = True <EOL> month_style = '<STR_LIT>' <EOL> separator = '<STR_LIT:/>' <EOL> datetime_module = None <EOL> _month_names = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT>' : <NUM_LIT:4> , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : <NUM_LIT:6> , '<STR_LIT>' : <NUM_LIT:6> , <EOL> '<STR_LIT>' : <NUM_LIT:7> , '<STR_LIT>' : <NUM_LIT:7> , <EOL> '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT:8> , <EOL> '<STR_LIT>' : <NUM_LIT:9> , '<STR_LIT>' : <NUM_LIT:9> , '<STR_LIT>' : <NUM_LIT:9> , <EOL> '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : <NUM_LIT:11> , '<STR_LIT>' : <NUM_LIT:11> , <EOL> '<STR_LIT>' : <NUM_LIT:12> , '<STR_LIT>' : <NUM_LIT:12> , <EOL> } <EOL> _date_re = dict ( <EOL> dmy = re . compile ( <EOL> r'<STR_LIT>' <EOL> % '<STR_LIT:|>' . join ( _month_names ) , re . I ) , <EOL> mdy = re . compile ( <EOL> r'<STR_LIT>' <EOL> % '<STR_LIT:|>' . join ( _month_names ) , re . I ) , <EOL> ymd = re . compile ( <EOL> r'<STR_LIT>' <EOL> % '<STR_LIT:|>' . join ( _month_names ) , re . I ) , <EOL> my = re . compile ( <EOL> r'<STR_LIT>' <EOL> % '<STR_LIT:|>' . join ( _month_names ) , re . I ) , <EOL> ym = re . compile ( <EOL> r'<STR_LIT>' <EOL> % '<STR_LIT:|>' . join ( _month_names ) , re . I ) ) <EOL> _formats = dict ( d = '<STR_LIT>' , m = '<STR_LIT>' , y = '<STR_LIT>' ) <EOL> _human_formats = dict ( d = _ ( '<STR_LIT>' ) , m = _ ( '<STR_LIT>' ) , y = _ ( '<STR_LIT>' ) ) <EOL> _monthDays = { <EOL> <NUM_LIT:1> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT> , <NUM_LIT:4> : <NUM_LIT:30> , <NUM_LIT:5> : <NUM_LIT> , <NUM_LIT:6> : <NUM_LIT:30> , <NUM_LIT:7> : <NUM_LIT> , <NUM_LIT:8> : <NUM_LIT> , <EOL> <NUM_LIT:9> : <NUM_LIT:30> , <NUM_LIT:10> : <NUM_LIT> , <NUM_LIT:11> : <NUM_LIT:30> , <NUM_LIT:12> : <NUM_LIT> } <EOL> messages = dict ( <EOL> badFormat = _ ( '<STR_LIT>' ) , <EOL> monthRange = _ ( '<STR_LIT>' ) , <EOL> invalidDay = _ ( '<STR_LIT>' ) , <EOL> dayRange = _ ( '<STR_LIT>' ) , <EOL> invalidDate = _ ( '<STR_LIT>' ) , <EOL> unknownMonthName = _ ( '<STR_LIT>' ) , <EOL> invalidYear = _ ( '<STR_LIT>' ) , <EOL> fourDigitYear = _ ( '<STR_LIT>' ) , <EOL> wrongFormat = _ ( '<STR_LIT>' ) ) <EOL> def __init__ ( self , * args , ** kw ) : <EOL> super ( DateConverter , self ) . __init__ ( * args , ** kw ) <EOL> month_style = ( self . month_style or DateConverter . month_style ) . lower ( ) <EOL> accept_day = bool ( self . accept_day ) <EOL> self . accept_day = self . accept_day <EOL> if month_style in ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> month_style = '<STR_LIT>' <EOL> elif month_style in ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> month_style = '<STR_LIT>' <EOL> elif month_style in ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> month_style = '<STR_LIT>' <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % month_style ) <EOL> self . month_style = month_style <EOL> separator = self . separator <EOL> if not separator or separator == '<STR_LIT>' : <EOL> separator = dict ( mdy = '<STR_LIT:/>' , dmy = '<STR_LIT:.>' , ymd = '<STR_LIT:->' ) [ month_style ] <EOL> elif separator not in ( '<STR_LIT:->' , '<STR_LIT:.>' , '<STR_LIT:/>' , '<STR_LIT:\\>' ) : <EOL> raise TypeError ( '<STR_LIT>' % separator ) <EOL> self . separator = separator <EOL> self . format = separator . join ( self . _formats [ part ] <EOL> for part in month_style if part != '<STR_LIT:d>' or accept_day ) <EOL> self . human_format = separator . join ( self . _human_formats [ part ] <EOL> for part in month_style if part != '<STR_LIT:d>' or accept_day ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> self . assert_string ( value , state ) <EOL> month_style = self . month_style <EOL> if not self . accept_day : <EOL> month_style = '<STR_LIT>' if month_style == '<STR_LIT>' else '<STR_LIT>' <EOL> match = self . _date_re [ month_style ] . search ( value ) <EOL> if not match : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> format = self . human_format ) , value , state ) <EOL> groups = match . groups ( ) <EOL> if self . accept_day : <EOL> if month_style == '<STR_LIT>' : <EOL> month , day , year = groups <EOL> elif month_style == '<STR_LIT>' : <EOL> day , month , year = groups <EOL> else : <EOL> year , month , day = groups <EOL> day = int ( day ) <EOL> if not <NUM_LIT:1> <= day <= <NUM_LIT> : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> else : <EOL> day = <NUM_LIT:1> <EOL> if month_style == '<STR_LIT>' : <EOL> month , year = groups <EOL> else : <EOL> year , month = groups <EOL> month = self . make_month ( month , state ) <EOL> if not <NUM_LIT:1> <= month <= <NUM_LIT:12> : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> if self . _monthDays [ month ] < day : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> days = self . _monthDays [ month ] ) , value , state ) <EOL> year = self . make_year ( year , state ) <EOL> dt_mod = import_datetime ( self . datetime_module ) <EOL> try : <EOL> return datetime_makedate ( dt_mod , year , month , day ) <EOL> except ValueError as v : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> exception = str ( v ) ) , value , state ) <EOL> def make_month ( self , value , state ) : <EOL> try : <EOL> return int ( value ) <EOL> except ValueError : <EOL> try : <EOL> return self . _month_names [ value . lower ( ) . strip ( ) ] <EOL> except KeyError : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> month = value ) , value , state ) <EOL> def make_year ( self , year , state ) : <EOL> try : <EOL> year = int ( year ) <EOL> except ValueError : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , year , state ) <EOL> if year <= <NUM_LIT:20> : <EOL> year += <NUM_LIT> <EOL> elif <NUM_LIT:50> <= year < <NUM_LIT:100> : <EOL> year += <NUM_LIT> <EOL> if <NUM_LIT:20> < year < <NUM_LIT:50> or <NUM_LIT> < year < <NUM_LIT> : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , year , state ) <EOL> return year <EOL> def _convert_from_python ( self , value , state ) : <EOL> if self . if_empty is not NoDefault and not value : <EOL> return '<STR_LIT>' <EOL> return value . strftime ( self . format ) <EOL> class TimeConverter ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> use_ampm = '<STR_LIT>' <EOL> prefer_ampm = False <EOL> use_seconds = '<STR_LIT>' <EOL> use_datetime = False <EOL> datetime_module = None <EOL> messages = dict ( <EOL> noAMPM = _ ( '<STR_LIT>' ) , <EOL> tooManyColon = _ ( '<STR_LIT>' ) , <EOL> noSeconds = _ ( '<STR_LIT>' ) , <EOL> secondsRequired = _ ( '<STR_LIT>' ) , <EOL> minutesRequired = _ ( '<STR_LIT>' ) , <EOL> badNumber = _ ( '<STR_LIT>' ) , <EOL> badHour = _ ( '<STR_LIT>' ) , <EOL> badMinute = _ ( '<STR_LIT>' ) , <EOL> badSecond = _ ( '<STR_LIT>' ) ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> result = self . _to_python_tuple ( value , state ) <EOL> if self . use_datetime : <EOL> dt_mod = import_datetime ( self . datetime_module ) <EOL> time_class = datetime_time ( dt_mod ) <EOL> return time_class ( * result ) <EOL> else : <EOL> return result <EOL> def _to_python_tuple ( self , value , state ) : <EOL> time = value . strip ( ) <EOL> explicit_ampm = False <EOL> if self . use_ampm : <EOL> last_two = time [ - <NUM_LIT:2> : ] . lower ( ) <EOL> if last_two not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if self . use_ampm != '<STR_LIT>' : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> offset = <NUM_LIT:0> <EOL> else : <EOL> explicit_ampm = True <EOL> offset = <NUM_LIT:12> if last_two == '<STR_LIT>' else <NUM_LIT:0> <EOL> time = time [ : - <NUM_LIT:2> ] <EOL> else : <EOL> offset = <NUM_LIT:0> <EOL> parts = time . split ( '<STR_LIT::>' , <NUM_LIT:3> ) <EOL> if len ( parts ) > <NUM_LIT:3> : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> if len ( parts ) == <NUM_LIT:3> and not self . use_seconds : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> if ( len ( parts ) == <NUM_LIT:2> <EOL> and self . use_seconds and self . use_seconds != '<STR_LIT>' ) : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> try : <EOL> hour = int ( parts [ <NUM_LIT:0> ] ) <EOL> except ValueError : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> number = parts [ <NUM_LIT:0> ] , part = '<STR_LIT>' ) , value , state ) <EOL> if explicit_ampm : <EOL> if not <NUM_LIT:1> <= hour <= <NUM_LIT:12> : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> number = hour , range = '<STR_LIT>' ) , value , state ) <EOL> if hour == <NUM_LIT:12> and offset == <NUM_LIT:12> : <EOL> pass <EOL> elif hour == <NUM_LIT:12> and offset == <NUM_LIT:0> : <EOL> hour = <NUM_LIT:0> <EOL> else : <EOL> hour += offset <EOL> else : <EOL> if not <NUM_LIT:0> <= hour < <NUM_LIT> : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> number = hour , range = '<STR_LIT>' ) , value , state ) <EOL> try : <EOL> minute = int ( parts [ <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> number = parts [ <NUM_LIT:1> ] , part = '<STR_LIT>' ) , value , state ) <EOL> if not <NUM_LIT:0> <= minute < <NUM_LIT> : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , number = minute ) , <EOL> value , state ) <EOL> if len ( parts ) == <NUM_LIT:3> : <EOL> try : <EOL> second = int ( parts [ <NUM_LIT:2> ] ) <EOL> except ValueError : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> number = parts [ <NUM_LIT:2> ] , part = '<STR_LIT>' ) , value , state ) <EOL> if not <NUM_LIT:0> <= second < <NUM_LIT> : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , number = second ) , <EOL> value , state ) <EOL> else : <EOL> second = None <EOL> if second is None : <EOL> return hour , minute <EOL> else : <EOL> return hour , minute , second <EOL> def _convert_from_python ( self , value , state ) : <EOL> if isinstance ( value , six . string_types ) : <EOL> return value <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> hour , minute = value . hour , value . minute <EOL> second = value . second <EOL> elif len ( value ) == <NUM_LIT:3> : <EOL> hour , minute , second = value <EOL> elif len ( value ) == <NUM_LIT:2> : <EOL> hour , minute = value <EOL> second = <NUM_LIT:0> <EOL> ampm = '<STR_LIT>' <EOL> if ( self . use_ampm == '<STR_LIT>' and self . prefer_ampm ) or ( <EOL> self . use_ampm and self . use_ampm != '<STR_LIT>' ) : <EOL> ampm = '<STR_LIT>' <EOL> if hour > <NUM_LIT:12> : <EOL> hour -= <NUM_LIT:12> <EOL> ampm = '<STR_LIT>' <EOL> elif hour == <NUM_LIT:12> : <EOL> ampm = '<STR_LIT>' <EOL> elif hour == <NUM_LIT:0> : <EOL> hour = <NUM_LIT:12> <EOL> if self . use_seconds : <EOL> return '<STR_LIT>' % ( hour , minute , second , ampm ) <EOL> else : <EOL> return '<STR_LIT>' % ( hour , minute , ampm ) <EOL> class ISODateTimeConverter ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> datetime_module = None <EOL> messages = dict ( <EOL> invalidFormat = _ ( '<STR_LIT>' ) , ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> dt_mod = import_datetime ( self . datetime_module ) <EOL> datetime_class = dt_mod . datetime <EOL> try : <EOL> datetime = datetime_class . strptime ( value , r"<STR_LIT>" ) <EOL> return datetime <EOL> except ValueError : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> def _convert_from_python ( self , value , state ) : <EOL> return value . isoformat ( "<STR_LIT:T>" ) <EOL> def PostalCode ( * kw , ** kwargs ) : <EOL> deprecation_warning ( "<STR_LIT>" ) <EOL> from formencode . national import USPostalCode <EOL> return USPostalCode ( * kw , ** kwargs ) <EOL> class StripField ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> __unpackargs__ = ( '<STR_LIT:name>' , ) <EOL> messages = dict ( <EOL> missing = _ ( '<STR_LIT>' ) ) <EOL> def _convert_to_python ( self , valueDict , state ) : <EOL> v = valueDict . copy ( ) <EOL> try : <EOL> field = v . pop ( self . name ) <EOL> except KeyError : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , name = repr ( self . name ) ) , <EOL> valueDict , state ) <EOL> return field , v <EOL> def is_empty ( self , value ) : <EOL> return False <EOL> class StringBool ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> true_values = [ '<STR_LIT:true>' , '<STR_LIT:t>' , '<STR_LIT:yes>' , '<STR_LIT:y>' , '<STR_LIT>' , '<STR_LIT:1>' ] <EOL> false_values = [ '<STR_LIT:false>' , '<STR_LIT:f>' , '<STR_LIT>' , '<STR_LIT:n>' , '<STR_LIT>' , '<STR_LIT:0>' ] <EOL> messages = dict ( <EOL> string = _ ( '<STR_LIT>' ) ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> if isinstance ( value , six . string_types ) : <EOL> value = value . strip ( ) . lower ( ) <EOL> if value in self . true_values : <EOL> return True <EOL> if not value or value in self . false_values : <EOL> return False <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT:string>' , state , <EOL> true = self . true_values [ <NUM_LIT:0> ] , false = self . false_values [ <NUM_LIT:0> ] ) , <EOL> value , state ) <EOL> return bool ( value ) <EOL> def _convert_from_python ( self , value , state ) : <EOL> return ( self . true_values if value else self . false_values ) [ <NUM_LIT:0> ] <EOL> StringBoolean = StringBool <EOL> class SignedString ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> messages = dict ( <EOL> malformed = _ ( '<STR_LIT>' ) , <EOL> badsig = _ ( '<STR_LIT>' ) ) <EOL> secret = None <EOL> nonce_length = <NUM_LIT:4> <EOL> def _convert_to_python ( self , value , state ) : <EOL> global sha1 <EOL> if not sha1 : <EOL> from hashlib import sha1 <EOL> assert self . secret is not None , "<STR_LIT>" <EOL> parts = value . split ( None , <NUM_LIT:1> ) <EOL> if not parts or len ( parts ) == <NUM_LIT:1> : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> sig , rest = parts <EOL> sig = sig . decode ( '<STR_LIT>' ) <EOL> rest = rest . decode ( '<STR_LIT>' ) <EOL> nonce = rest [ : self . nonce_length ] <EOL> rest = rest [ self . nonce_length : ] <EOL> expected = sha1 ( str ( self . secret ) + nonce + rest ) . digest ( ) <EOL> if expected != sig : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> return rest <EOL> def _convert_from_python ( self , value , state ) : <EOL> global sha1 <EOL> if not sha1 : <EOL> from hashlib import sha1 <EOL> nonce = self . make_nonce ( ) <EOL> value = str ( value ) <EOL> digest = sha1 ( self . secret + nonce + value ) . digest ( ) <EOL> return self . encode ( digest ) + '<STR_LIT:U+0020>' + self . encode ( nonce + value ) <EOL> def encode ( self , value ) : <EOL> return value . encode ( '<STR_LIT>' ) . strip ( ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> def make_nonce ( self ) : <EOL> global random <EOL> if not random : <EOL> import random <EOL> return '<STR_LIT>' . join ( chr ( random . randrange ( <NUM_LIT> ) ) <EOL> for _i in range ( self . nonce_length ) ) <EOL> class IPAddress ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> messages = dict ( <EOL> badFormat = _ ( '<STR_LIT>' ) , <EOL> leadingZeros = _ ( '<STR_LIT>' ) , <EOL> illegalOctets = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> leading_zeros = False <EOL> def _validate_python ( self , value , state = None ) : <EOL> try : <EOL> if not value : <EOL> raise ValueError <EOL> octets = value . split ( '<STR_LIT:.>' , <NUM_LIT:5> ) <EOL> if len ( octets ) != <NUM_LIT:4> : <EOL> raise ValueError <EOL> for octet in octets : <EOL> if octet . startswith ( '<STR_LIT:0>' ) and octet != '<STR_LIT:0>' : <EOL> if not self . leading_zeros : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> octet = octet . lstrip ( '<STR_LIT:0>' ) <EOL> if not <NUM_LIT:0> <= int ( octet ) < <NUM_LIT> : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , octet = octet ) , <EOL> value , state ) <EOL> except ValueError : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> class CIDR ( IPAddress ) : <EOL> """<STR_LIT>""" <EOL> messages = dict ( IPAddress . _messages , <EOL> badFormat = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> illegalBits = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> def _validate_python ( self , value , state ) : <EOL> try : <EOL> if '<STR_LIT:/>' in value : <EOL> addr , bits = value . split ( '<STR_LIT:/>' ) <EOL> else : <EOL> addr , bits = value , <NUM_LIT:32> <EOL> IPAddress . _validate_python ( self , addr , state ) <EOL> if not <NUM_LIT:8> <= int ( bits ) <= <NUM_LIT:32> : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , bits = bits ) , <EOL> value , state ) <EOL> except ValueError : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , value , state ) <EOL> class MACAddress ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> strip = True <EOL> valid_characters = '<STR_LIT>' <EOL> add_colons = False <EOL> messages = dict ( <EOL> badLength = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> badCharacter = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> def _convert_to_python ( self , value , state ) : <EOL> address = value . replace ( '<STR_LIT::>' , '<STR_LIT>' ) . lower ( ) <EOL> if len ( address ) != <NUM_LIT:12> : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> length = len ( address ) ) , address , state ) <EOL> for char in address : <EOL> if char not in self . valid_characters : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , <EOL> char = char ) , address , state ) <EOL> if self . add_colons : <EOL> address = '<STR_LIT>' % ( <EOL> address [ <NUM_LIT:0> : <NUM_LIT:2> ] , address [ <NUM_LIT:2> : <NUM_LIT:4> ] , address [ <NUM_LIT:4> : <NUM_LIT:6> ] , <EOL> address [ <NUM_LIT:6> : <NUM_LIT:8> ] , address [ <NUM_LIT:8> : <NUM_LIT:10> ] , address [ <NUM_LIT:10> : <NUM_LIT:12> ] ) <EOL> return address <EOL> _convert_from_python = _convert_to_python <EOL> class FormValidator ( FancyValidator ) : <EOL> """<STR_LIT>""" <EOL> validate_partial_form = False <EOL> validate_partial_python = None <EOL> validate_partial_other = None <EOL> def is_empty ( self , value ) : <EOL> return False <EOL> def field_is_empty ( self , value ) : <EOL> return is_empty ( value ) <EOL> class RequireIfMissing ( FormValidator ) : <EOL> """<STR_LIT>""" <EOL> required = None <EOL> missing = None <EOL> present = None <EOL> __unpackargs__ = ( '<STR_LIT>' , ) <EOL> def _convert_to_python ( self , value_dict , state ) : <EOL> is_empty = self . field_is_empty <EOL> if is_empty ( value_dict . get ( self . required ) ) and ( <EOL> ( self . missing and is_empty ( value_dict . get ( self . missing ) ) ) or <EOL> ( self . present and not is_empty ( value_dict . get ( self . present ) ) ) ) : <EOL> raise Invalid ( <EOL> _ ( '<STR_LIT>' ) % self . required , <EOL> value_dict , state , <EOL> error_dict = { self . required : <EOL> Invalid ( self . message ( '<STR_LIT>' , state ) , <EOL> value_dict . get ( self . required ) , state ) } ) <EOL> return value_dict <EOL> RequireIfPresent = RequireIfMissing <EOL> class RequireIfMatching ( FormValidator ) : <EOL> """<STR_LIT>""" <EOL> field = None <EOL> expected_value = None <EOL> required_fields = [ ] <EOL> __unpackargs__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _convert_to_python ( self , value_dict , state ) : <EOL> is_empty = self . field_is_empty <EOL> if self . field in value_dict and value_dict . get ( self . field ) == self . expected_value : <EOL> for required_field in self . required_fields : <EOL> if required_field not in value_dict or is_empty ( value_dict . get ( required_field ) ) : <EOL> raise Invalid ( <EOL> _ ( '<STR_LIT>' ) % required_field , <EOL> value_dict , state , <EOL> error_dict = { required_field : <EOL> Invalid ( self . message ( '<STR_LIT>' , state ) , <EOL> value_dict . get ( required_field ) , state ) } ) <EOL> return value_dict <EOL> class FieldsMatch ( FormValidator ) : <EOL> """<STR_LIT>""" <EOL> show_match = False <EOL> field_names = None <EOL> validate_partial_form = True <EOL> __unpackargs__ = ( '<STR_LIT:*>' , '<STR_LIT>' ) <EOL> messages = dict ( <EOL> invalid = _ ( '<STR_LIT>' ) , <EOL> invalidNoMatch = _ ( '<STR_LIT>' ) , <EOL> notDict = _ ( '<STR_LIT>' ) ) <EOL> def __init__ ( self , * args , ** kw ) : <EOL> super ( FieldsMatch , self ) . __init__ ( * args , ** kw ) <EOL> if len ( self . field_names ) < <NUM_LIT:2> : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def validate_partial ( self , field_dict , state ) : <EOL> for name in self . field_names : <EOL> if name not in field_dict : <EOL> return <EOL> self . _validate_python ( field_dict , state ) <EOL> def _validate_python ( self , field_dict , state ) : <EOL> try : <EOL> ref = field_dict [ self . field_names [ <NUM_LIT:0> ] ] <EOL> except TypeError : <EOL> raise Invalid ( self . message ( '<STR_LIT>' , state ) , field_dict , state ) <EOL> except KeyError : <EOL> ref = '<STR_LIT>' <EOL> errors = { } <EOL> for name in self . field_names [ <NUM_LIT:1> : ] : <EOL> if field_dict . get ( name , '<STR_LIT>' ) != ref : <EOL> if self . show_match : <EOL> errors [ name ] = self . message ( '<STR_LIT>' , state , <EOL> match = ref ) <EOL> else : <EOL> errors [ name ] = self . message ( '<STR_LIT>' , state ) <EOL> if errors : <EOL> error_list = sorted ( six . iteritems ( errors ) ) <EOL> error_message = '<STR_LIT>' . join ( <EOL> '<STR_LIT>' % ( name , value ) for name , value in error_list ) <EOL> raise Invalid ( error_message , field_dict , state , error_dict = errors ) <EOL> class CreditCardValidator ( FormValidator ) : <EOL> """<STR_LIT>""" <EOL> validate_partial_form = True <EOL> cc_type_field = '<STR_LIT>' <EOL> cc_number_field = '<STR_LIT>' <EOL> __unpackargs__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> messages = dict ( <EOL> notANumber = _ ( '<STR_LIT>' ) , <EOL> badLength = _ ( '<STR_LIT>' ) , <EOL> invalidNumber = _ ( '<STR_LIT>' ) , <EOL> missing_key = _ ( '<STR_LIT>' ) ) <EOL> def validate_partial ( self , field_dict , state ) : <EOL> if not field_dict . get ( self . cc_type_field , None ) or not field_dict . get ( self . cc_number_field , None ) : <EOL> return None <EOL> self . _validate_python ( field_dict , state ) <EOL> def _validate_python ( self , field_dict , state ) : <EOL> errors = self . _validateReturn ( field_dict , state ) <EOL> if errors : <EOL> error_list = sorted ( six . iteritems ( errors ) ) <EOL> raise Invalid ( <EOL> '<STR_LIT>' . join ( '<STR_LIT>' % ( name , value ) <EOL> for name , value in error_list ) , <EOL> field_dict , state , error_dict = errors ) <EOL> def _validateReturn ( self , field_dict , state ) : <EOL> for field in self . cc_type_field , self . cc_number_field : <EOL> if field not in field_dict : <EOL> raise Invalid ( <EOL> self . message ( '<STR_LIT>' , state , key = field ) , <EOL> field_dict , state ) <EOL> ccType = field_dict [ self . cc_type_field ] . lower ( ) . strip ( ) <EOL> number = field_dict [ self . cc_number_field ] . strip ( ) <EOL> number = number . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> number = number . replace ( '<STR_LIT:->' , '<STR_LIT>' ) <EOL> try : <EOL> int ( number ) <EOL> except ValueError : <EOL> return { self . cc_number_field : self . message ( '<STR_LIT>' , state ) } <EOL> assert ccType in self . _cardInfo , ( <EOL> "<STR_LIT>" ) <EOL> foundValid = False <EOL> validLength = False <EOL> for prefix , length in self . _cardInfo [ ccType ] : <EOL> if len ( number ) == length : <EOL> validLength = True <EOL> if number . startswith ( prefix ) : <EOL> foundValid = True <EOL> break <EOL> if not validLength : <EOL> return { self . cc_number_field : self . message ( '<STR_LIT>' , state ) } <EOL> if not foundValid : <EOL> return { self . cc_number_field : self . message ( '<STR_LIT>' , state ) } <EOL> if not self . _validateMod10 ( number ) : <EOL> return { self . cc_number_field : self . message ( '<STR_LIT>' , state ) } <EOL> return None <EOL> def _validateMod10 ( self , s ) : <EOL> """<STR_LIT>""" <EOL> checksum , factor = <NUM_LIT:0> , <NUM_LIT:1> <EOL> for c in reversed ( s ) : <EOL> for c in str ( factor * int ( c ) ) : <EOL> checksum += int ( c ) <EOL> factor = <NUM_LIT:3> - factor <EOL> return checksum % <NUM_LIT:10> == <NUM_LIT:0> <EOL> _cardInfo = { <EOL> "<STR_LIT>" : [ ( '<STR_LIT:4>' , <NUM_LIT:16> ) , <EOL> ( '<STR_LIT:4>' , <NUM_LIT> ) ] , <EOL> "<STR_LIT>" : [ ( '<STR_LIT>' , <NUM_LIT:16> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:16> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:16> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:16> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:16> ) ] , <EOL> "<STR_LIT>" : [ ( '<STR_LIT>' , <NUM_LIT:16> ) ] , <EOL> "<STR_LIT>" : [ ( '<STR_LIT>' , <NUM_LIT:15> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:15> ) ] , <EOL> "<STR_LIT>" : [ ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) ] , <EOL> "<STR_LIT>" : [ ( '<STR_LIT:3>' , <NUM_LIT:16> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:15> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:15> ) ] , <EOL> } <EOL> class CreditCardExpires ( FormValidator ) : <EOL> """<STR_LIT>""" <EOL> validate_partial_form = True <EOL> cc_expires_month_field = '<STR_LIT>' <EOL> cc_expires_year_field = '<STR_LIT>' <EOL> __unpackargs__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> datetime_module = None <EOL> messages = dict ( <EOL> notANumber = _ ( '<STR_LIT>' ) , <EOL> invalidNumber = _ ( '<STR_LIT>' ) ) <EOL> def validate_partial ( self , field_dict , state ) : <EOL> if not field_dict . get ( self . cc_expires_month_field , None ) or not field_dict . get ( self . cc_expires_year_field , None ) : <EOL> return None <EOL> self . _validate_python ( field_dict , state ) <EOL> def _validate_python ( self , field_dict , state ) : <EOL> errors = self . _validateReturn ( field_dict , state ) <EOL> if errors : <EOL> error_list = sorted ( six . iteritems ( errors ) ) <EOL> raise Invalid ( <EOL> '<STR_LIT>' . join ( '<STR_LIT>' % ( name , value ) <EOL> for name , value in error_list ) , <EOL> field_dict , state , error_dict = errors ) <EOL> def _validateReturn ( self , field_dict , state ) : <EOL> ccExpiresMonth = str ( field_dict [ self . cc_expires_month_field ] ) . strip ( ) <EOL> ccExpiresYear = str ( field_dict [ self . cc_expires_year_field ] ) . strip ( ) <EOL> try : <EOL> ccExpiresMonth = int ( ccExpiresMonth ) <EOL> ccExpiresYear = int ( ccExpiresYear ) <EOL> dt_mod = import_datetime ( self . datetime_module ) <EOL> now = datetime_now ( dt_mod ) <EOL> today = datetime_makedate ( dt_mod , now . year , now . month , now . day ) <EOL> next_month = ccExpiresMonth % <NUM_LIT:12> + <NUM_LIT:1> <EOL> next_month_year = ccExpiresYear <EOL> if next_month == <NUM_LIT:1> : <EOL> next_month_year += <NUM_LIT:1> <EOL> expires_date = datetime_makedate ( <EOL> dt_mod , next_month_year , next_month , <NUM_LIT:1> ) <EOL> assert expires_date > today <EOL> except ValueError : <EOL> return { self . cc_expires_month_field : <EOL> self . message ( '<STR_LIT>' , state ) , <EOL> self . cc_expires_year_field : <EOL> self . message ( '<STR_LIT>' , state ) } <EOL> except AssertionError : <EOL> return { self . cc_expires_month_field : <EOL> self . message ( '<STR_LIT>' , state ) , <EOL> self . cc_expires_year_field : <EOL> self . message ( '<STR_LIT>' , state ) } <EOL> class CreditCardSecurityCode ( FormValidator ) : <EOL> """<STR_LIT>""" <EOL> validate_partial_form = True <EOL> cc_type_field = '<STR_LIT>' <EOL> cc_code_field = '<STR_LIT>' <EOL> __unpackargs__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> messages = dict ( <EOL> notANumber = _ ( '<STR_LIT>' ) , <EOL> badLength = _ ( '<STR_LIT>' ) ) <EOL> def validate_partial ( self , field_dict , state ) : <EOL> if ( not field_dict . get ( self . cc_type_field , None ) <EOL> or not field_dict . get ( self . cc_code_field , None ) ) : <EOL> return None <EOL> self . _validate_python ( field_dict , state ) <EOL> def _validate_python ( self , field_dict , state ) : <EOL> errors = self . _validateReturn ( field_dict , state ) <EOL> if errors : <EOL> error_list = sorted ( six . iteritems ( errors ) ) <EOL> raise Invalid ( <EOL> '<STR_LIT>' . join ( <EOL> '<STR_LIT>' % ( name , value ) <EOL> for name , value in error_list ) , <EOL> field_dict , state , error_dict = errors ) <EOL> def _validateReturn ( self , field_dict , state ) : <EOL> ccType = str ( field_dict [ self . cc_type_field ] ) . strip ( ) <EOL> ccCode = str ( field_dict [ self . cc_code_field ] ) . strip ( ) <EOL> try : <EOL> int ( ccCode ) <EOL> except ValueError : <EOL> return { self . cc_code_field : self . message ( '<STR_LIT>' , state ) } <EOL> length = self . _cardInfo [ ccType ] <EOL> if len ( ccCode ) != length : <EOL> return { self . cc_code_field : self . message ( '<STR_LIT>' , state ) } <EOL> _cardInfo = dict ( visa = <NUM_LIT:3> , mastercard = <NUM_LIT:3> , discover = <NUM_LIT:3> , amex = <NUM_LIT:4> ) <EOL> def validators ( ) : <EOL> """<STR_LIT>""" <EOL> return [ name for name , value in six . iteritems ( globals ( ) ) <EOL> if isinstance ( value , type ) and issubclass ( value , Validator ) ] <EOL> __all__ = [ '<STR_LIT>' ] + validators ( ) </s>
