/usr/share/pyshared/translate/misc/xmlwrapper.py is in translate-toolkit 1.10.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2004, 2005 Zuza Software Foundation
#
# This file is part of translate.
#
# translate is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# translate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
"""simpler wrapper to the elementtree XML parser"""
try:
from xml.etree import ElementTree
except ImportError:
from elementtree import ElementTree
# this is needed to prevent expat-version conflicts with wx >= 2.5.2.2
from xml.parsers import expat
basicfixtag = ElementTree.fixtag
def makefixtagproc(namespacemap):
"""Constructs an alternative fixtag procedure that will use appropriate
names for namespaces."""
def fixtag(tag, namespaces):
"""Given a decorated tag (of the form {uri}tag), return prefixed
tag and namespace declaration, if any."""
if isinstance(tag, ElementTree.QName):
tag = tag.text
namespace_uri, tag = tag[1:].split("}", 1)
prefix = namespaces.get(namespace_uri)
if prefix is None:
if namespace_uri in namespacemap:
prefix = namespacemap[namespace_uri]
else:
prefix = "ns%d" % len(namespaces)
namespaces[namespace_uri] = prefix
xmlns = ("xmlns:%s" % prefix, namespace_uri)
else:
xmlns = None
return "%s:%s" % (prefix, tag), xmlns
return fixtag
def splitnamespace(fulltag):
if '{' in fulltag:
namespace = fulltag[fulltag.find('{'):fulltag.find('}')+1]
else:
namespace = ""
tag = fulltag.replace(namespace, "", 1)
return namespace, tag
class XMLWrapper:
"""simple wrapper for xml objects"""
def __init__(self, obj):
"""construct object from the elementtree item"""
self.obj = obj
self.namespace, self.tag = splitnamespace(self.obj.tag)
self.attrib = {}
for fullkey, value in self.obj.attrib.iteritems():
namespace, key = splitnamespace(fullkey)
self.attrib[key] = value
def getchild(self, searchtag, tagclass=None):
"""get a child with the given tag name"""
if tagclass is None:
tagclass = XMLWrapper
for childobj in self.obj.getiterator():
# getiterator() includes self...
if childobj == self.obj:
continue
childns, childtag = splitnamespace(childobj.tag)
if childtag == searchtag:
child = tagclass(childobj)
return child
raise KeyError("could not find child with tag %r" % searchtag)
def getchildren(self, searchtag, tagclass=None, excludetags=[]):
"""get all children with the given tag name"""
if tagclass is None:
tagclass = XMLWrapper
childobjects = []
for childobj in self.obj.getiterator():
# getiterator() includes self...
if childobj == self.obj:
continue
childns, childtag = splitnamespace(childobj.tag)
if childtag == searchtag:
childobjects.append(childobj)
children = [tagclass(childobj) for childobj in childobjects]
return children
def gettext(self, searchtag):
"""get some contained text"""
return self.getchild(searchtag).obj.text
def getxml(self, encoding=None):
return ElementTree.tostring(self.obj, encoding)
def getplaintext(self, excludetags=[]):
text = ""
if self.obj.text != None:
text += self.obj.text
for child in self.obj._children:
simplechild = XMLWrapper(child)
if simplechild.tag not in excludetags:
text += simplechild.getplaintext(excludetags)
if self.obj.tail != None:
text += self.obj.tail
return text
def getvalues(self, searchtag):
"""get some contained values..."""
values = [child.obj.text for child in self.getchildren(searchtag)]
return values
def __repr__(self):
"""return a representation of the object"""
return self.tag + ':' + repr(self.__dict__)
def getattr(self, attrname):
"""gets an attribute of the tag"""
return self.attrib[attrname]
def write(self, file, encoding="UTF-8"):
"""writes the object as XML to a file..."""
e = ElementTree.ElementTree(self.obj)
e.write(file, encoding)
def BuildTree(xmlstring):
parser = ElementTree.XMLTreeBuilder()
parser.feed(xmlstring)
return parser.close()
def MakeElement(tag, attrib={}, **extraargs):
return ElementTree.Element(tag, attrib, **extraargs)
|