def test_xml_to_strelem(): source = etree.fromstring(u'<source>a<x id="foo[1]/bar[1]/baz[1]"/></source>') elem = lisa.xml_to_strelem(source) assert elem.sub == [ StringElem(u'a'), X(id=u'foo[1]/bar[1]/baz[1]') ] source = etree.fromstring(u'<source>a<x id="foo[1]/bar[1]/baz[1]"/>é</source>') elem = lisa.xml_to_strelem(source) assert elem.sub == [ StringElem(u'a'), X(id=u'foo[1]/bar[1]/baz[1]'), StringElem(u'é') ] source = etree.fromstring(u'<source>a<g id="foo[2]/bar[2]/baz[2]">b<x id="foo[1]/bar[1]/baz[1]"/>c</g>é</source>') elem = lisa.xml_to_strelem(source) assert elem.sub == [ StringElem(u'a'), G(id=u'foo[2]/bar[2]/baz[2]', sub=[StringElem(u'b'), X(id=u'foo[1]/bar[1]/baz[1]'), StringElem(u'c')]), StringElem(u'é') ]
def get_rich_target(self, lang=None): """retrieves the "target" text (second entry), or the entry in the specified language, if it exists""" if self._rich_target is None: self._rich_target = [ xml_to_strelem(self.get_target_dom(lang), getXMLspace(self.xmlelement, self._default_xml_space)) ] return self._rich_target
def get_rich_source(self): #rsrc = xml_to_strelem(self.source_dom) #logging.debug('rich source: %s' % (repr(rsrc))) #from dubulib.debug.misc import print_stack_funcs #print_stack_funcs() return [ xml_to_strelem(self.source_dom, getXMLspace(self.xmlelement, self._default_xml_space)) ]
def multistring_to_rich(cls, mstr): """Override L{TranslationUnit.multistring_to_rich} which is used by the C{rich_source} and C{rich_target} properties.""" strings = mstr if isinstance(mstr, multistring): strings = mstr.strings elif isinstance(mstr, basestring): strings = [mstr] return [xml_to_strelem(s) for s in strings]
def get_rich_target(self, lang=None): """retrieves the "target" text (second entry), or the entry in the specified language, if it exists""" if self._rich_target is None: self._rich_target = [ xml_to_strelem( self.get_target_dom(lang), getXMLspace(self.xmlelement, self._default_xml_space)) ] return self._rich_target
def get_rich_source(self): #rsrc = xml_to_strelem(self.source_dom) #logging.debug('rich source: %s' % (repr(rsrc))) #from dubulib.debug.misc import print_stack_funcs #print_stack_funcs() return [ xml_to_strelem( self.source_dom, getXMLspace(self.xmlelement, self._default_xml_space)) ]
def multistring_to_rich(cls, mstr): """Override :meth:`TranslationUnit.multistring_to_rich` which is used by the ``rich_source`` and ``rich_target`` properties.""" strings = mstr if isinstance(mstr, multistring): strings = mstr.strings elif isinstance(mstr, basestring): strings = [mstr] return [xml_to_strelem(s) for s in strings]
def test_xml_to_strelem(): source = etree.fromstring(u"<source>a</source>") elem = lisa.xml_to_strelem(source) assert elem == StringElem(u"a") source = etree.fromstring(u'<source>a<x id="foo[1]/bar[1]/baz[1]"/></source>') elem = lisa.xml_to_strelem(source) assert elem.sub == [StringElem(u"a"), X(id=u"foo[1]/bar[1]/baz[1]")] source = etree.fromstring(u'<source>a<x id="foo[1]/bar[1]/baz[1]"/>é</source>') elem = lisa.xml_to_strelem(source) assert elem.sub == [StringElem(u"a"), X(id=u"foo[1]/bar[1]/baz[1]"), StringElem(u"é")] source = etree.fromstring(u'<source>a<g id="foo[2]/bar[2]/baz[2]">b<x id="foo[1]/bar[1]/baz[1]"/>c</g>é</source>') elem = lisa.xml_to_strelem(source) assert elem.sub == [ StringElem(u"a"), G(id=u"foo[2]/bar[2]/baz[2]", sub=[StringElem(u"b"), X(id=u"foo[1]/bar[1]/baz[1]"), StringElem(u"c")]), StringElem(u"é"), ]
def test_xml_space(): source = etree.fromstring( '<source xml:space="default"> a <x id="foo[1]/bar[1]/baz[1]"/> </source>' ) elem = lisa.xml_to_strelem(source) print(elem.sub) assert elem.sub == [ StringElem('a '), X(id='foo[1]/bar[1]/baz[1]'), StringElem(' ') ]
def test_unknown_xml_placeable(): # The XML below is (modified) from the official XLIFF example file Sample_AlmostEverything_1.2_strict.xlf source = etree.fromstring( """<source xml:lang="en-us">Text <g id="_1_ski_040">g</g>TEXT<bpt id="_1_ski_139">bpt<sub>sub</sub> </bpt>TEXT<ept id="_1_ski_238">ept</ept>TEXT<ph id="_1_ski_337"/>TEXT<it id="_1_ski_436" pos="open">it</it>TEXT<mrk mtype="x-test">mrk</mrk> <x id="_1_ski_535"/>TEXT<bx id="_1_ski_634"/>TEXT<ex id="_1_ski_733"/>TEXT.</source>""" ) elem = lisa.xml_to_strelem(source) from copy import copy custom = StringElem([ StringElem("Text "), G("g", id="_1_ski_040"), StringElem("TEXT"), UnknownXML( [ StringElem("bpt"), UnknownXML("sub", xml_node=copy(source[1][0])), StringElem("\n "), ], id="_1_ski_139", xml_node=copy(source[3]), ), StringElem("TEXT"), UnknownXML("ept", id="_1_ski_238", xml_node=copy(source[2])), StringElem("TEXT"), UnknownXML(id="_1_ski_337", xml_node=copy(source[3])), # ph-tag StringElem("TEXT"), UnknownXML("it", id="_1_ski_436", xml_node=copy(source[4])), StringElem("TEXT"), UnknownXML("mrk", xml_node=copy(source[5])), StringElem("\n "), X(id="_1_ski_535"), StringElem("TEXT"), Bx(id="_1_ski_634"), StringElem("TEXT"), Ex(id="_1_ski_733"), StringElem("TEXT."), ]) assert elem == custom xml = copy(source) for i in range(len(xml)): del xml[0] xml.text = None xml.tail = None lisa.strelem_to_xml(xml, elem) assert etree.tostring(xml) == etree.tostring(source)
def test_unknown_xml_placeable(): # The XML below is (modified) from the official XLIFF example file Sample_AlmostEverything_1.2_strict.xlf source = etree.fromstring( u"""<source xml:lang="en-us">Text <g id="_1_ski_040">g</g>TEXT<bpt id="_1_ski_139">bpt<sub>sub</sub> </bpt>TEXT<ept id="_1_ski_238">ept</ept>TEXT<ph id="_1_ski_337"/>TEXT<it id="_1_ski_436" pos="open">it</it>TEXT<mrk mtype="x-test">mrk</mrk> <x id="_1_ski_535"/>TEXT<bx id="_1_ski_634"/>TEXT<ex id="_1_ski_733"/>TEXT.</source>""" ) elem = lisa.xml_to_strelem(source) from copy import copy custom = StringElem( [ StringElem(u"Text "), G(u"g", id="_1_ski_040"), StringElem(u"TEXT"), UnknownXML( [StringElem(u"bpt"), UnknownXML(u"sub", xml_node=copy(source[1][0])), StringElem(u"\n ")], id="_1_ski_139", xml_node=copy(source[3]), ), StringElem(u"TEXT"), UnknownXML(u"ept", id=u"_1_ski_238", xml_node=copy(source[2])), StringElem(u"TEXT"), UnknownXML(id="_1_ski_337", xml_node=copy(source[3])), # ph-tag StringElem(u"TEXT"), UnknownXML(u"it", id="_1_ski_436", xml_node=copy(source[4])), StringElem(u"TEXT"), UnknownXML(u"mrk", xml_node=copy(source[5])), StringElem(u"\n "), X(id="_1_ski_535"), StringElem(u"TEXT"), Bx(id="_1_ski_634"), StringElem(u"TEXT"), Ex(id="_1_ski_733"), StringElem(u"TEXT."), ] ) assert elem == custom xml = copy(source) for i in range(len(xml)): del xml[0] xml.text = None xml.tail = None lisa.strelem_to_xml(xml, elem) assert etree.tostring(xml) == etree.tostring(source)
def test_xml_space(): source = etree.fromstring(u'<source xml:space="default"> a <x id="foo[1]/bar[1]/baz[1]"/> </source>') elem = lisa.xml_to_strelem(source) print elem.sub assert elem.sub == [StringElem(u'a '), X(id=u'foo[1]/bar[1]/baz[1]'), StringElem(u' ')]