Skip to content

Commit 208a9cf

Browse files
author
Steve Canny
committed
run: remove direct access to Run bool props
Access via Run.font instead.
1 parent 19a552e commit 208a9cf

File tree

2 files changed

+19
-267
lines changed

2 files changed

+19
-267
lines changed

docx/text/run.py

Lines changed: 0 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,6 @@
1010
from ..shared import ElementProxy, Parented
1111

1212

13-
def boolproperty(f):
14-
"""
15-
@boolproperty decorator. Decorated method must return the XML element
16-
name of the boolean property element occuring under rPr. Causes
17-
a read/write tri-state property to be added to the class having the name
18-
of the decorated function.
19-
"""
20-
def _get_prop_value(parent, attr_name):
21-
return getattr(parent, attr_name)
22-
23-
def _remove_prop(parent, attr_name):
24-
remove_method_name = '_remove_%s' % attr_name
25-
remove_method = getattr(parent, remove_method_name)
26-
remove_method()
27-
28-
def _add_prop(parent, attr_name):
29-
add_method_name = '_add_%s' % attr_name
30-
add_method = getattr(parent, add_method_name)
31-
return add_method()
32-
33-
def getter(obj):
34-
r, attr_name = obj._r, f(obj)
35-
if r.rPr is None:
36-
return None
37-
prop_value = _get_prop_value(r.rPr, attr_name)
38-
if prop_value is None:
39-
return None
40-
return prop_value.val
41-
42-
def setter(obj, value):
43-
if value not in (True, False, None):
44-
raise ValueError(
45-
"assigned value must be True, False, or None, got '%s'"
46-
% value
47-
)
48-
r, attr_name = obj._r, f(obj)
49-
rPr = r.get_or_add_rPr()
50-
_remove_prop(rPr, attr_name)
51-
if value is not None:
52-
elm = _add_prop(rPr, attr_name)
53-
elm.val = value
54-
55-
return property(getter, setter, doc=f.__doc__)
56-
57-
5813
class Run(Parented):
5914
"""
6015
Proxy object wrapping ``<w:r>`` element. Several of the properties on Run
@@ -136,13 +91,6 @@ def add_text(self, text):
13691
t = self._r.add_t(text)
13792
return _Text(t)
13893

139-
@boolproperty
140-
def all_caps(self):
141-
"""
142-
Read/write. Causes the text of the run to appear in capital letters.
143-
"""
144-
return 'caps'
145-
14694
@property
14795
def bold(self):
14896
"""
@@ -162,47 +110,6 @@ def clear(self):
162110
self._r.clear_content()
163111
return self
164112

165-
@boolproperty
166-
def complex_script(self):
167-
"""
168-
Read/write tri-state value. When |True|, causes the characters in the
169-
run to be treated as complex script regardless of their Unicode
170-
values.
171-
"""
172-
return 'cs'
173-
174-
@boolproperty
175-
def cs_bold(self):
176-
"""
177-
Read/write tri-state value. When |True|, causes the complex script
178-
characters in the run to be displayed in bold typeface.
179-
"""
180-
return 'bCs'
181-
182-
@boolproperty
183-
def cs_italic(self):
184-
"""
185-
Read/write tri-state value. When |True|, causes the complex script
186-
characters in the run to be displayed in italic typeface.
187-
"""
188-
return 'iCs'
189-
190-
@boolproperty
191-
def double_strike(self):
192-
"""
193-
Read/write tri-state value. When |True|, causes the text in the run
194-
to appear with double strikethrough.
195-
"""
196-
return 'dstrike'
197-
198-
@boolproperty
199-
def emboss(self):
200-
"""
201-
Read/write tri-state value. When |True|, causes the text in the run
202-
to appear as if raised off the page in relief.
203-
"""
204-
return 'emboss'
205-
206113
@property
207114
def font(self):
208115
"""
@@ -211,15 +118,6 @@ def font(self):
211118
"""
212119
return Font(self._element)
213120

