Repository navigation
Expand file tree
/
Copy pathtable.py
More file actions
305 lines (205 loc) · 10 KB
/
Copy pathtable.py
File metadata and controls
305 lines (205 loc) · 10 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
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
"""Gherkin step implementations for table-related features"""
from __future__ import annotations
from behave import given, then, when
from helpers import test_pptx
from pptx import Presentation
from pptx.enum.text import MSO_ANCHOR # noqa # pyright: ignore[reportUnusedImport]
from pptx.util import Inches
# given ===================================================
@given("a Table object as table")
@given("a 2x2 Table object as table")
def given_a_2x2_Table_object_as_table(context):
prs = Presentation(test_pptx("shp-shapes"))
context.table_ = prs.slides[0].shapes[3].table
@given("a 2x3 _MergeOriginCell object as cell")
def given_a_2x3_MergeOriginCell_object_as_cell(context):
prs = Presentation(test_pptx("tbl-cell"))
context.cell = prs.slides[1].shapes[1].table.cell(0, 0)
@given("a 3x3 Table object as table")
@given("a 3x3 Table object with cells a to i as table")
def given_a_3x3_table_with_cells_a_to_i_as_table(context):
prs = Presentation(test_pptx("tbl-cell"))
# ---context.table is used by Behave for some odd reason---
context.table_ = prs.slides[2].shapes[0].table
@given("a _Cell object as cell")
def given_a_Cell_object_as_cell(context):
prs = Presentation(test_pptx("shp-shapes"))
context.cell = prs.slides[0].shapes[3].table.cell(0, 0)
@given('a _Cell object containing "unladen swallows" as cell')
def given_a_Cell_object_containing_unladen_swallows_as_cell(context):
prs = Presentation(test_pptx("tbl-cell"))
context.cell = prs.slides[0].shapes[0].table.cell(1, 0)
@given("a _Cell object with known margins as cell")
def given_a_Cell_object_with_known_margins_as_cell(context):
prs = Presentation(test_pptx("tbl-cell"))
context.cell = prs.slides[0].shapes[0].table.cell(0, 0)
@given("a _Cell object with {setting} vertical alignment as cell")
def given_a_Cell_object_with_setting_vertical_alignment(context, setting):
cell_coordinates = {"inherited": (0, 1), "middle": (0, 2), "bottom": (0, 3)}[setting]
prs = Presentation(test_pptx("tbl-cell"))
context.cell = prs.slides[0].shapes[0].table.cell(*cell_coordinates)
@given("a {role} _Cell object as cell")
def given_a_role_Cell_object_as_cell(context, role):
coordinates = {"merge-origin": (0, 0), "spanned": (0, 1), "unmerged": (2, 2)}[role]
table = Presentation(test_pptx("tbl-cell")).slides[1].shapes[0].table
# ---create other_cell here where we know the coordinates---
context.cell = table.cell(*coordinates)
context.other_cell = table.cell(*coordinates)
@given("a second proxy instance for that cell as other_cell")
def given_a_second_proxy_instance_for_that_cell_as_other_cell(context):
# ---other_cell is actually produced by prior step---
assert context.other_cell
@given("a _Column object as column")
def given_a_Column_object_as_column(context):
prs = Presentation(test_pptx("shp-shapes"))
context.column = prs.slides[0].shapes[3].table.columns[0]
# when ====================================================
@when("I assign cell.margin_{side} = {value}")
def when_I_assign_cell_margin_side_eq_value(context, value, side):
setattr(context.cell, "margin_%s" % side, eval(value))
@when('I assign cell.text = "test text"')
def when_I_assign_cell_text(context):
context.cell.text = "test text"
@when("I assign cell.vertical_anchor = {value}")
def when_I_assign_cell_vertical_anchor_eq_value(context, value):
context.cell.vertical_anchor = eval(value)
@when("I assign column.width = {value}")
def when_I_assign_column_width_eq_value(context, value):
context.column.width = eval(value)
@when("I assign origin_cell = table.cell(0, 0)")
def when_I_assign_origin_cell_eq_table_cell_0_0(context):
context.origin_cell = context.table_.cell(0, 0)
@when("I assign other_cell = table.cell(1, 1)")
def when_I_assign_other_cell_eq_table_cell_1_1(context):
context.other_cell = context.table_.cell(1, 1)
@when("I assign table.first_col = True")
def when_I_assign_table_first_col_eq_True(context):
context.table_.first_col = True
@when("I assign table.first_row = True")
def when_I_assign_table_first_row_eq_True(context):
context.table_.first_row = True
@when("I assign table.horz_banding = True")
def when_I_assign_table_horz_banding_eq_True(context):
context.table_.horz_banding = True
@when("I assign table.last_col = True")
def when_I_assign_table_last_col_eq_True(context):
context.table_.last_col = True
@when("I assign table.last_row = True")
def when_I_assign_table_last_row_eq_True(context):
context.table_.last_row = True
@when("I assign table.vert_banding = True")
def when_I_assign_table_vert_banding_eq_True(context):
context.table_.vert_banding = True
@when("I call cell.split()")
def when_I_call_cell_split_other_cell(context):
context.cell.split()
@when("I call origin_cell.merge(other_cell)")
def when_I_call_origin_cell_merge_other_cell(context):
context.origin_cell.merge(context.other_cell)
# then ====================================================
@then("cell == other_cell")
def then_cell_eq_other_cell(context):
cell, other_cell = context.cell, context.other_cell
assert cell == other_cell, "cell != other_cell"
@then("cell.fill is a FillFormat object")
def then_cell_fill_is_a_FillFormat_object(context):
actual = type(context.cell.fill).__name__
expected = "FillFormat"
assert actual == expected, "cell.fill is a %s object" % actual
@then("cell.margin_{side} == Inches({num_lit})")
def then_cell_margin_side_eq_Inches_num(context, side, num_lit):
actual = getattr(context.cell, "margin_%s" % side)
expected = Inches(float(num_lit))
assert actual == expected, "cell.margin_%s == %s" % (side, actual.inches)
@then("{cell_ref}.is_merge_origin is {bool_lit}")
def then_cell_ref_is_merge_origin_is(context, cell_ref, bool_lit):
expected = eval(bool_lit)
actual = getattr(context, cell_ref).is_merge_origin
assert actual is expected, "%s.is_merge_origin is %s" % (cell_ref, actual)
@then("{cell_ref}.is_spanned is {bool_lit}")
def then_cell_is_spanned_is(context, cell_ref, bool_lit):
expected = eval(bool_lit)
actual = getattr(context, cell_ref).is_spanned
assert actual is expected, "%s.is_spanned is %s" % (cell_ref, actual)
@then("cell.text == {value}")
def then_cell_text_eq_value(context, value):
actual, expected = context.cell.text, eval(value)
assert actual == expected, 'cell.text == "%s"' % actual
@then("cell.span_height == {int_lit}")
def then_cell_span_height_eq(context, int_lit):
expected = int(int_lit)
actual = context.cell.span_height
assert actual is expected, "cell.span_height == %s" % actual
@then("cell.span_width == {int_lit}")
def then_cell_span_width_eq(context, int_lit):
expected = int(int_lit)
actual = context.cell.span_width
assert actual is expected, "cell.span_width == %s" % actual
@then("cell.vertical_anchor == {value}")
def then_cell_vertical_anchor_eq_value(context, value):
actual = context.cell.vertical_anchor
expected = eval(value)
assert actual == expected, "cell.vertical_anchor == %s" % actual
@then("column.width.inches == {float_lit}")
def then_column_width_inches_eq(context, float_lit):
actual = context.column.width.inches
expected = float(float_lit)
assert actual == expected, "column.width.inches == %s" % actual
@then("len(list(table.iter_cells())) == {int_lit}")
def then_len_list_table_iter_cells_eq(context, int_lit):
actual = len(list(context.table_.iter_cells()))
expected = int(int_lit)
assert actual is expected, "len(list(table.iter_cells())) == %s" % actual
@then("origin_cell.text == {value}")
def then_origin_cell_text_eq_value(context, value):
actual, expected = context.origin_cell.text, eval(value)
assert actual == expected, 'origin_cell.text == "%s"' % actual
@then("other_cell.text == {value}")
def then_other_cell_text_eq_value(context, value):
actual, expected = context.other_cell.text, eval(value)
assert actual == expected, 'other_cell.text == "%s"' % actual
@then("table.cell(0, 0) is a {type_name} object")
def then_table_cell_0_0_is_a_type_object(context, type_name):
actual = type(context.table_.cell(0, 0)).__name__
expected = type_name
assert actual == expected, "table.cell(0, 0) is a %s object" % actual
@then("table.columns is a {type_name} object")
def then_table_columns_is_a_type_object(context, type_name):
actual = type(context.table_.columns).__name__
expected = type_name
assert actual == expected, "table.columns is a %s object" % actual
@then("table.first_col is {bool_lit}")
def then_table_first_col_is_value(context, bool_lit):
actual = context.table_.first_col
expected = eval(bool_lit)
assert actual is expected, "table.first_col is %s" % actual
@then("table.first_row is {bool_lit}")
def then_table_first_row_is_value(context, bool_lit):
actual = context.table_.first_row
expected = eval(bool_lit)
assert actual is expected, "table.first_row is %s" % actual
@then("table.horz_banding is {bool_lit}")
def then_table_horz_banding_is_value(context, bool_lit):
actual = context.table_.horz_banding
expected = eval(bool_lit)
assert actual is expected, "table.horz_banding is %s" % actual
@then("table.last_col is {bool_lit}")
def then_table_last_col_is_value(context, bool_lit):
actual = context.table_.last_col
expected = eval(bool_lit)
assert actual is expected, "table.last_col is %s" % actual
@then("table.last_row is {bool_lit}")
def then_table_last_row_is_value(context, bool_lit):
actual = context.table_.last_row
expected = eval(bool_lit)
assert actual is expected, "table.last_row is %s" % actual
@then("table.rows is a {type_name} object")
def then_table_rows_is_a_type_object(context, type_name):
actual = type(context.table_.rows).__name__
expected = type_name
assert actual == expected, "table.rows is a %s object" % actual
@then("table.vert_banding is {bool_lit}")
def then_table_vert_banding_is_value(context, bool_lit):
actual = context.table_.vert_banding
expected = eval(bool_lit)
assert actual is expected, "table.vert_banding is %s" % actual