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

Source Code for Module translate.storage.xml_extract.unit_tree

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright 2002-2006 Zuza Software Foundation 
  5  #  
  6  # This file is part of translate. 
  7  # 
  8  # translate is free software; you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation; either version 2 of the License, or 
 11  # (at your option) any later version. 
 12  #  
 13  # translate is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with translate; if not, write to the Free Software 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 21  # 
 22   
 23  from lxml import etree 
 24   
 25  from translate.storage import base 
 26  from translate.misc.typecheck import accepts, Self, IsCallable, IsOneOf, Any 
 27  from translate.misc.typecheck.typeclasses import Number 
28 29 -class XPathTree(object):
30 @accepts(Self(), base.TranslationUnit)
31 - def __init__(self, unit = None):
32 self.unit = unit 33 self.children = {}
34
35 - def __eq__(self, other):
36 return isinstance(other, XPathTree) and \ 37 self.unit == other.unit and \ 38 self.children == other.children
39
40 @accepts(unicode) 41 -def _split_xpath_component(xpath_component):
42 """Split an xpath component into a tag-index tuple. 43 44 >>> split_xpath_component('{urn:oasis:names:tc:opendocument:xmlns:office:1.0}document-content[0]') 45 ('{urn:oasis:names:tc:opendocument:xmlns:office:1.0}document-content', 0). 46 """ 47 lbrac = xpath_component.rfind(u'[') 48 rbrac = xpath_component.rfind(u']') 49 tag = xpath_component[:lbrac] 50 index = int(xpath_component[lbrac+1:rbrac]) 51 return tag, index
52
53 @accepts(unicode) 54 -def _split_xpath(xpath):
55 """Split an 'xpath' string separated by / into a reversed list of its components. Thus: 56 57 >>> split_xpath('document-content[1]/body[2]/text[3]/p[4]') 58 [('p', 4), ('text', 3), ('body', 2), ('document-content', 1)] 59 60 The list is reversed so that it can be used as a stack, where the top of the stack is 61 the first component. 62 """ 63 components = xpath.split(u'/') 64 components = [_split_xpath_component(component) for component in components] 65 return list(reversed(components))
66 67 @accepts(etree._Element, [(unicode, Number)], base.TranslationUnit)
68 -def _add_unit_to_tree(node, xpath_components, unit):
69 """Walk down the tree rooted a node, and follow nodes which correspond to the 70 components of xpath_components. When reaching the end of xpath_components, 71 set the reference of the node to unit. 72 73 With reference to the tree diagram in build_unit_tree:: 74 75 add_unit_to_tree(node, [('p', 2), ('text', 3), ('body', 2), ('document-content', 1)], unit) 76 77 would begin by popping ('document-content', 1) from the path and following the node marked 78 ('document-content', 1) in the tree. Likewise, will descend down the nodes marked ('body', 2) 79 and ('text', 3). 80 81 Since the node marked ('text', 3) has no child node marked ('p', 2), this node is created. Then 82 the add_unit_to_tree descends down this node. When this happens, there are no xpath components 83 left to pop. Thus, node.unit = unit is executed. 84 """ 85 if len(xpath_components) > 0: 86 component = xpath_components.pop() # pop the stack; is a component such as ('p', 4) 87 # if the current node does not have any children indexed by 88 # the current component, add such a child 89 if component not in node.children: 90 node.children[component] = XPathTree() 91 _add_unit_to_tree(node.children[component], xpath_components, unit) 92 else: 93 node.unit = unit
94 95 @accepts(base.TranslationStore)
96 -def build_unit_tree(store):
97 """Enumerate a translation store and build a tree with XPath components as nodes 98 and where a node contains a unit if a path from the root of the tree to the node 99 containing the unit, is equal to the XPath of the unit. 100 101 The tree looks something like this:: 102 root 103 `- ('document-content', 1) 104 `- ('body', 2) 105 |- ('text', 1) 106 | `- ('p', 1) 107 | `- <reference to a unit> 108 |- ('text', 2) 109 | `- ('p', 1) 110 | `- <reference to a unit> 111 `- ('text', 3) 112 `- ('p', 1) 113 `- <reference to a unit> 114 """ 115 tree = XPathTree() 116 for unit in store.units: 117 location = _split_xpath(unit.getlocations()[0]) 118 _add_unit_to_tree(tree, location, unit) 119 return tree
120