Skip to content

Commit e77bbdd

Browse files
author
Steve Canny
committed
oxml: convert CT_OnOff to xmlchemy
1 parent 22af2b0 commit e77bbdd

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

docx/oxml/shared.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
from __future__ import absolute_import
88

99
from . import OxmlElement
10-
from .exceptions import InvalidXmlError
1110
from .ns import qn
12-
from .simpletypes import ST_DecimalNumber
13-
from .xmlchemy import BaseOxmlElement, RequiredAttribute
11+
from .simpletypes import ST_DecimalNumber, ST_OnOff
12+
from .xmlchemy import BaseOxmlElement, OptionalAttribute, RequiredAttribute
1413

1514

1615
class CT_DecimalNumber(BaseOxmlElement):
@@ -35,25 +34,7 @@ class CT_OnOff(BaseOxmlElement):
3534
Used for ``<w:b>``, ``<w:i>`` elements and others, containing a bool-ish
3635
string in its ``val`` attribute, xsd:boolean plus 'on' and 'off'.
3736
"""
38-
@property
39-
def val(self):
40-
val = self.get(qn('w:val'))
41-
if val is None:
42-
return True
43-
elif val in ('0', 'false', 'off'):
44-
return False
45-
elif val in ('1', 'true', 'on'):
46-
return True
47-
raise InvalidXmlError("expected xsd:boolean, got '%s'" % val)
48-
49-
@val.setter
50-
def val(self, value):
51-
val = qn('w:val')
52-
if bool(value) is True:
53-
if val in self.attrib:
54-
del self.attrib[val]
55-
else:
56-
self.set(val, '0')
37+
val = OptionalAttribute('w:val', ST_OnOff, default=True)
5738

5839

5940
class CT_String(BaseOxmlElement):

docx/oxml/simpletypes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ class ST_DecimalNumber(XsdInt):
177177
pass
178178

179179

180+
class ST_OnOff(XsdBoolean):
181+
182+
@classmethod
183+
def convert_from_xml(cls, str_value):
184+
return str_value in ('1', 'true', 'on')
185+
186+
180187
class ST_PositiveCoordinate(XsdLong):
181188

182189
@classmethod

0 commit comments

Comments
 (0)