<s> """<STR_LIT>""" <EOL> import re <EOL> import xml . dom . minidom <EOL> import xml . parsers . expat <EOL> from xml . sax . saxutils import escape as xml_escape <EOL> from django . utils . translation import ugettext , ugettext_lazy as _ <EOL> from django . db . models import get_model <EOL> from transifex . txcommon . log import logger <EOL> from transifex . txcommon . exceptions import FileCheckError <EOL> from transifex . resources . formats . core import Handler , ParseError , CompileError , STRICT <EOL> from transifex . resources . formats . resource_collections import StringSet , GenericTranslation <EOL> from transifex . resources . formats . utils . decorators import * <EOL> from transifex . resources . formats . utils . hash_tag import hash_tag , escape_context <EOL> Resource = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Translation = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> SourceEntity = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Template = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Storage = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class XliffParseError ( ParseError ) : <EOL> pass <EOL> class XliffCompileError ( CompileError ) : <EOL> pass <EOL> class XliffHandler ( Handler ) : <EOL> name = "<STR_LIT>" <EOL> format = "<STR_LIT>" <EOL> method_name = '<STR_LIT>' <EOL> format_encoding = '<STR_LIT>' <EOL> HandlerParseError = XliffParseError <EOL> HandlerCompileError = XliffCompileError <EOL> def _examine_content ( self , content ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( content , unicode ) : <EOL> content = content . encode ( '<STR_LIT:utf-8>' ) <EOL> doc = xml . dom . minidom . parseString ( content ) <EOL> root = doc . documentElement <EOL> rules = self . language . get_pluralrules_numbers ( ) <EOL> plurals = SourceEntity . objects . filter ( resource = self . resource , pluralized = True ) <EOL> if self . language != self . resource . source_language and plurals : <EOL> for entity in plurals : <EOL> match = False <EOL> for group_node in root . getElementsByTagName ( "<STR_LIT>" ) : <EOL> if group_node . attributes [ '<STR_LIT>' ] . value == "<STR_LIT>" : <EOL> trans_unit_nodes = group_node . getElementsByTagName ( "<STR_LIT>" ) <EOL> if self . getElementByTagName ( trans_unit_nodes [ <NUM_LIT:0> ] , "<STR_LIT:source>" ) . firstChild . data == entity . string_hash : <EOL> match = True <EOL> break <EOL> if not match : <EOL> continue <EOL> for count , rule in enumerate ( rules ) : <EOL> if rule == <NUM_LIT:0> : <EOL> clone = trans_unit_nodes [ <NUM_LIT:1> ] . cloneNode ( deep = True ) <EOL> target = self . getElementByTagName ( clone , "<STR_LIT:target>" ) <EOL> target . firstChild . data = target . firstChild . data [ : - <NUM_LIT:1> ] + '<STR_LIT:0>' <EOL> clone . setAttribute ( "<STR_LIT:id>" , group_node . attributes [ "<STR_LIT:id>" ] . value + '<STR_LIT>' % count ) <EOL> indent_node = trans_unit_nodes [ <NUM_LIT:0> ] . previousSibling . cloneNode ( deep = True ) <EOL> group_node . insertBefore ( indent_node , trans_unit_nodes [ <NUM_LIT:0> ] . previousSibling ) <EOL> group_node . insertBefore ( clone , trans_unit_nodes [ <NUM_LIT:0> ] . previousSibling ) <EOL> if rule == <NUM_LIT:1> : <EOL> trans_unit_nodes [ <NUM_LIT:0> ] . setAttribute ( "<STR_LIT:id>" , group_node . attributes [ "<STR_LIT:id>" ] . value + '<STR_LIT>' % count ) <EOL> if rule in range ( <NUM_LIT:2> , <NUM_LIT:5> ) : <EOL> clone = trans_unit_nodes [ <NUM_LIT:1> ] . cloneNode ( deep = True ) <EOL> target = self . getElementByTagName ( clone , "<STR_LIT:target>" ) <EOL> target . firstChild . data = target . firstChild . data [ : - <NUM_LIT:1> ] + '<STR_LIT>' % rule <EOL> clone . setAttribute ( "<STR_LIT:id>" , group_node . attributes [ "<STR_LIT:id>" ] . value + '<STR_LIT>' % count ) <EOL> indent_node = trans_unit_nodes [ <NUM_LIT:1> ] . previousSibling . cloneNode ( deep = True ) <EOL> group_node . insertBefore ( indent_node , trans_unit_nodes [ <NUM_LIT:1> ] . previousSibling ) <EOL> group_node . insertBefore ( clone , trans_unit_nodes [ <NUM_LIT:1> ] . previousSibling ) <EOL> if rule == <NUM_LIT:5> : <EOL> trans_unit_nodes [ <NUM_LIT:1> ] . setAttribute ( "<STR_LIT:id>" , group_node . attributes [ "<STR_LIT:id>" ] . value + '<STR_LIT>' % count ) <EOL> content = doc . toxml ( ) <EOL> return content <EOL> def _get_translation_strings ( self , source_entities , language ) : <EOL> """<STR_LIT>""" <EOL> res = { } <EOL> translations = Translation . objects . filter ( <EOL> source_entity__in = source_entities , language = language <EOL> ) . values_list ( '<STR_LIT>' , '<STR_LIT:string>' , '<STR_LIT>' ) . iterator ( ) <EOL> for t in translations : <EOL> if res . has_key ( t [ <NUM_LIT:0> ] ) : <EOL> if type ( res [ t [ <NUM_LIT:0> ] ] ) == type ( [ ] ) : <EOL> res [ t [ <NUM_LIT:0> ] ] . append ( t [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> res [ t [ <NUM_LIT:0> ] ] = [ res [ t [ <NUM_LIT:0> ] ] ] <EOL> res [ t [ <NUM_LIT:0> ] ] . append ( t [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> res [ t [ <NUM_LIT:0> ] ] = t [ <NUM_LIT:1> : ] <EOL> return res <EOL> def _post_compile ( self , * args , ** kwargs ) : <EOL> doc = xml . dom . minidom . parseString ( self . compiled_template ) <EOL> root = doc . documentElement <EOL> for node in root . getElementsByTagName ( "<STR_LIT:target>" ) : <EOL> value = "<STR_LIT>" <EOL> for child in node . childNodes : <EOL> value += child . toxml ( ) <EOL> if not value . strip ( ) or self . language == self . resource . source_language : <EOL> parent = node . parentNode <EOL> parent . removeChild ( node . previousSibling ) <EOL> parent . removeChild ( node ) <EOL> self . compiled_template = doc . toxml ( ) <EOL> def _compile ( self , content , language ) : <EOL> stringset = self . _get_source_strings ( self . resource ) <EOL> translations = self . _get_translation_strings ( <EOL> ( s [ <NUM_LIT:0> ] for s in stringset ) , language <EOL> ) <EOL> for string in stringset : <EOL> trans = translations . get ( string [ <NUM_LIT:0> ] , u"<STR_LIT>" ) <EOL> if SourceEntity . objects . get ( id__exact = string [ <NUM_LIT:0> ] ) . pluralized : <EOL> if type ( trans ) == type ( [ ] ) : <EOL> plural_trans = trans <EOL> else : <EOL> plural_trans = [ ] <EOL> for i in self . language . get_pluralrules_numbers ( ) : <EOL> plural_trans . append ( ( u"<STR_LIT>" , i ) ) <EOL> for i in plural_trans : <EOL> rule = str ( i [ <NUM_LIT:1> ] ) <EOL> trans = i [ <NUM_LIT:0> ] <EOL> if SourceEntity . objects . get ( id__exact = string [ <NUM_LIT:0> ] ) . pluralized : <EOL> content = self . _replace_translation ( <EOL> "<STR_LIT>" % ( string [ <NUM_LIT:1> ] . encode ( '<STR_LIT:utf-8>' ) , rule ) , <EOL> trans or "<STR_LIT>" , <EOL> content ) <EOL> else : <EOL> if trans : <EOL> trans = trans [ <NUM_LIT:0> ] <EOL> content = self . _replace_translation ( <EOL> "<STR_LIT>" % string [ <NUM_LIT:1> ] . encode ( '<STR_LIT:utf-8>' ) , <EOL> trans or "<STR_LIT>" , <EOL> content <EOL> ) <EOL> return content <EOL> def _getText ( self , nodelist ) : <EOL> rc = [ ] <EOL> for node in nodelist : <EOL> if hasattr ( node , '<STR_LIT:data>' ) : <EOL> rc . append ( node . data ) <EOL> else : <EOL> rc . append ( node . toxml ( ) ) <EOL> return '<STR_LIT>' . join ( rc ) <EOL> def getElementByTagName ( self , element , tagName , noneAllowed = False ) : <EOL> elements = element . getElementsByTagName ( tagName ) <EOL> if not noneAllowed and not elements : <EOL> raise self . HandlerParseError ( _ ( "<STR_LIT>" % tagName ) ) <EOL> if len ( elements ) > <NUM_LIT:1> : <EOL> raise self . HandlerParseError ( _ ( "<STR_LIT>" % tagName ) ) <EOL> return elements [ <NUM_LIT:0> ] <EOL> def _parse ( self , is_source , lang_rules ) : <EOL> """<STR_LIT>""" <EOL> resource = self . resource <EOL> context = "<STR_LIT>" <EOL> content = self . content . encode ( '<STR_LIT:utf-8>' ) <EOL> try : <EOL> self . doc = xml . dom . minidom . parseString ( content ) <EOL> root = self . doc . documentElement <EOL> if root . tagName != "<STR_LIT>" : <EOL> raise XliffParseError ( _ ( "<STR_LIT>" ) ) <EOL> for node in root . childNodes : <EOL> if node . nodeType == node . ELEMENT_NODE and node . localName == "<STR_LIT:file>" : <EOL> self . parse_tag_file ( node , is_source ) <EOL> except Exception , e : <EOL> XliffParseError ( e . message ) <EOL> return self . doc . toxml ( ) <EOL> def parse_tag_file ( self , file_node , is_source = False ) : <EOL> for node in file_node . childNodes : <EOL> if node . nodeType == node . ELEMENT_NODE and node . localName == "<STR_LIT:body>" : <EOL> self . parse_tag_body ( node , is_source ) <EOL> def parse_tag_body ( self , body_node , is_source = False ) : <EOL> for node in body_node . childNodes : <EOL> if node . nodeType == node . ELEMENT_NODE and node . localName == "<STR_LIT>" : <EOL> self . parse_tag_group ( node , is_source ) <EOL> if node . nodeType == node . ELEMENT_NODE and node . localName == "<STR_LIT>" : <EOL> self . parse_tag_trans_unit ( node , is_source , context = [ ] ) <EOL> def parse_tag_group ( self , group_node , is_source = False , context = None ) : <EOL> if not context : <EOL> context = [ ] <EOL> if group_node . attributes [ '<STR_LIT>' ] . value == "<STR_LIT>" : <EOL> pluralized = True <EOL> nplural_file = <NUM_LIT:0> <EOL> nplural = self . language . get_pluralrules_numbers ( ) <EOL> trans_unit_nodes = [ ] <EOL> for node in group_node . childNodes : <EOL> if node . nodeType == node . ELEMENT_NODE and node . localName == "<STR_LIT>" : <EOL> nplural_file += <NUM_LIT:1> <EOL> trans_unit_nodes . append ( node ) <EOL> if node . nodeType == node . ELEMENT_NODE and node . localName == "<STR_LIT>" : <EOL> context . extend ( self . parse_tag_context_group ( node , is_source ) ) <EOL> source = "<STR_LIT>" <EOL> source_node = trans_unit_nodes [ nplural . index ( <NUM_LIT:1> ) ] . getElementsByTagName ( "<STR_LIT:source>" ) [ <NUM_LIT:0> ] <EOL> if len ( source_node . childNodes ) > <NUM_LIT:1> : <EOL> source = self . _getText ( source_node . childNodes ) <EOL> else : <EOL> source = source_node . firstChild . data <EOL> if is_source : <EOL> if nplural_file != <NUM_LIT:2> : <EOL> raise self . HandlerParseError ( _ ( "<STR_LIT>" ) ) <EOL> for n , node in enumerate ( trans_unit_nodes ) : <EOL> if n == <NUM_LIT:0> : <EOL> rule = <NUM_LIT:1> <EOL> else : <EOL> rule = <NUM_LIT:5> <EOL> self . parse_tag_trans_unit ( node , is_source , [ i for i in context ] , source_string = source , rule = rule ) <EOL> else : <EOL> if nplural_file != len ( nplural ) : <EOL> raise self . HandlerParseError ( _ ( "<STR_LIT>" ) ) <EOL> for n , node in enumerate ( trans_unit_nodes ) : <EOL> self . parse_tag_trans_unit ( node , is_source , [ i for i in context ] , source_string = source , rule = nplural [ n ] ) <EOL> return <EOL> for node in group_node . childNodes : <EOL> if node . nodeType == node . ELEMENT_NODE and node . localName == "<STR_LIT>" : <EOL> self . parse_tag_group ( node , is_source , [ i for i in context ] ) <EOL> if node . nodeType == node . ELEMENT_NODE and node . localName == "<STR_LIT>" : <EOL> self . parse_tag_trans_unit ( node , is_source , [ i for i in context ] ) <EOL> if node . nodeType == node . ELEMENT_NODE and node . localName == "<STR_LIT>" : <EOL> context . extend ( self . parse_tag_context_group ( node , is_source ) ) <EOL> def parse_tag_trans_unit ( self , trans_unit_node , is_source = False , context = [ ] , source_string = None , rule = None ) : <EOL> source = "<STR_LIT>" <EOL> source_node = trans_unit_node . getElementsByTagName ( "<STR_LIT:source>" ) [ <NUM_LIT:0> ] <EOL> if len ( source_node . childNodes ) > <NUM_LIT:1> : <EOL> for i in source_node . childNodes : <EOL> source += i . toxml ( ) <EOL> else : <EOL> source = source_node . firstChild . data <EOL> if source_string : <EOL> pluralized = True <EOL> else : <EOL> pluralized = False <EOL> for node in trans_unit_node . childNodes : <EOL> if node . nodeType == node . ELEMENT_NODE and node . localName == "<STR_LIT>" and not source_string and not rule : <EOL> context . extend ( self . parse_tag_context_group ( node , is_source ) ) <EOL> context = escape_context ( context ) <EOL> if is_source : <EOL> translation = source <EOL> if pluralized : <EOL> source = source_string <EOL> target = self . doc . createElement ( "<STR_LIT:target>" ) <EOL> target . childNodes = [ ] <EOL> if source_string and rule : <EOL> target . appendChild ( self . doc . createTextNode ( <EOL> ( "<STR_LIT>" % { '<STR_LIT>' : hash_tag ( <EOL> source_string , context ) , '<STR_LIT>' : rule } ) <EOL> ) ) <EOL> else : <EOL> target . appendChild ( self . doc . createTextNode ( <EOL> ( "<STR_LIT>" % { '<STR_LIT>' : hash_tag ( <EOL> source , context ) } ) <EOL> ) ) <EOL> if translation and not translation . strip ( ) : <EOL> return <EOL> indent_node = source_node . previousSibling . cloneNode ( True ) <EOL> if source_node . nextSibling : <EOL> trans_unit_node . insertBefore ( target , source_node . nextSibling ) <EOL> trans_unit_node . insertBefore ( indent_node , source_node . nextSibling ) <EOL> else : <EOL> trans_unit_node . appendChild ( indent_node ) <EOL> trans_unit_node . appendChild ( target ) <EOL> else : <EOL> if pluralized : <EOL> source = source_string <EOL> target_list = trans_unit_node . getElementsByTagName ( "<STR_LIT:target>" ) <EOL> if target_list : <EOL> if len ( target_list [ <NUM_LIT:0> ] . childNodes ) > <NUM_LIT:1> : <EOL> translation = self . _getText ( target_list [ <NUM_LIT:0> ] . childNodes ) <EOL> else : <EOL> if target_list [ <NUM_LIT:0> ] . firstChild : <EOL> translation = target_list [ <NUM_LIT:0> ] . firstChild . data <EOL> else : <EOL> translation = u"<STR_LIT>" <EOL> else : <EOL> translation = u"<STR_LIT>" <EOL> if not translation : <EOL> return <EOL> if pluralized : <EOL> self . _add_translation_string ( <EOL> source , translation , context = context , <EOL> rule = rule , pluralized = True <EOL> ) <EOL> """<STR_LIT>""" <EOL> else : <EOL> self . _add_translation_string ( <EOL> source , translation , context = context <EOL> ) <EOL> """<STR_LIT>""" <EOL> def parse_tag_context_group ( self , context_group_node , is_source = False ) : <EOL> result = [ ] <EOL> for node in context_group_node . childNodes : <EOL> if node . nodeType == node . ELEMENT_NODE and node . localName == "<STR_LIT>" : <EOL> result . append ( self . parse_tag_context ( node , is_source ) ) <EOL> return result <EOL> def parse_tag_context ( self , context_node , is_source = False ) : <EOL> content = self . _getText ( context_node . childNodes ) <EOL> context_type = context_node . attributes [ '<STR_LIT>' ] . value <EOL> return "<STR_LIT>" % ( context_type , content . replace ( "<STR_LIT:\n>" , "<STR_LIT:U+0020>" ) ) </s>
