/usr/lib/python2.7/dist-packages/xlsxwriter/xmlwriter.py is in python-xlsxwriter 0.7.3-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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | ###############################################################################
#
# XMLwriter - A base class for XlsxWriter classes.
#
# Used in conjunction with XlsxWriter.
#
# Copyright 2013-2015, John McNamara, jmcnamara@cpan.org
#
# Standard packages.
import re
import codecs
# Standard packages in Python 2/3 compatibility mode.
from .compatibility import StringIO
class XMLwriter(object):
"""
Simple XML writer class.
"""
def __init__(self):
self.fh = None
self.escapes = re.compile('["&<>]')
self.internal_fh = False
def _set_filehandle(self, filehandle):
# Set the writer filehandle directly. Mainly for testing.
self.fh = filehandle
self.internal_fh = False
def _set_xml_writer(self, filename):
# Set the XML writer filehandle for the object.
if isinstance(filename, StringIO):
self.internal_fh = False
self.fh = filename
else:
self.internal_fh = True
self.fh = codecs.open(filename, 'w', 'utf-8')
def _xml_close(self):
# Close the XML filehandle if we created it.
if self.internal_fh:
self.fh.close()
def _xml_declaration(self):
# Write the XML declaration.
self.fh.write(
"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n""")
def _xml_start_tag(self, tag, attributes=[]):
# Write an XML start tag with optional attributes.
for key, value in attributes:
value = self._escape_attributes(value)
tag += ' %s="%s"' % (key, value)
self.fh.write("<%s>" % tag)
def _xml_start_tag_unencoded(self, tag, attributes=[]):
# Write an XML start tag with optional, unencoded, attributes.
# This is a minor speed optimisation for elements that don't
# need encoding.
for key, value in attributes:
tag += ' %s="%s"' % (key, value)
self.fh.write("<%s>" % tag)
def _xml_end_tag(self, tag):
# Write an XML end tag.
self.fh.write("</%s>" % tag)
def _xml_empty_tag(self, tag, attributes=[]):
# Write an empty XML tag with optional attributes.
for key, value in attributes:
value = self._escape_attributes(value)
tag += ' %s="%s"' % (key, value)
self.fh.write("<%s/>" % tag)
def _xml_empty_tag_unencoded(self, tag, attributes=[]):
# Write an empty XML tag with optional, unencoded, attributes.
# This is a minor speed optimisation for elements that don't
# need encoding.
for key, value in attributes:
tag += ' %s="%s"' % (key, value)
self.fh.write("<%s/>" % tag)
def _xml_data_element(self, tag, data, attributes=[]):
# Write an XML element containing data with optional attributes.
end_tag = tag
for key, value in attributes:
value = self._escape_attributes(value)
tag += ' %s="%s"' % (key, value)
data = self._escape_data(data)
self.fh.write("<%s>%s</%s>" % (tag, data, end_tag))
def _xml_string_element(self, index, attributes=[]):
# Optimised tag writer for <c> cell string elements in the inner loop.
attr = ''
for key, value in attributes:
value = self._escape_attributes(value)
attr += ' %s="%s"' % (key, value)
self.fh.write("""<c%s t="s"><v>%d</v></c>""" % (attr, index))
def _xml_si_element(self, string, attributes=[]):
# Optimised tag writer for shared strings <si> elements.
attr = ''
for key, value in attributes:
value = self._escape_attributes(value)
attr += ' %s="%s"' % (key, value)
string = self._escape_data(string)
self.fh.write("""<si><t%s>%s</t></si>""" % (attr, string))
def _xml_rich_si_element(self, string):
# Optimised tag writer for shared strings <si> rich string elements.
self.fh.write("""<si>%s</si>""" % string)
def _xml_number_element(self, number, attributes=[]):
# Optimised tag writer for <c> cell number elements in the inner loop.
attr = ''
for key, value in attributes:
value = self._escape_attributes(value)
attr += ' %s="%s"' % (key, value)
self.fh.write("""<c%s><v>%.15g</v></c>""" % (attr, number))
def _xml_formula_element(self, formula, result, attributes=[]):
# Optimised tag writer for <c> cell formula elements in the inner loop.
attr = ''
for key, value in attributes:
value = self._escape_attributes(value)
attr += ' %s="%s"' % (key, value)
self.fh.write("""<c%s><f>%s</f><v>%s</v></c>"""
% (attr, self._escape_data(formula),
self._escape_data(result)))
def _xml_inline_string(self, string, preserve, attributes=[]):
# Optimised tag writer for inlineStr cell elements in the inner loop.
attr = ''
t_attr = ''
# Set the <t> attribute to preserve whitespace.
if preserve:
t_attr = ' xml:space="preserve"'
for key, value in attributes:
value = self._escape_attributes(value)
attr += ' %s="%s"' % (key, value)
string = self._escape_data(string)
self.fh.write("""<c%s t="inlineStr"><is><t%s>%s</t></is></c>""" %
(attr, t_attr, string))
def _xml_rich_inline_string(self, string, attributes=[]):
# Optimised tag writer for rich inlineStr in the inner loop.
attr = ''
for key, value in attributes:
value = self._escape_attributes(value)
attr += ' %s="%s"' % (key, value)
self.fh.write("""<c%s t="inlineStr"><is>%s</is></c>""" %
(attr, string))
def _escape_attributes(self, attribute):
# Escape XML characters in attributes.
try:
if not self.escapes.search(attribute):
return attribute
except TypeError:
return attribute
attribute = attribute.replace('&', '&')
attribute = attribute.replace('"', '"')
attribute = attribute.replace('<', '<')
attribute = attribute.replace('>', '>')
return attribute
def _escape_data(self, data):
# Escape XML characters in data sections of tags. Note, this
# is different from _escape_attributes() in that double quotes
# are not escaped by Excel.
try:
if not self.escapes.search(data):
return data
except TypeError:
return data
data = data.replace('&', '&')
data = data.replace('<', '<')
data = data.replace('>', '>')
return data
|