Skip to content

Commit 4ed8545

Browse files
ondrej-111scanny
authored andcommitted
hdr: add HeaderPart.new()
1 parent 3512601 commit 4ed8545

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

docx/opc/package.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ def main_document_part(self):
108108
"""
109109
return self.part_related_by(RT.OFFICE_DOCUMENT)
110110

111+
def next_partname(self, template):
112+
"""Return a |PackURI| instance representing partname matching *template*.
113+
114+
The returned part-name has the next available numeric suffix to distinguish it
115+
from other parts of its type. *template* is a printf (%)-style template string
116+
containing a single replacement item, a '%d' to be used to insert the integer
117+
portion of the partname. Example: "/word/header%d.xml"
118+
"""
119+
raise NotImplementedError
120+
111121
@classmethod
112122
def open(cls, pkg_file):
113123
"""

docx/parts/hdrftr.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
from __future__ import absolute_import, division, print_function, unicode_literals
66

7+
from docx.opc.constants import CONTENT_TYPE as CT
78
from docx.opc.part import XmlPart
9+
from docx.oxml import parse_xml
810

911

1012
class HeaderPart(XmlPart):
@@ -13,4 +15,12 @@ class HeaderPart(XmlPart):
1315
@classmethod
1416
def new(cls, package):
1517
"""Return newly created header part."""
18+
partname = package.next_partname("/word/header%d.xml")
19+
content_type = CT.WML_HEADER
20+
element = parse_xml(cls._default_header_xml())
21+
return cls(partname, content_type, element, package)
22+
23+
@classmethod
24+
def _default_header_xml(cls):
25+
"""Return bytes containing XML for a default header part."""
1626
raise NotImplementedError

tests/parts/test_document.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# encoding: utf-8
22

3-
"""Test suite for the docx.parts.document module"""
3+
"""Unit test suite for the docx.parts.document module"""
44

55
from __future__ import absolute_import, division, print_function, unicode_literals
66

@@ -24,9 +24,7 @@
2424
from ..oxml.parts.unitdata.document import a_body, a_document
2525
from ..oxml.unitdata.text import a_p
2626
from ..unitutil.file import snippet_text
27-
from ..unitutil.mock import (
28-
instance_mock, class_mock, method_mock, property_mock
29-
)
27+
from ..unitutil.mock import class_mock, instance_mock, method_mock, property_mock
3028

3129

3230
class DescribeDocumentPart(object):

tests/parts/test_hdrftr.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# encoding: utf-8
2+
3+
"""Unit test suite for the docx.parts.hdrftr module"""
4+
5+
from __future__ import absolute_import, division, print_function, unicode_literals
6+
7+
import pytest
8+
9+
from docx.opc.constants import CONTENT_TYPE as CT
10+
from docx.package import Package
11+
from docx.parts.hdrftr import HeaderPart
12+
13+
from ..unitutil.cxml import element
14+
from ..unitutil.mock import function_mock, initializer_mock, instance_mock, method_mock
15+
16+
17+
class DescribeHeaderPart(object):
18+
19+
def it_can_create_a_new_header_part(
20+
self, package_, _default_header_xml_, parse_xml_, _init_
21+
):
22+
hdr = element("w:hdr")
23+
package_.next_partname.return_value = "/word/header42.xml"
24+
_default_header_xml_.return_value = "<w:hdr>"
25+
parse_xml_.return_value = hdr
26+
27+
header_part = HeaderPart.new(package_)
28+
29+
package_.next_partname.assert_called_once_with("/word/header%d.xml")
30+
_default_header_xml_.assert_called_once_with()
31+
parse_xml_.assert_called_once_with("<w:hdr>")
32+
_init_.assert_called_once_with(
33+
header_part, "/word/header42.xml", CT.WML_HEADER, hdr, package_
34+
)
35+
36+
# fixture components ---------------------------------------------
37+
38+
@pytest.fixture
39+
def _default_header_xml_(self, request):
40+
return method_mock(request, HeaderPart, "_default_header_xml", autospec=False)
41+
42+
@pytest.fixture
43+
def _init_(self, request):
44+
return initializer_mock(request, HeaderPart, autospec=True)
45+
46+
@pytest.fixture
47+
def package_(self, request):
48+
return instance_mock(request, Package)
49+
50+
@pytest.fixture
51+
def parse_xml_(self, request):
52+
return function_mock(request, "docx.parts.hdrftr.parse_xml")

0 commit comments

Comments
 (0)