214-
@boolproperty
215-
def hidden(self):
216-
"""
217-
Read/write tri-state value. When |True|, causes the text in the run
218-
to be hidden from display, unless applications settings force hidden
219-
text to be shown.
220-
"""
221-
return 'vanish'
222-
223121
@property
224122
def italic(self):
225123
"""
@@ -232,94 +130,6 @@ def italic(self):
232130
def italic(self, value):
233131
self.font.italic = value
234132

235-
@boolproperty
236-
def imprint(self):
237-
"""
238-
Read/write tri-state value. When |True|, causes the text in the run
239-
to appear as if pressed into the page.
240-
"""
241-
return 'imprint'
242-
243-
@boolproperty
244-
def math(self):
245-
"""
246-
Read/write tri-state value. When |True|, specifies this run contains
247-
WML that should be handled as though it was Office Open XML Math.
248-
"""
249-
return 'oMath'
250-
251-
@boolproperty
252-
def no_proof(self):
253-
"""
254-
Read/write tri-state value. When |True|, specifies that the contents
255-
of this run should not report any errors when the document is scanned
256-
for spelling and grammar.
257-
"""
258-
return 'noProof'
259-
260-
@boolproperty
261-
def outline(self):
262-
"""
263-
Read/write tri-state value. When |True| causes the characters in the
264-
run to appear as if they have an outline, by drawing a one pixel wide
265-
border around the inside and outside borders of each character glyph.
266-
"""
267-
return 'outline'
268-
269-
@boolproperty
270-
def rtl(self):
271-
"""
272-
Read/write tri-state value. When |True| causes the text in the run
273-
to have right-to-left characteristics.
274-
"""
275-
return 'rtl'
276-
277-
@boolproperty
278-
def shadow(self):
279-
"""
280-
Read/write tri-state value. When |True| causes the text in the run
281-
to appear as if each character has a shadow.
282-
"""
283-
return 'shadow'
284-
285-
@boolproperty
286-
def small_caps(self):
287-
"""
288-
Read/write tri-state value. When |True| causes the lowercase
289-
characters in the run to appear as capital letters two points smaller
290-
than the font size specified for the run.
291-
"""
292-
return 'smallCaps'
293-
294-
@boolproperty
295-
def snap_to_grid(self):
296-
"""
297-
Read/write tri-state value. When |True| causes the run to use the
298-
document grid characters per line settings defined in the docGrid
299-
element when laying out the characters in this run.
300-
"""
301-
return 'snapToGrid'
302-
303-
@boolproperty
304-
def spec_vanish(self):
305-
"""
306-
Read/write tri-state value. When |True|, specifies that the given run
307-
shall always behave as if it is hidden, even when hidden text is
308-
being displayed in the current document. The property has a very
309-
narrow, specialized use related to the table of contents. Consult the
310-
spec (§17.3.2.36) for more details.
311-
"""
312-
return 'specVanish'
313-
314-
@boolproperty
315-
def strike(self):
316-
"""
317-
Read/write tri-state value. When |True| causes the text in the run
318-
to appear with a single horizontal line through the center of the
319-
line.
320-
"""
321-
return 'strike'
322-
323133
@property
324134
def style(self):
325135
"""
@@ -378,15 +188,6 @@ def underline(self):
378188
def underline(self, value):
379189
self.font.underline = value
380190

381-
@boolproperty
382-
def web_hidden(self):
383-
"""
384-
Read/write tri-state value. When |True|, specifies that the contents
385-
of this run shall be hidden when the document is displayed in web
386-
page view.
387-
"""
388-
return 'webHidden'
389-
390191

