<s> """<STR_LIT>""" <EOL> import re <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class ValidationException ( ValueError ) : <EOL> pass <EOL> class BaseValidator ( object ) : <EOL> def validate_or_raise ( self , * a , ** k ) : <EOL> """<STR_LIT>""" <EOL> validate , err = self . validate ( * a , ** k ) <EOL> if err : <EOL> raise ValidationException ( err ) <EOL> return validate <EOL> class DomainValidator ( BaseValidator ) : <EOL> """<STR_LIT>""" <EOL> domain_pattern = r'<STR_LIT>' <EOL> domain_regex = re . compile ( '<STR_LIT>' + domain_pattern + '<STR_LIT:$>' , re . IGNORECASE | re . UNICODE ) <EOL> false_positive_ips = [ '<STR_LIT>' ] <EOL> def __init__ ( self , fix = False , lookup_dns = None ) : <EOL> self . fix = fix <EOL> if lookup_dns : <EOL> try : <EOL> import DNS <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> lookup_dns = lookup_dns . lower ( ) <EOL> if lookup_dns not in ( '<STR_LIT:a>' , '<STR_LIT>' ) : <EOL> raise RuntimeError ( "<STR_LIT>" + lookup_dns ) <EOL> self . _lookup_dns = lookup_dns <EOL> def _apply_common_rules ( self , part , maxlength ) : <EOL> """<STR_LIT>""" <EOL> part = part . strip ( ) <EOL> if self . fix : <EOL> part = part . strip ( '<STR_LIT:.>' ) <EOL> if not part : <EOL> return part , '<STR_LIT>' <EOL> if len ( part ) > maxlength : <EOL> return part , '<STR_LIT>' % maxlength <EOL> if part [ <NUM_LIT:0> ] == '<STR_LIT:.>' : <EOL> return part , '<STR_LIT>' <EOL> if part [ - <NUM_LIT:1> ] == '<STR_LIT:.>' : <EOL> return part , '<STR_LIT>' <EOL> if '<STR_LIT:..>' in part : <EOL> return part , '<STR_LIT>' <EOL> return part , '<STR_LIT>' <EOL> def validate_domain ( self , part ) : <EOL> part , err = self . _apply_common_rules ( part , maxlength = <NUM_LIT:255> ) <EOL> if err : <EOL> return part , '<STR_LIT>' % err <EOL> if not self . domain_regex . search ( part ) : <EOL> return part , '<STR_LIT>' <EOL> if self . _lookup_dns and not self . lookup_domain ( part ) : <EOL> return part , '<STR_LIT>' <EOL> return part . lower ( ) , '<STR_LIT>' <EOL> validate = validate_domain <EOL> def lookup_domain ( self , domain , lookup_record = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> import DNS <EOL> lookup_record = lookup_record . lower ( ) if lookup_record else self . _lookup_dns <EOL> if lookup_record not in ( '<STR_LIT:a>' , '<STR_LIT>' ) : <EOL> raise RuntimeError ( "<STR_LIT>" + lookup_record ) <EOL> if lookup_record == "<STR_LIT:a>" : <EOL> request = DNS . Request ( domain , ** kw ) <EOL> try : <EOL> answers = request . req ( ) . answers <EOL> except ( DNS . Lib . PackError , UnicodeError ) : <EOL> return False <EOL> if not answers : <EOL> return False <EOL> result = answers [ <NUM_LIT:0> ] [ '<STR_LIT:data>' ] <EOL> if result in self . false_positive_ips : <EOL> return False <EOL> return result <EOL> try : <EOL> return DNS . mxlookup ( domain ) <EOL> except UnicodeError : <EOL> pass <EOL> return False <EOL> class EmailValidator ( DomainValidator ) : <EOL> def __init__ ( self , local_part_chars = "<STR_LIT>" , ** k ) : <EOL> super ( EmailValidator , self ) . __init__ ( ** k ) <EOL> self . local_part_pattern = '<STR_LIT>' + local_part_chars . replace ( '<STR_LIT:->' , r'<STR_LIT>' ) + '<STR_LIT>' <EOL> self . local_part_regex = re . compile ( '<STR_LIT>' + self . local_part_pattern + '<STR_LIT:$>' , re . IGNORECASE ) <EOL> def validate_local_part ( self , part ) : <EOL> part , err = self . _apply_common_rules ( part , maxlength = <NUM_LIT:64> ) <EOL> if err : <EOL> return part , '<STR_LIT>' % err <EOL> if not self . local_part_regex . search ( part ) : <EOL> return part , '<STR_LIT>' <EOL> return part , '<STR_LIT>' <EOL> def validate_email ( self , email ) : <EOL> if not email : <EOL> return email , '<STR_LIT>' <EOL> parts = email . split ( '<STR_LIT:@>' ) <EOL> if len ( parts ) != <NUM_LIT:2> : <EOL> return email , '<STR_LIT>' <EOL> local , domain = parts <EOL> domain , err = self . validate_domain ( domain ) <EOL> if err : <EOL> return email , "<STR_LIT>" % err <EOL> local , err = self . validate_local_part ( local ) <EOL> if err : <EOL> return email , "<STR_LIT>" % err <EOL> return local + '<STR_LIT:@>' + domain , '<STR_LIT>' <EOL> validate = validate_email <EOL> class EmailHarvester ( EmailValidator ) : <EOL> def __init__ ( self , * a , ** k ) : <EOL> super ( EmailHarvester , self ) . __init__ ( * a , ** k ) <EOL> self . harvest_regex = re . compile ( self . local_part_pattern + '<STR_LIT:@>' + self . domain_pattern , <EOL> re . IGNORECASE | re . UNICODE ) <EOL> def harvest ( self , text ) : <EOL> """<STR_LIT>""" <EOL> for match in self . harvest_regex . finditer ( text ) : <EOL> yield match . group ( ) . replace ( '<STR_LIT:..>' , '<STR_LIT:.>' ) </s>
