Skip to content

Commit

Permalink
hdr: add Settings.odd_and_even_pages_header_footer
Browse files Browse the repository at this point in the history
Getter side.
  • Loading branch information
scanny committed Jan 6, 2019
1 parent 1fd57de commit a46dd64
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docx/oxml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
57 changes: 57 additions & 0 deletions docx/oxml/settings.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions docx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion features/doc-settings.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Feature: Document.settings
| no |


@wip
Scenario Outline: Settings.odd_and_even_pages_header_footer getter
Given a Settings object <with-or-without> odd and even page headers as settings
Then settings.odd_and_even_pages_header_footer is <value>
Expand Down
40 changes: 40 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a46dd64

Please sign in to comment.