|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +""" |
| 4 | +Step implementations for paragraph format-related features. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import ( |
| 8 | + absolute_import, division, print_function, unicode_literals |
| 9 | +) |
| 10 | + |
| 11 | +from behave import given, then, when |
| 12 | + |
| 13 | +from docx import Document |
| 14 | +from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_LINE_SPACING |
| 15 | +from docx.shared import Pt |
| 16 | + |
| 17 | +from helpers import test_docx |
| 18 | + |
| 19 | + |
| 20 | +# given =================================================== |
| 21 | + |
| 22 | +@given('a paragraph format having {prop_name} set {setting}') |
| 23 | +def given_a_paragraph_format_having_prop_set(context, prop_name, setting): |
| 24 | + style_name = { |
| 25 | + 'to inherit': 'Normal', |
| 26 | + 'On': 'Base', |
| 27 | + 'Off': 'Citation', |
| 28 | + }[setting] |
| 29 | + document = Document(test_docx('sty-known-styles')) |
| 30 | + context.paragraph_format = document.styles[style_name].paragraph_format |
| 31 | + |
| 32 | + |
| 33 | +@given('a paragraph format having {setting} line spacing') |
| 34 | +def given_a_paragraph_format_having_setting_line_spacing(context, setting): |
| 35 | + style_name = { |
| 36 | + 'inherited': 'Normal', |
| 37 | + '14 pt': 'Base', |
| 38 | + 'double': 'Citation', |
| 39 | + }[setting] |
| 40 | + document = Document(test_docx('sty-known-styles')) |
| 41 | + context.paragraph_format = document.styles[style_name].paragraph_format |
| 42 | + |
| 43 | + |
| 44 | +@given('a paragraph format having {setting} space {side}') |
| 45 | +def given_a_paragraph_format_having_setting_spacing(context, setting, side): |
| 46 | + style_name = 'Normal' if setting == 'inherited' else 'Base' |
| 47 | + document = Document(test_docx('sty-known-styles')) |
| 48 | + context.paragraph_format = document.styles[style_name].paragraph_format |
| 49 | + |
| 50 | + |
| 51 | +@given('a paragraph format having {type} alignment') |
| 52 | +def given_a_paragraph_format_having_align_type_alignment(context, type): |
| 53 | + style_name = { |
| 54 | + 'inherited': 'Normal', |
| 55 | + 'center': 'Base', |
| 56 | + 'right': 'Citation', |
| 57 | + }[type] |
| 58 | + document = Document(test_docx('sty-known-styles')) |
| 59 | + context.paragraph_format = document.styles[style_name].paragraph_format |
| 60 | + |
| 61 | + |
| 62 | +@given('a paragraph format having {type} indent of {value}') |
| 63 | +def given_a_paragraph_format_having_type_indent_value(context, type, value): |
| 64 | + style_name = { |
| 65 | + 'inherit': 'Normal', |
| 66 | + '18 pt': 'Base', |
| 67 | + '17.3 pt': 'Base', |
| 68 | + '-17.3 pt': 'Citation', |
| 69 | + '46.1 pt': 'Citation', |
| 70 | + }[value] |
| 71 | + document = Document(test_docx('sty-known-styles')) |
| 72 | + context.paragraph_format = document.styles[style_name].paragraph_format |
| 73 | + |
| 74 | + |
| 75 | +# when ==================================================== |
| 76 | + |
| 77 | +@when('I assign {value} to paragraph_format.line_spacing') |
| 78 | +def when_I_assign_value_to_paragraph_format_line_spacing(context, value): |
| 79 | + new_value = { |
| 80 | + 'Pt(14)': Pt(14), |
| 81 | + '2': 2, |
| 82 | + }.get(value) |
| 83 | + new_value = float(value) if new_value is None else new_value |
| 84 | + context.paragraph_format.line_spacing = new_value |
| 85 | + |
| 86 | + |
| 87 | +@when('I assign {value} to paragraph_format.line_spacing_rule') |
| 88 | +def when_I_assign_value_to_paragraph_format_line_rule(context, value): |
| 89 | + new_value = { |
| 90 | + 'None': None, |
| 91 | + 'WD_LINE_SPACING.EXACTLY': WD_LINE_SPACING.EXACTLY, |
| 92 | + 'WD_LINE_SPACING.MULTIPLE': WD_LINE_SPACING.MULTIPLE, |
| 93 | + 'WD_LINE_SPACING.SINGLE': WD_LINE_SPACING.SINGLE, |
| 94 | + 'WD_LINE_SPACING.DOUBLE': WD_LINE_SPACING.DOUBLE, |
| 95 | + 'WD_LINE_SPACING.AT_LEAST': WD_LINE_SPACING.AT_LEAST, |
| 96 | + 'WD_LINE_SPACING.ONE_POINT_FIVE': WD_LINE_SPACING.ONE_POINT_FIVE, |
| 97 | + }[value] |
| 98 | + paragraph_format = context.paragraph_format |
| 99 | + paragraph_format.line_spacing_rule = new_value |
| 100 | + |
| 101 | + |
| 102 | +@when('I assign {value} to paragraph_format.alignment') |
| 103 | +def when_I_assign_value_to_paragraph_format_alignment(context, value): |
| 104 | + new_value = { |
| 105 | + 'None': None, |
| 106 | + 'WD_ALIGN_PARAGRAPH.CENTER': WD_ALIGN_PARAGRAPH.CENTER, |
| 107 | + 'WD_ALIGN_PARAGRAPH.RIGHT': WD_ALIGN_PARAGRAPH.RIGHT, |
| 108 | + }[value] |
| 109 | + paragraph_format = context.paragraph_format |
| 110 | + paragraph_format.alignment = new_value |
| 111 | + |
| 112 | + |
| 113 | +@when('I assign {value} to paragraph_format.space_{side}') |
| 114 | +def when_I_assign_value_to_paragraph_format_space(context, value, side): |
| 115 | + paragraph_format = context.paragraph_format |
| 116 | + prop_name = 'space_%s' % side |
| 117 | + new_value = { |
| 118 | + 'None': None, |
| 119 | + 'Pt(12)': Pt(12), |
| 120 | + 'Pt(18)': Pt(18), |
| 121 | + }[value] |
| 122 | + setattr(paragraph_format, prop_name, new_value) |
| 123 | + |
| 124 | + |
| 125 | +@when('I assign {value} to paragraph_format.{type_}_indent') |
| 126 | +def when_I_assign_value_to_paragraph_format_indent(context, value, type_): |
| 127 | + paragraph_format = context.paragraph_format |
| 128 | + prop_name = '%s_indent' % type_ |
| 129 | + value = None if value == 'None' else Pt(float(value.split()[0])) |
| 130 | + setattr(paragraph_format, prop_name, value) |
| 131 | + |
| 132 | + |
| 133 | +@when('I assign {value} to paragraph_format.{prop_name}') |
| 134 | +def when_I_assign_value_to_paragraph_format_prop(context, value, prop_name): |
| 135 | + paragraph_format = context.paragraph_format |
| 136 | + value = {'None': None, 'True': True, 'False': False}[value] |
| 137 | + setattr(paragraph_format, prop_name, value) |
| 138 | + |
| 139 | + |
| 140 | +# then ===================================================== |
| 141 | + |
| 142 | +@then('paragraph_format.alignment is {value}') |
| 143 | +def then_paragraph_format_alignment_is_value(context, value): |
| 144 | + expected_value = { |
| 145 | + 'None': None, |
| 146 | + 'WD_ALIGN_PARAGRAPH.LEFT': WD_ALIGN_PARAGRAPH.LEFT, |
| 147 | + 'WD_ALIGN_PARAGRAPH.CENTER': WD_ALIGN_PARAGRAPH.CENTER, |
| 148 | + 'WD_ALIGN_PARAGRAPH.RIGHT': WD_ALIGN_PARAGRAPH.RIGHT, |
| 149 | + }[value] |
| 150 | + paragraph_format = context.paragraph_format |
| 151 | + assert paragraph_format.alignment == expected_value |
| 152 | + |
| 153 | + |
| 154 | +@then('paragraph_format.line_spacing is {value}') |
| 155 | +def then_paragraph_format_line_spacing_is_value(context, value): |
| 156 | + expected_value = ( |
| 157 | + None if value == 'None' else |
| 158 | + float(value) if '.' in value else |
| 159 | + int(value) |
| 160 | + ) |
| 161 | + paragraph_format = context.paragraph_format |
| 162 | + |
| 163 | + if expected_value is None or isinstance(expected_value, int): |
| 164 | + assert paragraph_format.line_spacing == expected_value |
| 165 | + else: |
| 166 | + assert abs(paragraph_format.line_spacing - expected_value) < 0.001 |
| 167 | + |
| 168 | + |
| 169 | +@then('paragraph_format.line_spacing_rule is {value}') |
| 170 | +def then_paragraph_format_line_spacing_rule_is_value(context, value): |
| 171 | + expected_value = { |
| 172 | + 'None': None, |
| 173 | + 'WD_LINE_SPACING.EXACTLY': WD_LINE_SPACING.EXACTLY, |
| 174 | + 'WD_LINE_SPACING.MULTIPLE': WD_LINE_SPACING.MULTIPLE, |
| 175 | + 'WD_LINE_SPACING.SINGLE': WD_LINE_SPACING.SINGLE, |
| 176 | + 'WD_LINE_SPACING.DOUBLE': WD_LINE_SPACING.DOUBLE, |
| 177 | + 'WD_LINE_SPACING.AT_LEAST': WD_LINE_SPACING.AT_LEAST, |
| 178 | + 'WD_LINE_SPACING.ONE_POINT_FIVE': WD_LINE_SPACING.ONE_POINT_FIVE, |
| 179 | + }[value] |
| 180 | + paragraph_format = context.paragraph_format |
| 181 | + assert paragraph_format.line_spacing_rule == expected_value |
| 182 | + |
| 183 | + |
| 184 | +@then('paragraph_format.space_{side} is {value}') |
| 185 | +def then_paragraph_format_space_side_is_value(context, side, value): |
| 186 | + expected_value = None if value == 'None' else int(value) |
| 187 | + prop_name = 'space_%s' % side |
| 188 | + paragraph_format = context.paragraph_format |
| 189 | + actual_value = getattr(paragraph_format, prop_name) |
| 190 | + assert actual_value == expected_value |
| 191 | + |
| 192 | + |
| 193 | +@then('paragraph_format.{type_}_indent is {value}') |
| 194 | +def then_paragraph_format_type_indent_is_value(context, type_, value): |
| 195 | + expected_value = None if value == 'None' else int(value) |
| 196 | + prop_name = '%s_indent' % type_ |
| 197 | + paragraph_format = context.paragraph_format |
| 198 | + actual_value = getattr(paragraph_format, prop_name) |
| 199 | + assert actual_value == expected_value |
| 200 | + |
| 201 | + |
| 202 | +@then('paragraph_format.{prop_name} is {value}') |
| 203 | +def then_paragraph_format_prop_name_is_value(context, prop_name, value): |
| 204 | + expected_value = {'None': None, 'True': True, 'False': False}[value] |
| 205 | + paragraph_format = context.paragraph_format |
| 206 | + actual_value = getattr(paragraph_format, prop_name) |
| 207 | + assert actual_value == expected_value |
0 commit comments