Skip to content

Commit c729d2b

Browse files
author
Steve Canny
committed
doc: add DocumentPart.settings
1 parent 3805d70 commit c729d2b

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

docx/parts/document.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def settings(self):
133133
A |Settings| object providing access to the settings in the settings
134134
part of this document.
135135
"""
136-
raise NotImplementedError
136+
return self._settings_part.settings
137137

138138
@property
139139
def styles(self):
@@ -143,6 +143,15 @@ def styles(self):
143143
"""
144144
return self._styles_part.styles
145145

146+
@property
147+
def _settings_part(self):
148+
"""
149+
A |SettingsPart| object providing access to the document-level
150+
settings for this document. Creates a default settings part if one is
151+
not present.
152+
"""
153+
raise NotImplementedError
154+
146155
@property
147156
def _styles_part(self):
148157
"""

docx/parts/settings.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# encoding: utf-8
2+
3+
"""
4+
|SettingsPart| and closely related objects
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
from ..opc.part import XmlPart
12+
13+
14+
class SettingsPart(XmlPart):
15+
"""
16+
Document-level settings part of a WordprocessingML (WML) package.
17+
"""
18+
@property
19+
def settings(self):
20+
"""
21+
A |Settings| proxy object for the `w:settings` element in this part,
22+
containing the document-level settings for this document.
23+
"""
24+
raise NotImplementedError

tests/parts/test_document.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
from docx.parts.document import DocumentPart
1616
from docx.parts.image import ImagePart
1717
from docx.parts.numbering import NumberingPart
18+
from docx.parts.settings import SettingsPart
1819
from docx.parts.styles import StylesPart
20+
from docx.settings import Settings
1921
from docx.styles.style import BaseStyle
2022
from docx.styles.styles import Styles
2123
from docx.text.paragraph import Paragraph
@@ -45,6 +47,11 @@ def it_can_get_or_add_an_image(self, get_image_fixture):
4547
document_part.relate_to.assert_called_once_with(image_part_, RT.IMAGE)
4648
assert (rId, image) == (rId_, image_)
4749

50+
def it_provides_access_to_the_document_settings(self, settings_fixture):
51+
document_part, settings_ = settings_fixture
52+
settings = document_part.settings
53+
assert settings is settings_
54+
4855
def it_provides_access_to_the_document_styles(self, styles_fixture):
4956
document_part, styles_ = styles_fixture
5057
styles = document_part.styles
@@ -217,6 +224,14 @@ def save_fixture(self, package_):
217224
file_ = 'foobar.docx'
218225
return document_part, file_
219226

227+
@pytest.fixture
228+
def settings_fixture(self, _settings_part_prop_, settings_part_,
229+
settings_):
230+
document_part = DocumentPart(None, None, None, None)
231+
_settings_part_prop_.return_value = settings_part_
232+
settings_part_.settings = settings_
233+
return document_part, settings_
234+
220235
@pytest.fixture
221236
def styles_fixture(self, _styles_part_prop_, styles_part_, styles_):
222237
document_part = DocumentPart(None, None, None, None)
@@ -289,6 +304,18 @@ def part_related_by_(self, request):
289304
def relate_to_(self, request):
290305
return method_mock(request, DocumentPart, 'relate_to')
291306

307+
@pytest.fixture
308+
def settings_(self, request):
309+
return instance_mock(request, Settings)
310+
311+
@pytest.fixture
312+
def settings_part_(self, request):
313+
return instance_mock(request, SettingsPart)
314+
315+
@pytest.fixture
316+
def _settings_part_prop_(self, request):
317+
return property_mock(request, DocumentPart, '_settings_part')
318+
292319
@pytest.fixture
293320
def style_(self, request):
294321
return instance_mock(request, BaseStyle)

0 commit comments

Comments
 (0)