/usr/share/pyshared/FontTools/xmlWriter.py is in fonttools 2.4-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 | """xmlWriter.py -- Simple XML authoring class"""
import string
import struct
import os
INDENT = " "
class XMLWriter:
def __init__(self, fileOrPath, indentwhite=INDENT, idlefunc=None, encoding="ISO-8859-1"):
if not hasattr(fileOrPath, "write"):
self.file = open(fileOrPath, "w")
else:
# assume writable file object
self.file = fileOrPath
self.indentwhite = indentwhite
self.indentlevel = 0
self.stack = []
self.needindent = 1
self.idlefunc = idlefunc
self.idlecounter = 0
if encoding:
self.writeraw('<?xml version="1.0" encoding="%s"?>' % encoding)
else:
self.writeraw('<?xml version="1.0"?>')
self.newline()
def close(self):
self.file.close()
def write(self, data):
self.writeraw(escape(data))
def write_noindent(self, data):
self.file.write(escape(data))
def write8bit(self, data):
self.writeraw(escape8bit(data))
def write16bit(self, data):
self.writeraw(escape16bit(data))
def writeraw(self, data):
if self.needindent:
self.file.write(self.indentlevel * self.indentwhite)
self.needindent = 0
self.file.write(data)
def newline(self):
self.file.write("\n")
self.needindent = 1
idlecounter = self.idlecounter
if not idlecounter % 100 and self.idlefunc is not None:
self.idlefunc()
self.idlecounter = idlecounter + 1
def comment(self, data):
data = escape(data)
lines = string.split(data, "\n")
self.writeraw("<!-- " + lines[0])
for line in lines[1:]:
self.newline()
self.writeraw(" " + line)
self.writeraw(" -->")
def simpletag(self, _TAG_, *args, **kwargs):
attrdata = apply(self.stringifyattrs, args, kwargs)
data = "<%s%s/>" % (_TAG_, attrdata)
self.writeraw(data)
def begintag(self, _TAG_, *args, **kwargs):
attrdata = apply(self.stringifyattrs, args, kwargs)
data = "<%s%s>" % (_TAG_, attrdata)
self.writeraw(data)
self.stack.append(_TAG_)
self.indent()
def endtag(self, _TAG_):
assert self.stack and self.stack[-1] == _TAG_, "nonmatching endtag"
del self.stack[-1]
self.dedent()
data = "</%s>" % _TAG_
self.writeraw(data)
def dumphex(self, data):
linelength = 16
hexlinelength = linelength * 2
chunksize = 8
for i in range(0, len(data), linelength):
hexline = hexStr(data[i:i+linelength])
line = ""
white = ""
for j in range(0, hexlinelength, chunksize):
line = line + white + hexline[j:j+chunksize]
white = " "
self.writeraw(line)
self.newline()
def indent(self):
self.indentlevel = self.indentlevel + 1
def dedent(self):
assert self.indentlevel > 0
self.indentlevel = self.indentlevel - 1
def stringifyattrs(self, *args, **kwargs):
if kwargs:
assert not args
attributes = kwargs.items()
attributes.sort()
elif args:
assert len(args) == 1
attributes = args[0]
else:
return ""
data = ""
for attr, value in attributes:
data = data + ' %s="%s"' % (attr, escapeattr(str(value)))
return data
def escape(data):
data = string.replace(data, "&", "&")
data = string.replace(data, "<", "<")
return data
def escapeattr(data):
data = string.replace(data, "&", "&")
data = string.replace(data, "<", "<")
data = string.replace(data, '"', """)
return data
def escape8bit(data):
def escapechar(c):
n = ord(c)
if c in "<&":
if c == "&":
return "&"
else:
return "<"
elif 32 <= n <= 127:
return c
else:
return "&#" + `n` + ";"
return string.join(map(escapechar, data), "")
needswap = struct.pack("h", 1) == "\001\000"
def escape16bit(data):
import array
a = array.array("H")
a.fromstring(data)
if needswap:
a.byteswap()
def escapenum(n, amp=ord("&"), lt=ord("<")):
if n == amp:
return "&"
elif n == lt:
return "<"
elif 32 <= n <= 127:
return chr(n)
else:
return "&#" + `n` + ";"
return string.join(map(escapenum, a), "")
def hexStr(s):
h = string.hexdigits
r = ''
for c in s:
i = ord(c)
r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
return r
|