<s> import platform <EOL> import re <EOL> import urllib <EOL> import urllib2 <EOL> import urlparse <EOL> from django . core . exceptions import ValidationError <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . encoding import smart_unicode <EOL> EMPTY_VALUES = ( None , '<STR_LIT>' , [ ] , ( ) , { } ) <EOL> try : <EOL> from django . conf import settings <EOL> URL_VALIDATOR_USER_AGENT = settings . URL_VALIDATOR_USER_AGENT <EOL> except ImportError : <EOL> URL_VALIDATOR_USER_AGENT = '<STR_LIT>' <EOL> class RegexValidator ( object ) : <EOL> regex = '<STR_LIT>' <EOL> message = _ ( u'<STR_LIT>' ) <EOL> code = '<STR_LIT>' <EOL> def __init__ ( self , regex = None , message = None , code = None ) : <EOL> if regex is not None : <EOL> self . regex = regex <EOL> if message is not None : <EOL> self . message = message <EOL> if code is not None : <EOL> self . code = code <EOL> if isinstance ( self . regex , basestring ) : <EOL> self . regex = re . compile ( regex ) <EOL> def __call__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not self . regex . search ( smart_unicode ( value ) ) : <EOL> raise ValidationError ( self . message , code = self . code ) <EOL> class URLValidator ( RegexValidator ) : <EOL> regex = re . compile ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' , re . IGNORECASE ) <EOL> def __init__ ( self , verify_exists = False , <EOL> validator_user_agent = URL_VALIDATOR_USER_AGENT ) : <EOL> super ( URLValidator , self ) . __init__ ( ) <EOL> self . verify_exists = verify_exists <EOL> self . user_agent = validator_user_agent <EOL> def __call__ ( self , value ) : <EOL> try : <EOL> super ( URLValidator , self ) . __call__ ( value ) <EOL> except ValidationError , e : <EOL> if value : <EOL> value = smart_unicode ( value ) <EOL> scheme , netloc , path , query , fragment = urlparse . urlsplit ( value ) <EOL> try : <EOL> netloc = netloc . encode ( '<STR_LIT>' ) <EOL> except UnicodeError : <EOL> raise e <EOL> url = urlparse . urlunsplit ( ( scheme , netloc , path , query , fragment ) ) <EOL> super ( URLValidator , self ) . __call__ ( url ) <EOL> else : <EOL> raise <EOL> else : <EOL> url = value <EOL> if self . verify_exists : <EOL> headers = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : self . user_agent , <EOL> } <EOL> url = url . encode ( '<STR_LIT:utf-8>' ) <EOL> url = urllib . quote ( url , "<STR_LIT>" ) <EOL> broken_error = ValidationError ( <EOL> _ ( u'<STR_LIT>' ) , code = '<STR_LIT>' ) <EOL> try : <EOL> req = urllib2 . Request ( url , None , headers ) <EOL> req . get_method = lambda : '<STR_LIT>' <EOL> opener = urllib2 . OpenerDirector ( ) <EOL> error_nop = lambda * args , ** kwargs : True <EOL> http_error_processor = urllib2 . HTTPErrorProcessor ( ) <EOL> http_error_processor . http_error_301 = error_nop <EOL> http_error_processor . http_error_302 = error_nop <EOL> http_error_processor . http_error_307 = error_nop <EOL> handlers = [ urllib2 . UnknownHandler ( ) , <EOL> urllib2 . HTTPHandler ( ) , <EOL> urllib2 . HTTPDefaultErrorHandler ( ) , <EOL> urllib2 . FTPHandler ( ) , <EOL> http_error_processor ] <EOL> try : <EOL> import ssl <EOL> handlers . append ( urllib2 . HTTPSHandler ( ) ) <EOL> except : <EOL> pass <EOL> map ( opener . add_handler , handlers ) <EOL> if platform . python_version_tuple ( ) >= ( <NUM_LIT:2> , <NUM_LIT:6> ) : <EOL> opener . open ( req , timeout = <NUM_LIT:10> ) <EOL> else : <EOL> opener . open ( req ) <EOL> except ValueError : <EOL> raise ValidationError ( _ ( u'<STR_LIT>' ) , code = '<STR_LIT>' ) <EOL> except : <EOL> raise broken_error <EOL> def validate_integer ( value ) : <EOL> try : <EOL> int ( value ) <EOL> except ( ValueError , TypeError ) , e : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> class EmailValidator ( RegexValidator ) : <EOL> def __call__ ( self , value ) : <EOL> try : <EOL> super ( EmailValidator , self ) . __call__ ( value ) <EOL> except ValidationError , e : <EOL> if value and u'<STR_LIT:@>' in value : <EOL> parts = value . split ( u'<STR_LIT:@>' ) <EOL> domain_part = parts [ - <NUM_LIT:1> ] <EOL> try : <EOL> parts [ - <NUM_LIT:1> ] = parts [ - <NUM_LIT:1> ] . encode ( '<STR_LIT>' ) <EOL> except UnicodeError : <EOL> raise e <EOL> super ( EmailValidator , self ) . __call__ ( u'<STR_LIT:@>' . join ( parts ) ) <EOL> else : <EOL> raise <EOL> email_re = re . compile ( <EOL> r"<STR_LIT>" <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' , re . IGNORECASE ) <EOL> validate_email = EmailValidator ( email_re , _ ( u'<STR_LIT>' ) , '<STR_LIT>' ) <EOL> slug_re = re . compile ( r'<STR_LIT>' ) <EOL> validate_slug = RegexValidator ( slug_re , _ ( u"<STR_LIT>" ) , '<STR_LIT>' ) <EOL> ipv4_re = re . compile ( r'<STR_LIT>' ) <EOL> validate_ipv4_address = RegexValidator ( ipv4_re , _ ( u'<STR_LIT>' ) , '<STR_LIT>' ) <EOL> comma_separated_int_list_re = re . compile ( '<STR_LIT>' ) <EOL> validate_comma_separated_integer_list = RegexValidator ( comma_separated_int_list_re , _ ( u'<STR_LIT>' ) , '<STR_LIT>' ) <EOL> class BaseValidator ( object ) : <EOL> compare = lambda self , a , b : a is not b <EOL> clean = lambda self , x : x <EOL> message = _ ( u'<STR_LIT>' ) <EOL> code = '<STR_LIT>' <EOL> def __init__ ( self , limit_value ) : <EOL> self . limit_value = limit_value <EOL> def __call__ ( self , value ) : <EOL> cleaned = self . clean ( value ) <EOL> params = { '<STR_LIT>' : self . limit_value , '<STR_LIT>' : cleaned } <EOL> if self . compare ( cleaned , self . limit_value ) : <EOL> raise ValidationError ( <EOL> self . message % params , <EOL> code = self . code , <EOL> params = params , <EOL> ) <EOL> class MaxValueValidator ( BaseValidator ) : <EOL> compare = lambda self , a , b : a > b <EOL> message = _ ( u'<STR_LIT>' ) <EOL> code = '<STR_LIT>' <EOL> class MinValueValidator ( BaseValidator ) : <EOL> compare = lambda self , a , b : a < b <EOL> message = _ ( u'<STR_LIT>' ) <EOL> code = '<STR_LIT>' <EOL> class MinLengthValidator ( BaseValidator ) : <EOL> compare = lambda self , a , b : a < b <EOL> clean = lambda self , x : len ( x ) <EOL> message = _ ( u'<STR_LIT>' ) <EOL> code = '<STR_LIT>' <EOL> class MaxLengthValidator ( BaseValidator ) : <EOL> compare = lambda self , a , b : a > b <EOL> clean = lambda self , x : len ( x ) <EOL> message = _ ( u'<STR_LIT>' ) <EOL> code = '<STR_LIT:max_length>' </s>
