/usr/share/pyshared/kid/element.py is in python-kid 0.9.6-2.1.
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | # -*- coding: utf-8 -*-
"""A partial implementation of ElementTree and some extensions."""
__revision__ = "$Rev: 492 $"
__date__ = "$Date: 2007-07-06 21:38:45 -0400 (Fri, 06 Jul 2007) $"
__author__ = "Ryan Tomayko (rtomayko@gmail.com)"
__copyright__ = "Copyright 2004-2005, Ryan Tomayko"
__license__ = "MIT <http://www.opensource.org/licenses/mit-license.php>"
import re
__all__ = ['Element', 'SubElement', 'Comment', 'ProcessingInstruction',
'Fragment', 'QName', 'namespaces', 'escape_map',
'encode_entity', 'raise_serialization_error']
class Element(object):
"""A class representing an XML element."""
text = None
tail = None
def __init__(self, tag, attrib={}, **extra):
attrib = attrib.copy()
attrib.update(extra)
self.tag = tag
self.attrib = attrib
self._children = []
def __repr__(self):
return "<Element %s at %x>" % (self.tag, id(self))
# Methods for dealing with children - a list interface
def __len__(self):
return len(self._children)
def __getitem__(self, index):
return self._children[index]
def __setitem__(self, index, element):
assert isinstance(element, Element)
self._children[index] = element
def __delitem__(self, index):
del self._children[index]
def __getslice__(self, start, stop):
return self._children[start:stop]
def __setslice__(self, start, stop, elements):
for element in elements:
assert isinstance(element, Element)
self._children[start:stop] = list(elements)
def __delslice__(self, start, stop):
del self._children[start:stop]
def append(self, element):
assert isinstance(element, Element)
self._children.append(element)
def insert(self, index, element):
assert isinstance(element, Element)
self._children.insert(index, element)
def remove(self, element):
assert isinstance(element, Element)
self._children.remove(element)
def getchildren(self):
return self._children
def clear(self):
self.attrib.clear()
self._children = []
self.text = self.tail = None
# Methods for dealing with attributes - a dictionary like interface
def get(self, key, value=None):
return self.attrib.get(key, value)
def set(self, key, value):
self.attrib[key] = value
def keys(self):
return self.attrib.keys()
def items(self):
return self.attrib.items()
def SubElement(parent, tag, attrib={}, **extra):
attrib = attrib.copy()
attrib.update(extra)
element = Element(tag, attrib)
parent.append(element)
return element
def Comment(text=None):
"""Comment element factory."""
elem = Element(Comment)
elem.text = text
return elem
def ProcessingInstruction(target, text=None):
"""PI element factory."""
elem = Element(ProcessingInstruction)
elem.text = target
if text:
elem.text += " " + text
return elem
def Fragment(text=''):
"""XML fragment factory.
Fragments hold TEXT and children but do not have a tag or attributes.
"""
elem = Element(Fragment)
elem.text = text
return elem
class QName:
def __init__(self, text_or_uri, tag=None):
if tag:
text_or_uri = "{%s}%s" % (text_or_uri, tag)
self.text = text_or_uri
def __str__(self):
return self.text
def namespaces(elem, remove=False):
"""Get the namespace declarations for an Element.
This function looks for attributes on the Element provided that have the
following characteristics:
* Begin with 'xmlns:' and have no namespace URI.
* Are named 'xmlns' and have no namespace URI.
The result is a dictionary containing namespace prefix -> URI mappings.
Default namespace attributes result in a key of ''.
If remove is truthful, namespace declaration attributes are removed
from the passed in Element.
"""
names = {}
for k in elem.keys():
if k.startswith('xmlns:'):
names[k[6:]] = elem.get(k)
if remove:
del elem.attrib[k]
elif k == 'xmlns':
names[''] = elem.get(k)
if remove:
del elem.attrib[k]
return names
escape_map = {
"&": "&",
"<": "<",
">": ">",
'"': """,
}
re_escape = re.compile(eval(r'u"[&<>\"\u0080-\uffff]+"'))
def encode_entity(text, pattern=re_escape, entities=None):
if entities is None:
entities = escape_map
# map reserved and non-ascii characters to XML entities
def escape_entities(m, entities=entities):
out = []
for char in m.group():
text = entities.get(char)
if text is None:
text = "&#%d;" % ord(char)
out.append(text)
return ''.join(out)
try:
return pattern.sub(escape_entities, text).encode('ascii')
except TypeError:
raise_serialization_error(text)
def raise_serialization_error(text):
raise TypeError(
"cannot serialize %r (type %s)" % (text, type(text).__name__)
)
|