translate.storage.xml_extract.xpath_breadcrumb

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

# Source Code for Module translate.storage.xml_extract.xpath_breadcrumb

     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 translate.misc.typecheck import accepts, Self
    24
    25 -class XPathBreadcrumb(object):
    26      """A class which is used to build XPath-like paths as a DOM tree is
    27      walked. It keeps track of the number of times which it has seen
    28      a certain tag, so that it will correctly create indices for tags.
    29
    30      Initially, the path is empty. Thus
    31      >>> xb = XPathBreadcrumb()
    32      >>> xb.xpath
    33      ""
    34
    35      Suppose we walk down a DOM node for the tag <foo> and we want to
    36      record this, we simply do
    37      >>> xb.start_tag('foo')
    38
    39      Now, the path is no longer empty. Thus
    40      >>> xb.xpath
    41      foo[0]
    42
    43      Now suppose there are two <bar> tags under the tag <foo> (that is
    44      <foo><bar></bar><bar></bar><foo>), then the breadcrumb will keep
    45      track of the number of times it sees <bar>. Thus
    46
    47      >>> xb.start_tag('bar')
    48      >>> xb.xpath
    49      foo[0]/bar[0]
    50      >>> xb.end_tag()
    51      >>> xb.xpath
    52      foo[0]
    53      >>> xb.start_tag('bar')
    54      >>> xb.xpath
    55      foo[0]/bar[1]
    56      """
    57
    58 -    def __init__(self):
    59          self._xpath = []
    60          self._tagtally = [{}]
    61
    62      @accepts(Self(), unicode)
    63 -    def start_tag(self, tag):
    64          tally_dict = self._tagtally[-1]
    65          tally = tally_dict.get(tag, -1) + 1
    66          tally_dict[tag] = tally
    67          self._xpath.append((tag, tally))
    68          self._tagtally.append({})
    69
    70 -    def end_tag(self):
    71          self._xpath.pop()
    72          self._tagtally.pop()
    73
    74 -    def _get_xpath(self):
    75          def str_component(component):
    76              tag, pos = component
    77              return u"%s[%d]" % (tag, pos)
    78          return u"/".join(str_component(component) for component in self._xpath)
    79
    80      xpath = property(_get_xpath)
    81