From a46dd645f94ad8b3c53878d3dae1a0510661317b Mon Sep 17 00:00:00 2001 From: Steve Canny Date: Fri, 4 Jan 2019 23:36:30 -0800 Subject: [PATCH] hdr: add Settings.odd_and_even_pages_header_footer Getter side. --- docx/oxml/__init__.py | 4 +++ docx/oxml/settings.py | 57 +++++++++++++++++++++++++++++++++++ docx/settings.py | 8 +++++ features/doc-settings.feature | 1 - tests/test_settings.py | 40 ++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 docx/oxml/settings.py create mode 100644 tests/test_settings.py diff --git a/docx/oxml/__init__.py b/docx/oxml/__init__.py index 1d88f5927..d1c01c6f6 100644 --- a/docx/oxml/__init__.py +++ b/docx/oxml/__init__.py @@ -65,6 +65,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None): # =========================================================================== from .shared import CT_DecimalNumber, CT_OnOff, CT_String # noqa +register_element_cls("w:evenAndOddHeaders", CT_OnOff) from .coreprops import CT_CoreProperties # noqa @@ -101,6 +102,9 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None): register_element_cls("w:sectPr", CT_SectPr) register_element_cls("w:type", CT_SectType) +from .settings import CT_Settings # noqa +register_element_cls("w:settings", CT_Settings) + from .shape import ( # noqa CT_Blip, CT_BlipFillProperties, diff --git a/docx/oxml/settings.py b/docx/oxml/settings.py new file mode 100644 index 000000000..fbd262f1a --- /dev/null +++ b/docx/oxml/settings.py @@ -0,0 +1,57 @@ +# encoding: utf-8 + +"""Custom element classes related to document settings""" + +from __future__ import absolute_import, division, print_function, unicode_literals + +from docx.oxml.xmlchemy import BaseOxmlElement, ZeroOrOne + + +class CT_Settings(BaseOxmlElement): + """`w:settings` element, root element for the settings part""" + + _tag_seq = ( + "w:writeProtection", "w:view", "w:zoom", "w:removePersonalInformation", + "w:removeDateAndTime", "w:doNotDisplayPageBoundaries", + "w:displayBackgroundShape", "w:printPostScriptOverText", + "w:printFractionalCharacterWidth", "w:printFormsData", "w:embedTrueTypeFonts", + "w:embedSystemFonts", "w:saveSubsetFonts", "w:saveFormsData", "w:mirrorMargins", + "w:alignBordersAndEdges", "w:bordersDoNotSurroundHeader", + "w:bordersDoNotSurroundFooter", "w:gutterAtTop", "w:hideSpellingErrors", + "w:hideGrammaticalErrors", "w:activeWritingStyle", "w:proofState", + "w:formsDesign", "w:attachedTemplate", "w:linkStyles", + "w:stylePaneFormatFilter", "w:stylePaneSortMethod", "w:documentType", + "w:mailMerge", "w:revisionView", "w:trackRevisions", "w:doNotTrackMoves", + "w:doNotTrackFormatting", "w:documentProtection", "w:autoFormatOverride", + "w:styleLockTheme", "w:styleLockQFSet", "w:defaultTabStop", "w:autoHyphenation", + "w:consecutiveHyphenLimit", "w:hyphenationZone", "w:doNotHyphenateCaps", + "w:showEnvelope", "w:summaryLength", "w:clickAndTypeStyle", + "w:defaultTableStyle", "w:evenAndOddHeaders", "w:bookFoldRevPrinting", + "w:bookFoldPrinting", "w:bookFoldPrintingSheets", + "w:drawingGridHorizontalSpacing", "w:drawingGridVerticalSpacing", + "w:displayHorizontalDrawingGridEvery", "w:displayVerticalDrawingGridEvery", + "w:doNotUseMarginsForDrawingGridOrigin", "w:drawingGridHorizontalOrigin", + "w:drawingGridVerticalOrigin", "w:doNotShadeFormData", "w:noPunctuationKerning", + "w:characterSpacingControl", "w:printTwoOnOne", "w:strictFirstAndLastChars", + "w:noLineBreaksAfter", "w:noLineBreaksBefore", "w:savePreviewPicture", + "w:doNotValidateAgainstSchema", "w:saveInvalidXml", "w:ignoreMixedContent", + "w:alwaysShowPlaceholderText", "w:doNotDemarcateInvalidXml", + "w:saveXmlDataOnly", "w:useXSLTWhenSaving", "w:saveThroughXslt", + "w:showXMLTags", "w:alwaysMergeEmptyNamespace", "w:updateFields", + "w:hdrShapeDefaults", "w:footnotePr", "w:endnotePr", "w:compat", "w:docVars", + "w:rsids", "m:mathPr", "w:attachedSchema", "w:themeFontLang", + "w:clrSchemeMapping", "w:doNotIncludeSubdocsInStats", + "w:doNotAutoCompressPictures", "w:forceUpgrade", "w:captions", + "w:readModeInkLockDown", "w:smartTagType", "sl:schemaLibrary", + "w:shapeDefaults", "w:doNotEmbedSmartTags", "w:decimalSymbol", "w:listSeparator" + ) + evenAndOddHeaders = ZeroOrOne("w:evenAndOddHeaders", successors=_tag_seq[48:]) + del _tag_seq + + @property + def evenAndOddHeaders_val(self): + """value of `w:evenAndOddHeaders/@w:val` or |None| if not present.""" + evenAndOddHeaders = self.evenAndOddHeaders + if evenAndOddHeaders is None: + return False + return evenAndOddHeaders.val diff --git a/docx/settings.py b/docx/settings.py index f8318d809..1a2178a67 100644 --- a/docx/settings.py +++ b/docx/settings.py @@ -14,3 +14,11 @@ class Settings(ElementProxy): """ __slots__ = () + + @property + def odd_and_even_pages_header_footer(self): + """True if this document has distinct odd and even page headers and footers. + + Read/write. + """ + return self._element.evenAndOddHeaders_val diff --git a/features/doc-settings.feature b/features/doc-settings.feature index 3eccbb57e..8ff839cc9 100644 --- a/features/doc-settings.feature +++ b/features/doc-settings.feature @@ -15,7 +15,6 @@ Feature: Document.settings | no | - @wip Scenario Outline: Settings.odd_and_even_pages_header_footer getter Given a Settings object odd and even page headers as settings Then settings.odd_and_even_pages_header_footer is diff --git a/tests/test_settings.py b/tests/test_settings.py new file mode 100644 index 000000000..7507b9541 --- /dev/null +++ b/tests/test_settings.py @@ -0,0 +1,40 @@ +# encoding: utf-8 + +"""Unit test suite for the docx.settings module""" + +from __future__ import absolute_import, division, print_function, unicode_literals + +import pytest + +from docx.settings import Settings + +from .unitutil.cxml import element + + +class DescribeSettings(object): + + def it_knows_when_the_document_has_distinct_odd_and_even_headers( + self, odd_and_even_get_fixture + ): + settings_elm, expected_value = odd_and_even_get_fixture + settings = Settings(settings_elm) + + odd_and_even_pages_header_footer = settings.odd_and_even_pages_header_footer + + assert odd_and_even_pages_header_footer is expected_value + + # fixtures ------------------------------------------------------- + + @pytest.fixture( + params=[ + ("w:settings", False), + ("w:settings/w:evenAndOddHeaders", True), + ("w:settings/w:evenAndOddHeaders{w:val=0}", False), + ("w:settings/w:evenAndOddHeaders{w:val=1}", True), + ("w:settings/w:evenAndOddHeaders{w:val=true}", True), + ] + ) + def odd_and_even_get_fixture(self, request): + settings_cxml, expected_value = request.param + settings_elm = element(settings_cxml) + return settings_elm, expected_value