391192
class Font(ElementProxy):
392193
"""

tests/text/test_run.py

Lines changed: 19 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -163,30 +163,12 @@ def add_text_fixture(self, request, Text_):
163163
return run, text, expected_xml, Text_
164164

165165
@pytest.fixture(params=[
166-
('w:r/w:rPr', 'all_caps', None),
167-
('w:r/w:rPr/w:caps', 'all_caps', True),
168-
('w:r/w:rPr/w:caps{w:val=on}', 'all_caps', True),
169-
('w:r/w:rPr/w:caps{w:val=off}', 'all_caps', False),
170-
('w:r/w:rPr/w:b{w:val=1}', 'bold', True),
171-
('w:r/w:rPr/w:i{w:val=0}', 'italic', False),
172-
('w:r/w:rPr/w:cs{w:val=true}', 'complex_script', True),
173-
('w:r/w:rPr/w:bCs{w:val=false}', 'cs_bold', False),
174-
('w:r/w:rPr/w:iCs{w:val=on}', 'cs_italic', True),
175-
('w:r/w:rPr/w:dstrike{w:val=off}', 'double_strike', False),
176-
('w:r/w:rPr/w:emboss{w:val=1}', 'emboss', True),
177-
('w:r/w:rPr/w:vanish{w:val=0}', 'hidden', False),
178-
('w:r/w:rPr/w:i{w:val=true}', 'italic', True),
179-
('w:r/w:rPr/w:imprint{w:val=false}', 'imprint', False),
180-
('w:r/w:rPr/w:oMath{w:val=on}', 'math', True),
181-
('w:r/w:rPr/w:noProof{w:val=off}', 'no_proof', False),
182-
('w:r/w:rPr/w:outline{w:val=1}', 'outline', True),
183-
('w:r/w:rPr/w:rtl{w:val=0}', 'rtl', False),
184-
('w:r/w:rPr/w:shadow{w:val=true}', 'shadow', True),
185-
('w:r/w:rPr/w:smallCaps{w:val=false}', 'small_caps', False),
186-
('w:r/w:rPr/w:snapToGrid{w:val=on}', 'snap_to_grid', True),
187-
('w:r/w:rPr/w:specVanish{w:val=off}', 'spec_vanish', False),
188-
('w:r/w:rPr/w:strike{w:val=1}', 'strike', True),
189-
('w:r/w:rPr/w:webHidden{w:val=0}', 'web_hidden', False),
166+
('w:r/w:rPr', 'bold', None),
167+
('w:r/w:rPr/w:b', 'bold', True),
168+
('w:r/w:rPr/w:b{w:val=on}', 'bold', True),
169+
('w:r/w:rPr/w:b{w:val=off}', 'bold', False),
170+
('w:r/w:rPr/w:b{w:val=1}', 'bold', True),
171+
('w:r/w:rPr/w:i{w:val=0}', 'italic', False),
190172
])
191173
def bool_prop_get_fixture(self, request):
192174
r_cxml, bool_prop_name, expected_value = request.param
@@ -195,52 +177,22 @@ def bool_prop_get_fixture(self, request):
195177

196178
@pytest.fixture(params=[
197179
# nothing to True, False, and None ---------------------------
198-
('w:r', 'all_caps', True,
199-
'w:r/w:rPr/w:caps'),
200-
('w:r', 'bold', False,
201-
'w:r/w:rPr/w:b{w:val=0}'),
202-
('w:r', 'italic', None,
203-
'w:r/w:rPr'),
180+
('w:r', 'bold', True, 'w:r/w:rPr/w:b'),
181+
('w:r', 'bold', False, 'w:r/w:rPr/w:b{w:val=0}'),
182+
('w:r', 'italic', None, 'w:r/w:rPr'),
204183
# default to True, False, and None ---------------------------
205-
('w:r/w:rPr/w:cs', 'complex_script', True,
206-
'w:r/w:rPr/w:cs'),
207-
('w:r/w:rPr/w:bCs', 'cs_bold', False,
208-
'w:r/w:rPr/w:bCs{w:val=0}'),
209-
('w:r/w:rPr/w:iCs', 'cs_italic', None,
210-
'w:r/w:rPr'),
184+
('w:r/w:rPr/w:b', 'bold', True, 'w:r/w:rPr/w:b'),
185+
('w:r/w:rPr/w:b', 'bold', False, 'w:r/w:rPr/w:b{w:val=0}'),
186+
('w:r/w:rPr/w:i', 'italic', None, 'w:r/w:rPr'),
211187
# True to True, False, and None ------------------------------
212-
('w:r/w:rPr/w:dstrike{w:val=1}', 'double_strike', True,
213-
'w:r/w:rPr/w:dstrike'),
214-
('w:r/w:rPr/w:emboss{w:val=on}', 'emboss', False,
215-
'w:r/w:rPr/w:emboss{w:val=0}'),
216-
('w:r/w:rPr/w:vanish{w:val=1}', 'hidden', None,
217-
'w:r/w:rPr'),
188+
('w:r/w:rPr/w:b{w:val=on}', 'bold', True, 'w:r/w:rPr/w:b'),
189+
('w:r/w:rPr/w:b{w:val=1}', 'bold', False, 'w:r/w:rPr/w:b{w:val=0}'),
190+
('w:r/w:rPr/w:b{w:val=1}', 'bold', None, 'w:r/w:rPr'),
218191
# False to True, False, and None -----------------------------
219-
('w:r/w:rPr/w:i{w:val=false}', 'italic', True,
220-
'w:r/w:rPr/w:i'),
221-
('w:r/w:rPr/w:imprint{w:val=0}', 'imprint', False,
222-
'w:r/w:rPr/w:imprint{w:val=0}'),
223-
('w:r/w:rPr/w:oMath{w:val=off}', 'math', None,
224-
'w:r/w:rPr'),
225-
# random mix -------------------------------------------------
226-
('w:r/w:rPr/w:noProof{w:val=1}', 'no_proof', False,
227-
'w:r/w:rPr/w:noProof{w:val=0}'),
228-
('w:r/w:rPr', 'outline', True,
229-
'w:r/w:rPr/w:outline'),
230-
('w:r/w:rPr/w:rtl{w:val=true}', 'rtl', False,
231-
'w:r/w:rPr/w:rtl{w:val=0}'),
232-
('w:r/w:rPr/w:shadow{w:val=on}', 'shadow', True,
233-
'w:r/w:rPr/w:shadow'),
234-
('w:r/w:rPr/w:smallCaps', 'small_caps', False,
235-
'w:r/w:rPr/w:smallCaps{w:val=0}'),
236-
('w:r/w:rPr/w:snapToGrid', 'snap_to_grid', True,
237-
'w:r/w:rPr/w:snapToGrid'),
238-
('w:r/w:rPr/w:specVanish', 'spec_vanish', None,
239-
'w:r/w:rPr'),
240-
('w:r/w:rPr/w:strike{w:val=foo}', 'strike', True,
241-
'w:r/w:rPr/w:strike'),
242-
('w:r/w:rPr/w:webHidden', 'web_hidden', False,
243-
'w:r/w:rPr/w:webHidden{w:val=0}'),
192+
('w:r/w:rPr/w:i{w:val=false}', 'italic', True, 'w:r/w:rPr/w:i'),
193+
('w:r/w:rPr/w:i{w:val=0}', 'italic', False,
194+
'w:r/w:rPr/w:i{w:val=0}'),
195+
('w:r/w:rPr/w:i{w:val=off}', 'italic', None, 'w:r/w:rPr'),
244196
])
245197
def bool_prop_set_fixture(self, request):
246198
initial_r_cxml, bool_prop_name, value, expected_cxml = request.param
@@ -397,7 +349,6 @@ def it_knows_its_typeface_name(self, name_get_fixture):
397349
def it_can_change_its_typeface_name(self, name_set_fixture):
398350
font, value, expected_xml = name_set_fixture
399351
font.name = value
400-
print(font._element.xml)
401352
assert font._element.xml == expected_xml
402353

403354
def it_knows_its_size(self, size_get_fixture):

0 commit comments

Comments
 (0)