<s> """<STR_LIT>""" <EOL> import logging <EOL> from xml . etree . ElementTree import Element , fromstring , ParseError <EOL> from django . core . exceptions import ValidationError <EOL> from appraise . settings import LOG_LEVEL , LOG_HANDLER <EOL> logging . basicConfig ( level = LOG_LEVEL ) <EOL> LOGGER = logging . getLogger ( '<STR_LIT>' ) <EOL> LOGGER . addHandler ( LOG_HANDLER ) <EOL> HIT_REQUIRED_ATTRIBUTES = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> def validate_hits_xml_file ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isinstance ( value , Element ) : <EOL> _tree = value <EOL> else : <EOL> _tree = fromstring ( value . encode ( "<STR_LIT:utf-8>" ) ) <EOL> assert ( _tree . tag == '<STR_LIT>' ) , '<STR_LIT>' <EOL> for _child in _tree : <EOL> validate_hit_xml ( _child ) <EOL> except ( AssertionError , ParseError ) , msg : <EOL> raise ValidationError ( '<STR_LIT>' . format ( msg ) ) <EOL> return value <EOL> def validate_hit_xml ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isinstance ( value , Element ) : <EOL> _tree = value <EOL> else : <EOL> _tree = fromstring ( value . encode ( "<STR_LIT:utf-8>" ) ) <EOL> assert ( _tree . tag == '<STR_LIT>' ) , '<STR_LIT>' <EOL> systems_available = '<STR_LIT>' in _tree . attrib . keys ( ) <EOL> for _attr in HIT_REQUIRED_ATTRIBUTES : <EOL> assert ( _attr in _tree . attrib . keys ( ) ) , '<STR_LIT>' . format ( _attr ) <EOL> try : <EOL> _block_id = _tree . attrib [ '<STR_LIT>' ] <EOL> _block_id = int ( _block_id ) <EOL> except ValueError , msg : <EOL> raise ValidationError ( '<STR_LIT>' . format ( <EOL> _block_id , msg ) ) <EOL> _no_of_children = <NUM_LIT:0> <EOL> for _seg in _tree : <EOL> validate_segment_xml ( _seg , require_systems = not systems_available ) <EOL> _no_of_children += <NUM_LIT:1> <EOL> assert ( _no_of_children == <NUM_LIT:3> ) , '<STR_LIT>' <EOL> except ( AssertionError , ParseError ) , msg : <EOL> raise ValidationError ( '<STR_LIT>' . format ( msg ) ) <EOL> return value <EOL> def validate_segment_xml ( value , require_systems = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isinstance ( value , Element ) : <EOL> _tree = value <EOL> else : <EOL> _tree = fromstring ( value ) <EOL> if not _tree . tag == '<STR_LIT>' : <EOL> raise ValidationError ( '<STR_LIT>' . format ( <EOL> _tree . tag ) ) <EOL> assert ( len ( _tree . findall ( '<STR_LIT:source>' ) ) == <NUM_LIT:1> ) , '<STR_LIT>' <EOL> assert ( _tree . find ( '<STR_LIT:source>' ) . text is not None ) , '<STR_LIT>' <EOL> assert ( len ( _tree . findall ( '<STR_LIT>' ) ) == <NUM_LIT:1> ) , '<STR_LIT>' <EOL> assert ( _tree . find ( '<STR_LIT>' ) . text is not None ) , '<STR_LIT>' <EOL> assert ( len ( _tree . findall ( '<STR_LIT>' ) ) > <NUM_LIT:0> ) , '<STR_LIT>' <EOL> for _translation in _tree . iterfind ( '<STR_LIT>' ) : <EOL> assert ( _translation . text is not None ) , '<STR_LIT>' <EOL> if require_systems : <EOL> assert ( '<STR_LIT>' in _translation . attrib . keys ( ) ) , '<STR_LIT>' <EOL> except ( AssertionError , ParseError ) , msg : <EOL> raise ValidationError ( '<STR_LIT>' . format ( msg ) ) </s>
