You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.5 KiB

12 years ago
from __future__ import absolute_import, division, unicode_literals
try:
from collections import OrderedDict
except ImportError:
try:
from ordereddict import OrderedDict
except ImportError:
OrderedDict = dict
13 years ago
import gettext
_ = gettext.gettext
import re
12 years ago
from six import text_type
13 years ago
12 years ago
from . import _base
from ..utils import moduleFactoryFactory
13 years ago
12 years ago
tag_regexp = re.compile("{([^}]*)}(.*)")
13 years ago
def getETreeBuilder(ElementTreeImplementation):
ElementTree = ElementTreeImplementation
12 years ago
ElementTreeCommentType = ElementTree.Comment("asd").tag
13 years ago
class TreeWalker(_base.NonRecursiveTreeWalker):
"""Given the particular ElementTree representation, this implementation,
to avoid using recursion, returns "nodes" as tuples with the following
content:
1. The current element
12 years ago
13 years ago
2. The index of the element relative to its parent
12 years ago
13 years ago
3. A stack of ancestor elements
12 years ago
13 years ago
4. A flag "text", "tail" or None to indicate if the current node is a
text node; either the text or tail of the current element (1)
"""
def getNodeDetails(self, node):
12 years ago
if isinstance(node, tuple): # It might be the root Element
13 years ago
elt, key, parents, flag = node
if flag in ("text", "tail"):
return _base.TEXT, getattr(elt, flag)
else:
node = elt
if not(hasattr(node, "tag")):
node = node.getroot()
12 years ago
if node.tag in ("DOCUMENT_ROOT", "DOCUMENT_FRAGMENT"):
13 years ago
return (_base.DOCUMENT,)
elif node.tag == "<!DOCTYPE>":
12 years ago
return (_base.DOCTYPE, node.text,
13 years ago
node.get("publicId"), node.get("systemId"))
12 years ago
elif node.tag == ElementTreeCommentType:
13 years ago
return _base.COMMENT, node.text
else:
12 years ago
assert type(node.tag) == text_type, type(node.tag)
# This is assumed to be an ordinary element
13 years ago
match = tag_regexp.match(node.tag)
if match:
namespace, tag = match.groups()
else:
namespace = None
tag = node.tag
12 years ago
attrs = OrderedDict()
for name, value in list(node.attrib.items()):
13 years ago
match = tag_regexp.match(name)
if match:
12 years ago
attrs[(match.group(1), match.group(2))] = value
13 years ago
else:
12 years ago
attrs[(None, name)] = value
return (_base.ELEMENT, namespace, tag,
13 years ago
attrs, len(node) or node.text)
12 years ago
13 years ago
def getFirstChild(self, node):
if isinstance(node, tuple):
element, key, parents, flag = node
else:
element, key, parents, flag = node, None, [], None
12 years ago
13 years ago
if flag in ("text", "tail"):
return None
else:
if element.text:
return element, key, parents, "text"
elif len(element):
parents.append(element)
return element[0], 0, parents, None
else:
return None
12 years ago
13 years ago
def getNextSibling(self, node):
if isinstance(node, tuple):
element, key, parents, flag = node
else:
return None
12 years ago
13 years ago
if flag == "text":
if len(element):
parents.append(element)
return element[0], 0, parents, None
else:
return None
else:
if element.tail and flag != "tail":
return element, key, parents, "tail"
elif key < len(parents[-1]) - 1:
12 years ago
return parents[-1][key + 1], key + 1, parents, None
13 years ago
else:
return None
12 years ago
13 years ago
def getParentNode(self, node):
if isinstance(node, tuple):
element, key, parents, flag = node
else:
return None
12 years ago
13 years ago
if flag == "text":
if not parents:
return element
else:
return element, key, parents, None
else:
parent = parents.pop()
if not parents:
return parent
else:
return parent, list(parents[-1]).index(parent), parents, None
return locals()
12 years ago
getETreeModule = moduleFactoryFactory(getETreeBuilder)