Package translate :: Package storage :: Package xml_extract :: Module unit_tree
[hide private]
[frames] | no frames]

Module unit_tree

source code

Classes [hide private]
  XPathTree
Functions [hide private]
 
_split_xpath_component(xpath_component)
Split an xpath component into a tag-index tuple.
source code
 
_split_xpath(xpath)
Split an 'xpath' string separated by / into a reversed list of its components.
source code
 
_add_unit_to_tree(node, xpath_components, unit)
Walk down the tree rooted a node, and follow nodes which correspond to the components of xpath_components.
source code
 
build_unit_tree(store)
Enumerate a translation store and build a tree with XPath components as nodes and where a node contains a unit if a path from the root of the tree to the node containing the unit, is equal to the XPath of the unit.
source code

Imports: etree, base, accepts, Self, IsCallable, IsOneOf, Any, Number


Function Details [hide private]

_split_xpath_component(xpath_component)

source code 

Split an xpath component into a tag-index tuple.

>>> split_xpath_component('{urn:oasis:names:tc:opendocument:xmlns:office:1.0}document-content[0]')
('{urn:oasis:names:tc:opendocument:xmlns:office:1.0}document-content', 0).
Decorators:
  • @accepts(unicode)

_split_xpath(xpath)

source code 

Split an 'xpath' string separated by / into a reversed list of its components. Thus:

>>> split_xpath('document-content[1]/body[2]/text[3]/p[4]')
[('p', 4), ('text', 3), ('body', 2), ('document-content', 1)]

The list is reversed so that it can be used as a stack, where the top of the stack is the first component.

Decorators:
  • @accepts(unicode)

_add_unit_to_tree(node, xpath_components, unit)

source code 

Walk down the tree rooted a node, and follow nodes which correspond to the components of xpath_components. When reaching the end of xpath_components, set the reference of the node to unit.

With reference to the tree diagram in build_unit_tree:

 add_unit_to_tree(node, [('p', 2), ('text', 3), ('body', 2), ('document-content', 1)], unit)

would begin by popping ('document-content', 1) from the path and following the node marked ('document-content', 1) in the tree. Likewise, will descend down the nodes marked ('body', 2) and ('text', 3).

Since the node marked ('text', 3) has no child node marked ('p', 2), this node is created. Then the add_unit_to_tree descends down this node. When this happens, there are no xpath components left to pop. Thus, node.unit = unit is executed.

Decorators:
  • @accepts(etree._Element, [(unicode, Number)], base.TranslationUnit)

build_unit_tree(store)

source code 

Enumerate a translation store and build a tree with XPath components as nodes and where a node contains a unit if a path from the root of the tree to the node containing the unit, is equal to the XPath of the unit.

The tree looks something like this:

   root
      `- ('document-content', 1)
         `- ('body', 2)
            |- ('text', 1)
            |  `- ('p', 1)
            |     `- <reference to a unit>
            |- ('text', 2)
            |  `- ('p', 1)
            |     `- <reference to a unit>
            `- ('text', 3)
               `- ('p', 1)
                  `- <reference to a unit>
Decorators:
  • @accepts(base.TranslationStore)