<s> """<STR_LIT>""" <EOL> import urllib2 <EOL> from django . conf import settings <EOL> from django . utils . translation import gettext , gettext_lazy , ngettext <EOL> from django . utils . functional import Promise , lazy <EOL> import re <EOL> _datere = r'<STR_LIT>' <EOL> _timere = r'<STR_LIT>' <EOL> alnum_re = re . compile ( r'<STR_LIT>' ) <EOL> alnumurl_re = re . compile ( r'<STR_LIT>' ) <EOL> ansi_date_re = re . compile ( '<STR_LIT>' % _datere ) <EOL> ansi_time_re = re . compile ( '<STR_LIT>' % _timere ) <EOL> ansi_datetime_re = re . compile ( '<STR_LIT>' % ( _datere , _timere ) ) <EOL> email_re = re . compile ( <EOL> r"<STR_LIT>" <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' , re . IGNORECASE ) <EOL> integer_re = re . compile ( r'<STR_LIT>' ) <EOL> ip4_re = re . compile ( r'<STR_LIT>' ) <EOL> phone_re = re . compile ( r'<STR_LIT>' , re . IGNORECASE ) <EOL> slug_re = re . compile ( r'<STR_LIT>' ) <EOL> url_re = re . compile ( r'<STR_LIT>' ) <EOL> lazy_inter = lazy ( lambda a , b : str ( a ) % b , str ) <EOL> class ValidationError ( Exception ) : <EOL> def __init__ ( self , message ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( message , list ) : <EOL> self . messages = message <EOL> else : <EOL> assert isinstance ( message , ( basestring , Promise ) ) , ( "<STR_LIT>" % repr ( message ) ) <EOL> self . messages = [ message ] <EOL> def __str__ ( self ) : <EOL> return str ( self . messages ) <EOL> class CriticalValidationError ( Exception ) : <EOL> def __init__ ( self , message ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( message , list ) : <EOL> self . messages = message <EOL> else : <EOL> assert isinstance ( message , ( basestring , Promise ) ) , ( "<STR_LIT>" % message ) <EOL> self . messages = [ message ] <EOL> def __str__ ( self ) : <EOL> return str ( self . messages ) <EOL> def isAlphaNumeric ( field_data , all_data ) : <EOL> if not alnum_re . search ( field_data ) : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isAlphaNumericURL ( field_data , all_data ) : <EOL> if not alnumurl_re . search ( field_data ) : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isSlug ( field_data , all_data ) : <EOL> if not slug_re . search ( field_data ) : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isLowerCase ( field_data , all_data ) : <EOL> if field_data . lower ( ) != field_data : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isUpperCase ( field_data , all_data ) : <EOL> if field_data . upper ( ) != field_data : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isCommaSeparatedIntegerList ( field_data , all_data ) : <EOL> for supposed_int in field_data . split ( '<STR_LIT:U+002C>' ) : <EOL> try : <EOL> int ( supposed_int ) <EOL> except ValueError : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isCommaSeparatedEmailList ( field_data , all_data ) : <EOL> """<STR_LIT>""" <EOL> for supposed_email in field_data . split ( '<STR_LIT:U+002C>' ) : <EOL> try : <EOL> isValidEmail ( supposed_email . strip ( ) , '<STR_LIT>' ) <EOL> except ValidationError : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isValidIPAddress4 ( field_data , all_data ) : <EOL> if not ip4_re . search ( field_data ) : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isNotEmpty ( field_data , all_data ) : <EOL> if field_data . strip ( ) == '<STR_LIT>' : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isOnlyDigits ( field_data , all_data ) : <EOL> if not field_data . isdigit ( ) : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isNotOnlyDigits ( field_data , all_data ) : <EOL> if field_data . isdigit ( ) : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isInteger ( field_data , all_data ) : <EOL> if not integer_re . search ( field_data ) : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isOnlyLetters ( field_data , all_data ) : <EOL> if not field_data . isalpha ( ) : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def _isValidDate ( date_string ) : <EOL> """<STR_LIT>""" <EOL> from datetime import date <EOL> year , month , day = map ( int , date_string . split ( '<STR_LIT:->' ) ) <EOL> if year < <NUM_LIT> : <EOL> raise ValidationError , gettext ( '<STR_LIT>' ) <EOL> try : <EOL> date ( year , month , day ) <EOL> except ValueError , e : <EOL> msg = gettext ( '<STR_LIT>' ) % gettext ( str ( e ) ) <EOL> raise ValidationError , msg <EOL> def isValidANSIDate ( field_data , all_data ) : <EOL> if not ansi_date_re . search ( field_data ) : <EOL> raise ValidationError , gettext ( '<STR_LIT>' ) <EOL> _isValidDate ( field_data ) <EOL> def isValidANSITime ( field_data , all_data ) : <EOL> if not ansi_time_re . search ( field_data ) : <EOL> raise ValidationError , gettext ( '<STR_LIT>' ) <EOL> def isValidANSIDatetime ( field_data , all_data ) : <EOL> if not ansi_datetime_re . search ( field_data ) : <EOL> raise ValidationError , gettext ( '<STR_LIT>' ) <EOL> _isValidDate ( field_data . split ( ) [ <NUM_LIT:0> ] ) <EOL> def isValidEmail ( field_data , all_data ) : <EOL> if not email_re . search ( field_data ) : <EOL> raise ValidationError , gettext ( '<STR_LIT>' ) <EOL> def isValidImage ( field_data , all_data ) : <EOL> """<STR_LIT>""" <EOL> from PIL import Image <EOL> from cStringIO import StringIO <EOL> try : <EOL> content = field_data [ '<STR_LIT:content>' ] <EOL> except TypeError : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> try : <EOL> Image . open ( StringIO ( content ) ) <EOL> except IOError : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isValidImageURL ( field_data , all_data ) : <EOL> uc = URLMimeTypeCheck ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> try : <EOL> uc ( field_data , all_data ) <EOL> except URLMimeTypeCheck . InvalidContentType : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) % field_data <EOL> def isValidPhone ( field_data , all_data ) : <EOL> if not phone_re . search ( field_data ) : <EOL> raise ValidationError , gettext ( '<STR_LIT>' ) % field_data <EOL> def isValidQuicktimeVideoURL ( field_data , all_data ) : <EOL> "<STR_LIT>" <EOL> uc = URLMimeTypeCheck ( ( '<STR_LIT>' , '<STR_LIT>' , ) ) <EOL> try : <EOL> uc ( field_data , all_data ) <EOL> except URLMimeTypeCheck . InvalidContentType : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) % field_data <EOL> def isValidURL ( field_data , all_data ) : <EOL> if not url_re . search ( field_data ) : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def isValidHTML ( field_data , all_data ) : <EOL> import urllib , urllib2 <EOL> try : <EOL> u = urllib2 . urlopen ( '<STR_LIT>' , urllib . urlencode ( { '<STR_LIT>' : field_data , '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> except : <EOL> return <EOL> html_is_valid = ( u . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> if html_is_valid : <EOL> return <EOL> from xml . dom . minidom import parseString <EOL> error_messages = [ e . firstChild . wholeText for e in parseString ( u . read ( ) ) . getElementsByTagName ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . getElementsByTagName ( '<STR_LIT>' ) ] <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) % "<STR_LIT:\n>" . join ( error_messages ) <EOL> def isWellFormedXml ( field_data , all_data ) : <EOL> from xml . dom . minidom import parseString <EOL> try : <EOL> parseString ( field_data ) <EOL> except Exception , e : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) % str ( e ) <EOL> def isWellFormedXmlFragment ( field_data , all_data ) : <EOL> isWellFormedXml ( '<STR_LIT>' % field_data , all_data ) <EOL> def isExistingURL ( field_data , all_data ) : <EOL> try : <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>" : settings . URL_VALIDATOR_USER_AGENT <EOL> } <EOL> req = urllib2 . Request ( field_data , None , headers ) <EOL> u = urllib2 . urlopen ( req ) <EOL> except ValueError : <EOL> raise ValidationError , _ ( "<STR_LIT>" ) % field_data <EOL> except urllib2 . HTTPError , e : <EOL> if str ( e . code ) not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise ValidationError , _ ( "<STR_LIT>" ) % field_data <EOL> except : <EOL> raise ValidationError , _ ( "<STR_LIT>" ) % field_data <EOL> def isValidUSState ( field_data , all_data ) : <EOL> "<STR_LIT>" <EOL> states = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:OK>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if field_data . upper ( ) not in states : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> def hasNoProfanities ( field_data , all_data ) : <EOL> """<STR_LIT>""" <EOL> field_data = field_data . lower ( ) <EOL> words_seen = [ w for w in settings . PROFANITIES_LIST if w in field_data ] <EOL> if words_seen : <EOL> from django . utils . text import get_text_list <EOL> plural = len ( words_seen ) > <NUM_LIT:1> <EOL> raise ValidationError , ngettext ( "<STR_LIT>" , <EOL> "<STR_LIT>" , plural ) % get_text_list ( [ '<STR_LIT>' % ( i [ <NUM_LIT:0> ] , '<STR_LIT:->' * ( len ( i ) - <NUM_LIT:2> ) , i [ - <NUM_LIT:1> ] ) for i in words_seen ] , '<STR_LIT>' ) <EOL> class AlwaysMatchesOtherField ( object ) : <EOL> def __init__ ( self , other_field_name , error_message = None ) : <EOL> self . other = other_field_name <EOL> self . error_message = error_message or lazy_inter ( gettext_lazy ( "<STR_LIT>" ) , self . other ) <EOL> self . always_test = True <EOL> def __call__ ( self , field_data , all_data ) : <EOL> if field_data != all_data [ self . other ] : <EOL> raise ValidationError , self . error_message <EOL> class ValidateIfOtherFieldEquals ( object ) : <EOL> def __init__ ( self , other_field , other_value , validator_list ) : <EOL> self . other_field , self . other_value = other_field , other_value <EOL> self . validator_list = validator_list <EOL> self . always_test = True <EOL> def __call__ ( self , field_data , all_data ) : <EOL> if all_data . has_key ( self . other_field ) and all_data [ self . other_field ] == self . other_value : <EOL> for v in self . validator_list : <EOL> v ( field_data , all_data ) <EOL> class RequiredIfOtherFieldNotGiven ( object ) : <EOL> def __init__ ( self , other_field_name , error_message = gettext_lazy ( "<STR_LIT>" ) ) : <EOL> self . other , self . error_message = other_field_name , error_message <EOL> self . always_test = True <EOL> def __call__ ( self , field_data , all_data ) : <EOL> if not all_data . get ( self . other , False ) and not field_data : <EOL> raise ValidationError , self . error_message <EOL> class RequiredIfOtherFieldsGiven ( object ) : <EOL> def __init__ ( self , other_field_names , error_message = gettext_lazy ( "<STR_LIT>" ) ) : <EOL> self . other , self . error_message = other_field_names , error_message <EOL> self . always_test = True <EOL> def __call__ ( self , field_data , all_data ) : <EOL> for field in self . other : <EOL> if all_data . get ( field , False ) and not field_data : <EOL> raise ValidationError , self . error_message <EOL> class RequiredIfOtherFieldGiven ( RequiredIfOtherFieldsGiven ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , other_field_name , error_message = gettext_lazy ( "<STR_LIT>" ) ) : <EOL> RequiredIfOtherFieldsGiven . __init__ ( self , [ other_field_name ] , error_message ) <EOL> class RequiredIfOtherFieldEquals ( object ) : <EOL> def __init__ ( self , other_field , other_value , error_message = None , other_label = None ) : <EOL> self . other_field = other_field <EOL> self . other_value = other_value <EOL> other_label = other_label or other_value <EOL> self . error_message = error_message or lazy_inter ( gettext_lazy ( "<STR_LIT>" ) , { <EOL> '<STR_LIT>' : other_field , '<STR_LIT:value>' : other_label } ) <EOL> self . always_test = True <EOL> def __call__ ( self , field_data , all_data ) : <EOL> if all_data . has_key ( self . other_field ) and all_data [ self . other_field ] == self . other_value and not field_data : <EOL> raise ValidationError ( self . error_message ) <EOL> class RequiredIfOtherFieldDoesNotEqual ( object ) : <EOL> def __init__ ( self , other_field , other_value , other_label = None , error_message = None ) : <EOL> self . other_field = other_field <EOL> self . other_value = other_value <EOL> other_label = other_label or other_value <EOL> self . error_message = error_message or lazy_inter ( gettext_lazy ( "<STR_LIT>" ) , { <EOL> '<STR_LIT>' : other_field , '<STR_LIT:value>' : other_label } ) <EOL> self . always_test = True <EOL> def __call__ ( self , field_data , all_data ) : <EOL> if all_data . has_key ( self . other_field ) and all_data [ self . other_field ] != self . other_value and not field_data : <EOL> raise ValidationError ( self . error_message ) <EOL> class IsLessThanOtherField ( object ) : <EOL> def __init__ ( self , other_field_name , error_message ) : <EOL> self . other , self . error_message = other_field_name , error_message <EOL> def __call__ ( self , field_data , all_data ) : <EOL> if field_data > all_data [ self . other ] : <EOL> raise ValidationError , self . error_message <EOL> class UniqueAmongstFieldsWithPrefix ( object ) : <EOL> def __init__ ( self , field_name , prefix , error_message ) : <EOL> self . field_name , self . prefix = field_name , prefix <EOL> self . error_message = error_message or gettext_lazy ( "<STR_LIT>" ) <EOL> def __call__ ( self , field_data , all_data ) : <EOL> for field_name , value in all_data . items ( ) : <EOL> if field_name != self . field_name and value == field_data : <EOL> raise ValidationError , self . error_message <EOL> class NumberIsInRange ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , lower = None , upper = None , error_message = '<STR_LIT>' ) : <EOL> self . lower , self . upper = lower , upper <EOL> if not error_message : <EOL> if lower and upper : <EOL> self . error_message = gettext ( "<STR_LIT>" ) % { '<STR_LIT>' : lower , '<STR_LIT>' : upper } <EOL> elif lower : <EOL> self . error_message = gettext ( "<STR_LIT>" ) % lower <EOL> elif upper : <EOL> self . error_message = gettext ( "<STR_LIT>" ) % upper <EOL> else : <EOL> self . error_message = error_message <EOL> def __call__ ( self , field_data , all_data ) : <EOL> try : <EOL> val = float ( field_data ) <EOL> except ValueError : <EOL> return <EOL> if self . lower and self . upper and ( val < self . lower or val > self . upper ) : <EOL> raise ValidationError ( self . error_message ) <EOL> elif self . lower and val < self . lower : <EOL> raise ValidationError ( self . error_message ) <EOL> elif self . upper and val > self . upper : <EOL> raise ValidationError ( self . error_message ) <EOL> class IsAPowerOf ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , power_of ) : <EOL> self . power_of = power_of <EOL> def __call__ ( self , field_data , all_data ) : <EOL> from math import log <EOL> val = log ( int ( field_data ) ) / log ( self . power_of ) <EOL> if val != int ( val ) : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) % self . power_of <EOL> class IsValidFloat ( object ) : <EOL> def __init__ ( self , max_digits , decimal_places ) : <EOL> self . max_digits , self . decimal_places = max_digits , decimal_places <EOL> def __call__ ( self , field_data , all_data ) : <EOL> data = str ( field_data ) <EOL> try : <EOL> float ( data ) <EOL> except ValueError : <EOL> raise ValidationError , gettext ( "<STR_LIT>" ) <EOL> max_allowed_length = data . startswith ( '<STR_LIT:->' ) and ( self . max_digits + <NUM_LIT:2> ) or ( self . max_digits + <NUM_LIT:1> ) <EOL> if len ( data ) > max_allowed_length : <EOL> raise ValidationError , ngettext ( "<STR_LIT>" , <EOL> "<STR_LIT>" , self . max_digits ) % self . max_digits <EOL> if ( not '<STR_LIT:.>' in data and len ( data ) > ( max_allowed_length - self . decimal_places - <NUM_LIT:1> ) ) or ( '<STR_LIT:.>' in data and len ( data ) > ( max_allowed_length - ( self . decimal_places - len ( data . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> ] ) ) ) ) : <EOL> raise ValidationError , ngettext ( "<STR_LIT>" , <EOL> "<STR_LIT>" , str ( self . max_digits - self . decimal_places ) ) % str ( self . max_digits - self . decimal_places ) <EOL> if '<STR_LIT:.>' in data and len ( data . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> ] ) > self . decimal_places : <EOL> raise ValidationError , ngettext ( "<STR_LIT>" , <EOL> "<STR_LIT>" , self . decimal_places ) % self . decimal_places <EOL> class HasAllowableSize ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , min_size = None , max_size = None , min_error_message = None , max_error_message = None ) : <EOL> self . min_size , self . max_size = min_size , max_size <EOL> self . min_error_message = min_error_message or lazy_inter ( gettext_lazy ( "<STR_LIT>" ) , min_size ) <EOL> self . max_error_message = max_error_message or lazy_inter ( gettext_lazy ( "<STR_LIT>" ) , max_size ) <EOL> def __call__ ( self , field_data , all_data ) : <EOL> try : <EOL> content = field_data [ '<STR_LIT:content>' ] <EOL> except TypeError : <EOL> raise ValidationError , gettext_lazy ( "<STR_LIT>" ) <EOL> if self . min_size is not None and len ( content ) < self . min_size : <EOL> raise ValidationError , self . min_error_message <EOL> if self . max_size is not None and len ( content ) > self . max_size : <EOL> raise ValidationError , self . max_error_message <EOL> class MatchesRegularExpression ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , regexp , error_message = gettext_lazy ( "<STR_LIT>" ) ) : <EOL> self . regexp = re . compile ( regexp ) <EOL> self . error_message = error_message <EOL> def __call__ ( self , field_data , all_data ) : <EOL> if not self . regexp . search ( field_data ) : <EOL> raise ValidationError ( self . error_message ) <EOL> class AnyValidator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , validator_list = None , error_message = gettext_lazy ( "<STR_LIT>" ) ) : <EOL> if validator_list is None : validator_list = [ ] <EOL> self . validator_list = validator_list <EOL> self . error_message = error_message <EOL> for v in validator_list : <EOL> if hasattr ( v , '<STR_LIT>' ) : <EOL> self . always_test = True <EOL> def __call__ ( self , field_data , all_data ) : <EOL> for v in self . validator_list : <EOL> try : <EOL> v ( field_data , all_data ) <EOL> return <EOL> except ValidationError , e : <EOL> pass <EOL> raise ValidationError ( self . error_message ) <EOL> class URLMimeTypeCheck ( object ) : <EOL> "<STR_LIT>" <EOL> class CouldNotRetrieve ( ValidationError ) : <EOL> pass <EOL> class InvalidContentType ( ValidationError ) : <EOL> pass <EOL> def __init__ ( self , mime_type_list ) : <EOL> self . mime_type_list = mime_type_list <EOL> def __call__ ( self , field_data , all_data ) : <EOL> import urllib2 <EOL> try : <EOL> isValidURL ( field_data , all_data ) <EOL> except ValidationError : <EOL> raise <EOL> try : <EOL> info = urllib2 . urlopen ( field_data ) . info ( ) <EOL> except ( urllib2 . HTTPError , urllib2 . URLError ) : <EOL> raise URLMimeTypeCheck . CouldNotRetrieve , gettext ( "<STR_LIT>" ) % field_data <EOL> content_type = info [ '<STR_LIT>' ] <EOL> if content_type not in self . mime_type_list : <EOL> raise URLMimeTypeCheck . InvalidContentType , gettext ( "<STR_LIT>" ) % { <EOL> '<STR_LIT:url>' : field_data , '<STR_LIT>' : content_type } <EOL> class RelaxNGCompact ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , schema_path , additional_root_element = None ) : <EOL> self . schema_path = schema_path <EOL> self . additional_root_element = additional_root_element <EOL> def __call__ ( self , field_data , all_data ) : <EOL> import os , tempfile <EOL> if self . additional_root_element : <EOL> field_data = '<STR_LIT>' % { <EOL> '<STR_LIT>' : self . additional_root_element , <EOL> '<STR_LIT:data>' : field_data <EOL> } <EOL> filename = tempfile . mktemp ( ) <EOL> fp = open ( filename , '<STR_LIT:w>' ) <EOL> fp . write ( field_data ) <EOL> fp . close ( ) <EOL> if not os . path . exists ( settings . JING_PATH ) : <EOL> raise Exception , "<STR_LIT>" % settings . JING_PATH <EOL> p = os . popen ( '<STR_LIT>' % ( settings . JING_PATH , self . schema_path , filename ) ) <EOL> errors = [ line . strip ( ) for line in p . readlines ( ) ] <EOL> p . close ( ) <EOL> os . unlink ( filename ) <EOL> display_errors = [ ] <EOL> lines = field_data . split ( '<STR_LIT:\n>' ) <EOL> for error in errors : <EOL> ignored , line , level , message = error . split ( '<STR_LIT::>' , <NUM_LIT:3> ) <EOL> m = re . search ( r'<STR_LIT>' , message ) <EOL> if m : <EOL> display_errors . append ( _ ( '<STR_LIT>' ) % { '<STR_LIT>' : m . group ( <NUM_LIT:1> ) . replace ( '<STR_LIT:/>' , '<STR_LIT>' ) , '<STR_LIT>' : m . group ( <NUM_LIT:2> ) , '<STR_LIT:start>' : lines [ int ( m . group ( <NUM_LIT:2> ) ) - <NUM_LIT:1> ] [ : <NUM_LIT:30> ] } ) <EOL> continue <EOL> if message . strip ( ) == '<STR_LIT>' : <EOL> display_errors . append ( _ ( '<STR_LIT>' ) % { '<STR_LIT>' : line , '<STR_LIT:start>' : lines [ int ( line ) - <NUM_LIT:1> ] [ : <NUM_LIT:30> ] } ) <EOL> continue <EOL> m = re . search ( r'<STR_LIT>' , message ) <EOL> if m : <EOL> display_errors . append ( _ ( '<STR_LIT>' ) % { '<STR_LIT>' : m . group ( <NUM_LIT:1> ) , '<STR_LIT>' : line , '<STR_LIT:start>' : lines [ int ( line ) - <NUM_LIT:1> ] [ : <NUM_LIT:30> ] } ) <EOL> continue <EOL> m = re . search ( r'<STR_LIT>' , message ) <EOL> if m : <EOL> display_errors . append ( _ ( '<STR_LIT>' ) % { '<STR_LIT>' : m . group ( <NUM_LIT:1> ) , '<STR_LIT>' : line , '<STR_LIT:start>' : lines [ int ( line ) - <NUM_LIT:1> ] [ : <NUM_LIT:30> ] } ) <EOL> continue <EOL> if message . strip ( ) == '<STR_LIT>' : <EOL> display_errors . append ( _ ( '<STR_LIT>' ) % { '<STR_LIT>' : line , '<STR_LIT:start>' : lines [ int ( line ) - <NUM_LIT:1> ] [ : <NUM_LIT:30> ] } ) <EOL> continue <EOL> m = re . search ( r'<STR_LIT>' , message ) <EOL> if m : <EOL> display_errors . append ( _ ( '<STR_LIT>' ) % { '<STR_LIT>' : m . group ( <NUM_LIT:1> ) , '<STR_LIT>' : line , '<STR_LIT:start>' : lines [ int ( line ) - <NUM_LIT:1> ] [ : <NUM_LIT:30> ] } ) <EOL> continue <EOL> display_error = '<STR_LIT>' % ( line , message , level . strip ( ) ) <EOL> display_errors . append ( display_error ) <EOL> if len ( display_errors ) > <NUM_LIT:0> : <EOL> raise ValidationError , display_errors </s>
