forked from python-openxml/python-opc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopc_steps.py
More file actions
249 lines (213 loc) · 9.2 KB
/
opc_steps.py
File metadata and controls
249 lines (213 loc) · 9.2 KB
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
# -*- coding: utf-8 -*-
#
# opc_steps.py
#
# Copyright (C) 2012, 2013 Steve Canny scanny@cisco.com
#
# This module is part of python-opc and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
"""Acceptance test steps for python-opc."""
import hashlib
import os
from behave import given, when, then
from opc import OpcPackage
from opc.constants import CONTENT_TYPE as CT, RELATIONSHIP_TYPE as RT
def absjoin(*paths):
return os.path.abspath(os.path.join(*paths))
thisdir = os.path.split(__file__)[0]
scratch_dir = absjoin(thisdir, '../_scratch')
test_file_dir = absjoin(thisdir, '../../tests/test_files')
basic_docx_path = absjoin(test_file_dir, 'test.docx')
basic_pptx_path = absjoin(test_file_dir, 'test.pptx')
basic_xlsx_path = absjoin(test_file_dir, 'test.xlsx')
saved_docx_path = absjoin(scratch_dir, 'test_out.docx')
saved_pptx_path = absjoin(scratch_dir, 'test_out.pptx')
saved_xlsx_path = absjoin(scratch_dir, 'test_out.xlsx')
# given ====================================================
@given('a clean working directory')
def step_given_clean_working_dir(context):
files_to_clean_out = (saved_docx_path, saved_pptx_path, saved_xlsx_path)
for path in files_to_clean_out:
if os.path.isfile(path):
os.remove(path)
@given('a python-opc working environment')
def step_given_python_opc_working_environment(context):
pass
# when =====================================================
@when('I open an Excel file')
def step_when_open_basic_xlsx(context):
context.pkg = OpcPackage.open(basic_xlsx_path)
@when('I open a PowerPoint file')
def step_when_open_basic_pptx(context):
context.pkg = OpcPackage.open(basic_pptx_path)
@when('I open a Word file')
def step_when_open_basic_docx(context):
context.pkg = OpcPackage.open(basic_docx_path)
@when('I save the document package')
def step_when_save_document_package(context):
if os.path.isfile(saved_docx_path):
os.remove(saved_docx_path)
context.pkg.save(saved_docx_path)
@when('I save the presentation package')
def step_when_save_presentation_package(context):
if os.path.isfile(saved_pptx_path):
os.remove(saved_pptx_path)
context.pkg.save(saved_pptx_path)
@when('I save the spreadsheet package')
def step_when_save_spreadsheet_package(context):
if os.path.isfile(saved_xlsx_path):
os.remove(saved_xlsx_path)
context.pkg.save(saved_xlsx_path)
# then =====================================================
@then('the expected package rels are loaded')
def step_then_expected_pkg_rels_loaded(context):
expected_rel_values = (
('rId1', RT.OFFICE_DOCUMENT, False, '/ppt/presentation.xml'),
('rId2', RT.THUMBNAIL, False, '/docProps/thumbnail.jpeg'),
('rId3', RT.CORE_PROPERTIES, False, '/docProps/core.xml'),
('rId4', RT.EXTENDED_PROPERTIES, False, '/docProps/app.xml'),
)
assert len(expected_rel_values) == len(context.pkg.rels)
for rId, reltype, is_external, partname in expected_rel_values:
rel = context.pkg.rels[rId]
assert rel.rId == rId, "rId is '%s'" % rel.rId
assert rel.reltype == reltype, "reltype is '%s'" % rel.reltype
assert rel.is_external == is_external
assert rel.target_part.partname == partname, (
"target partname is '%s'" % rel.target_part.partname)
@then('the expected parts are loaded')
def step_then_expected_parts_are_loaded(context):
expected_part_values = {
'/docProps/app.xml': (
CT.OFC_EXTENDED_PROPERTIES, 'e5a7552c35180b9796f2132d39bc0d208cf'
'8761f', []
),
'/docProps/core.xml': (
CT.OPC_CORE_PROPERTIES, '08c8ff0912231db740fa1277d8fa4ef175a306e'
'4', []
),
'/docProps/thumbnail.jpeg': (
CT.JPEG, '8a93420017d57f9c69f802639ee9791579b21af5', []
),
'/ppt/presentation.xml': (
CT.PML_PRESENTATION_MAIN,
'efa7bee0ac72464903a67a6744c1169035d52a54',
[
('rId1', RT.SLIDE_MASTER, False,
'/ppt/slideMasters/slideMaster1.xml'),
('rId2', RT.SLIDE, False, '/ppt/slides/slide1.xml'),
('rId3', RT.PRINTER_SETTINGS, False,
'/ppt/printerSettings/printerSettings1.bin'),
('rId4', RT.PRES_PROPS, False, '/ppt/presProps.xml'),
('rId5', RT.VIEW_PROPS, False, '/ppt/viewProps.xml'),
('rId6', RT.THEME, False, '/ppt/theme/theme1.xml'),
('rId7', RT.TABLE_STYLES, False, '/ppt/tableStyles.xml'),
]
),
'/ppt/printerSettings/printerSettings1.bin': (
CT.PML_PRINTER_SETTINGS, 'b0feb4cc107c9b2d135b1940560cf8f045ffb7'
'46', []
),
'/ppt/presProps.xml': (
CT.PML_PRES_PROPS, '7d4981fd742429e6b8cc99089575ac0ee7db5194', []
),
'/ppt/viewProps.xml': (
CT.PML_VIEW_PROPS, '172a42a6be09d04eab61ae3d49eff5580a4be451', []
),
'/ppt/theme/theme1.xml': (
CT.OFC_THEME, '9f362326d8dc050ab6eef7f17335094bd06da47e', []
),
'/ppt/tableStyles.xml': (
CT.PML_TABLE_STYLES, '49bfd13ed02199b004bf0a019a596f127758d926',
[]
),
'/ppt/slideMasters/slideMaster1.xml': (
CT.PML_SLIDE_MASTER, 'be6fe53e199ef10259227a447e4ac9530803ecce',
[
('rId1', RT.SLIDE_LAYOUT, False,
'/ppt/slideLayouts/slideLayout1.xml'),
('rId2', RT.SLIDE_LAYOUT, False,
'/ppt/slideLayouts/slideLayout2.xml'),
('rId3', RT.SLIDE_LAYOUT, False,
'/ppt/slideLayouts/slideLayout3.xml'),
('rId4', RT.THEME, False, '/ppt/theme/theme1.xml'),
],
),
'/ppt/slideLayouts/slideLayout1.xml': (
CT.PML_SLIDE_LAYOUT, 'bcbeb908e22346fecda6be389759ca9ed068693c',
[
('rId1', RT.SLIDE_MASTER, False,
'/ppt/slideMasters/slideMaster1.xml'),
],
),
'/ppt/slideLayouts/slideLayout2.xml': (
CT.PML_SLIDE_LAYOUT, '316d0fb0ce4c3560fa2ed4edc3becf2c4ce84b6b',
[
('rId1', RT.SLIDE_MASTER, False,
'/ppt/slideMasters/slideMaster1.xml'),
],
),
'/ppt/slideLayouts/slideLayout3.xml': (
CT.PML_SLIDE_LAYOUT, '5b704e54c995b7d1bd7d24ef996a573676cc15ca',
[
('rId1', RT.SLIDE_MASTER, False,
'/ppt/slideMasters/slideMaster1.xml'),
],
),
'/ppt/slides/slide1.xml': (
CT.PML_SLIDE, '1841b18f1191629c70b7176d8e210fa2ef079d85',
[
('rId1', RT.SLIDE_LAYOUT, False,
'/ppt/slideLayouts/slideLayout1.xml'),
('rId2', RT.HYPERLINK, True,
'https://github.com/scanny/python-pptx'),
]
),
}
assert len(context.pkg.parts) == len(expected_part_values), (
"len(context.pkg.parts) is %d" % len(context.pkg.parts))
for part in context.pkg.parts:
partname = part.partname
content_type, sha1, exp_rel_vals = expected_part_values[partname]
assert part.content_type == content_type, (
"content_type for %s is '%s'" % (partname, part.content_type))
blob_sha1 = hashlib.sha1(part.blob).hexdigest()
assert blob_sha1 == sha1, ("SHA1 for %s is '%s'" %
(partname, blob_sha1))
assert len(part.rels) == len(exp_rel_vals), (
"len(part.rels) for %s is %d" % (partname, len(part.rels)))
for rId, reltype, is_external, target in exp_rel_vals:
rel = part.rels[rId]
assert rel.rId == rId, "rId is '%s'" % rel.rId
assert rel.reltype == reltype, ("reltype for %s on %s is '%s'" %
(rId, partname, rel.reltype))
assert rel.is_external == is_external
if rel.is_external:
assert rel.target_ref == target, (
"target_ref for %s on %s is '%s'" %
(rId, partname, rel.target_ref))
else:
assert rel.target_part.partname == target, (
"target partname for %s on %s is '%s'" %
(rId, partname, rel.target_part.partname))
@then('I see the docx file in the working directory')
def step_then_see_docx_file_in_working_dir(context):
reason = "file '%s' not found" % saved_docx_path
assert os.path.isfile(saved_docx_path), reason
minimum = 20000
filesize = os.path.getsize(saved_docx_path)
assert filesize > minimum
@then('I see the pptx file in the working directory')
def step_then_see_pptx_file_in_working_dir(context):
reason = "file '%s' not found" % saved_pptx_path
assert os.path.isfile(saved_pptx_path), reason
minimum = 20000
filesize = os.path.getsize(saved_pptx_path)
assert filesize > minimum
@then('I see the xlsx file in the working directory')
def step_then_see_xlsx_file_in_working_dir(context):
reason = "file '%s' not found" % saved_xlsx_path
assert os.path.isfile(saved_xlsx_path), reason
minimum = 30000
filesize = os.path.getsize(saved_xlsx_path)
assert filesize > minimum