/usr/lib/python2.7/dist-packages/openpyxl/writer/tests/test_write_only.py is in python-openpyxl 2.3.0-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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | from __future__ import absolute_import
# Copyright (c) 2010-2015 openpyxl
import datetime
import decimal
from io import BytesIO
from zipfile import ZipFile
from tempfile import TemporaryFile
from openpyxl.xml.functions import tostring, xmlfile
from openpyxl.utils.indexed_list import IndexedList
from openpyxl.utils.datetime import CALENDAR_WINDOWS_1900
from openpyxl.styles import Style
from openpyxl.styles.styleable import StyleArray
from openpyxl.tests.helper import compare_xml
import pytest
class DummyWorkbook:
def __init__(self):
self.shared_strings = IndexedList()
self._cell_styles = IndexedList(
[StyleArray([0, 0, 0, 0, 0, 0, 0, 0, 0])]
)
self._number_formats = IndexedList()
self.encoding = "UTF-8"
self.excel_base_date = CALENDAR_WINDOWS_1900
self.sheetnames = []
@pytest.fixture
def WriteOnlyWorksheet():
from ..write_only import WriteOnlyWorksheet
return WriteOnlyWorksheet(DummyWorkbook(), title="TestWorksheet")
@pytest.mark.lxml_required
def test_write_header(WriteOnlyWorksheet):
ws = WriteOnlyWorksheet
doc = ws._write_header()
next(doc)
doc.close()
header = open(ws.filename)
xml = header.read()
expected = """
<worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<sheetPr>
<outlinePr summaryRight="1" summaryBelow="1"/>
<pageSetUpPr/>
</sheetPr>
<sheetViews>
<sheetView workbookViewId="0">
<selection sqref="A1" activeCell="A1"/>
</sheetView>
</sheetViews>
<sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
<sheetData/>
</worksheet>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_append(WriteOnlyWorksheet):
ws = WriteOnlyWorksheet
def _writer(doc):
with xmlfile(doc) as xf:
with xf.element('sheetData'):
try:
while True:
body = (yield)
xf.write(body)
except GeneratorExit:
pass
doc = BytesIO()
ws.writer = _writer(doc)
next(ws.writer)
ws.append([1, "s"])
ws.append(['2', 3])
ws.append(i for i in [1, 2])
ws.writer.close()
xml = doc.getvalue()
expected = """
<sheetData>
<row r="1" spans="1:2">
<c r="A1" t="n">
<v>1</v>
</c>
<c r="B1" t="s">
<v>0</v>
</c>
</row>
<row r="2" spans="1:2">
<c r="A2" t="s">
<v>1</v>
</c>
<c r="B2" t="n">
<v>3</v>
</c>
</row>
<row r="3" spans="1:2">
<c r="A3" t="n">
<v>1</v>
</c>
<c r="B3" t="n">
<v>2</v>
</c>
</row>
</sheetData>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_dirty_cell(WriteOnlyWorksheet):
ws = WriteOnlyWorksheet
def _writer(doc):
with xmlfile(doc) as xf:
with xf.element('sheetData'):
try:
while True:
body = (yield)
xf.write(body)
except GeneratorExit:
pass
doc = BytesIO()
ws.writer = _writer(doc)
next(ws.writer)
ws.append((datetime.date(2001, 1, 1), 1))
ws.writer.close()
xml = doc.getvalue()
expected = """
<sheetData>
<row r="1" spans="1:2">
<c r="A1" t="n" s="1"><v>36892</v></c>
<c r="B1" t="n"><v>1</v></c>
</row>
</sheetData>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
@pytest.mark.parametrize("row", ("string", dict()))
def test_invalid_append(WriteOnlyWorksheet, row):
ws = WriteOnlyWorksheet
with pytest.raises(TypeError):
ws.append(row)
@pytest.mark.lxml_required
def test_cell_comment(WriteOnlyWorksheet):
ws = WriteOnlyWorksheet
from openpyxl.comments import Comment
from .. write_only import WriteOnlyCell
cell = WriteOnlyCell(ws, 1)
comment = Comment('hello', 'me')
cell.comment = comment
ws.append([cell])
assert ws._comments == [comment]
ws.close()
with open(ws.filename) as src:
xml = src.read()
expected = """
<worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<sheetPr>
<outlinePr summaryRight="1" summaryBelow="1"/>
<pageSetUpPr/>
</sheetPr>
<sheetViews>
<sheetView workbookViewId="0">
<selection sqref="A1" activeCell="A1"/>
</sheetView>
</sheetViews>
<sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
<sheetData>
<row r="1" spans="1:1"><c r="A1" t="n"><v>1</v></c></row>
</sheetData>
<legacyDrawing r:id="commentsvml"></legacyDrawing>
</worksheet>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
@pytest.mark.lxml_required
def test_cannot_save_twice(WriteOnlyWorksheet):
from .. write_only import WorkbookAlreadySaved
ws = WriteOnlyWorksheet
ws.close()
with pytest.raises(WorkbookAlreadySaved):
ws.close()
with pytest.raises(WorkbookAlreadySaved):
ws.append([1])
@pytest.mark.lxml_required
def test_close(WriteOnlyWorksheet):
ws = WriteOnlyWorksheet
ws.close()
with open(ws.filename) as src:
xml = src.read()
expected = """
<worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<sheetPr>
<outlinePr summaryRight="1" summaryBelow="1"/>
<pageSetUpPr/>
</sheetPr>
<sheetViews>
<sheetView workbookViewId="0">
<selection sqref="A1" activeCell="A1"/>
</sheetView>
</sheetViews>
<sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
<sheetData/>
</worksheet>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
@pytest.mark.lxml_required
def test_auto_filter(WriteOnlyWorksheet):
ws = WriteOnlyWorksheet
ws.auto_filter.ref = 'A1:F1'
ws.close()
with open(ws.filename) as src:
xml = src.read()
expected = """
<worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<sheetPr>
<outlinePr summaryRight="1" summaryBelow="1"/>
<pageSetUpPr/>
</sheetPr>
<sheetViews>
<sheetView workbookViewId="0">
<selection sqref="A1" activeCell="A1"/>
</sheetView>
</sheetViews>
<sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
<sheetData/>
<autoFilter ref="A1:F1"/>
</worksheet>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
@pytest.mark.lxml_required
def test_frozen_panes(WriteOnlyWorksheet):
ws = WriteOnlyWorksheet
ws.freeze_panes = 'D4'
ws.close()
with open(ws.filename) as src:
xml = src.read()
expected = """
<worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<sheetPr>
<outlinePr summaryRight="1" summaryBelow="1"/>
<pageSetUpPr/>
</sheetPr>
<sheetViews>
<sheetView workbookViewId="0">
<pane xSplit="3" ySplit="3" topLeftCell="D4" activePane="bottomRight" state="frozen"/>
<selection pane="topRight"/>
<selection pane="bottomLeft"/>
<selection pane="bottomRight" activeCell="A1" sqref="A1"/>
</sheetView>
</sheetViews>
<sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
<sheetData/>
</worksheet>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
@pytest.mark.lxml_required
def test_write_empty_row(WriteOnlyWorksheet):
ws = WriteOnlyWorksheet
ws.append(['1', '2', '3'])
ws.append([])
ws.close()
with open(ws.filename) as src:
xml = src.read()
expected = """
<worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<sheetPr>
<outlinePr summaryRight="1" summaryBelow="1"/>
<pageSetUpPr/>
</sheetPr>
<sheetViews>
<sheetView workbookViewId="0">
<selection sqref="A1" activeCell="A1"/>
</sheetView>
</sheetViews>
<sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
<sheetData>
<row r="1" spans="1:3">
<c r="A1" t="s">
<v>0</v>
</c>
<c r="B1" t="s">
<v>1</v>
</c>
<c r="C1" t="s">
<v>2</v>
</c>
</row>
<row r="2"/>
</sheetData>
</worksheet>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_save():
from tempfile import NamedTemporaryFile
filename = NamedTemporaryFile(delete=False)
from openpyxl.workbook import Workbook
from ..write_only import save_dump
wb = Workbook(write_only=True)
save_dump(wb, filename)
|