Skip to content

Commit 8313711

Browse files
Benjamin Toornstrascanny
authored andcommitted
parts: add EndnotesPart
1 parent e297131 commit 8313711

6 files changed

Lines changed: 78 additions & 1 deletion

File tree

docs/conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# documentation root, use os.path.abspath to make it absolute, like shown here.
2121
sys.path.insert(0, os.path.abspath('..'))
2222

23-
from docx import __version__
23+
from docx import __version__ # noqa
2424

2525

2626
# -- General configuration ---------------------------------------------------
@@ -101,6 +101,8 @@
101101
102102
.. |Emu| replace:: :class:`.Emu`
103103
104+
.. |EndnotesPart| replace:: :class:`.EndnotesPart`
105+
104106
.. |False| replace:: :class:`False`
105107
106108
.. |float| replace:: :class:`.float`

docx/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from docx.opc.parts.coreprops import CorePropertiesPart
1313

1414
from docx.parts.document import DocumentPart
15+
from docx.parts.endnotes import EndnotesPart
1516
from docx.parts.hdrftr import FooterPart, HeaderPart
1617
from docx.parts.image import ImagePart
1718
from docx.parts.numbering import NumberingPart
@@ -28,6 +29,7 @@ def part_class_selector(content_type, reltype):
2829
PartFactory.part_class_selector = part_class_selector
2930
PartFactory.part_type_for[CT.OPC_CORE_PROPERTIES] = CorePropertiesPart
3031
PartFactory.part_type_for[CT.WML_DOCUMENT_MAIN] = DocumentPart
32+
PartFactory.part_type_for[CT.WML_ENDNOTES] = EndnotesPart
3133
PartFactory.part_type_for[CT.WML_FOOTER] = FooterPart
3234
PartFactory.part_type_for[CT.WML_HEADER] = HeaderPart
3335
PartFactory.part_type_for[CT.WML_NUMBERING] = NumberingPart
@@ -38,6 +40,7 @@ def part_class_selector(content_type, reltype):
3840
CT,
3941
CorePropertiesPart,
4042
DocumentPart,
43+
EndnotesPart,
4144
FooterPart,
4245
HeaderPart,
4346
NumberingPart,

docx/oxml/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
7777
register_element_cls('w:body', CT_Body)
7878
register_element_cls('w:document', CT_Document)
7979

80+
from docx.oxml.endnotes import CT_Endnotes # noqa
81+
register_element_cls('w:endnotes', CT_Endnotes)
82+
8083
from docx.oxml.hdrftr import CT_HdrFtr # noqa
8184
register_element_cls('w:ftr', CT_HdrFtr)
8285
register_element_cls('w:hdr', CT_HdrFtr)

docx/oxml/endnotes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# encoding: utf-8
2+
3+
"""Custom element classes related to end-notes"""
4+
5+
from __future__ import absolute_import, division, print_function, unicode_literals
6+
7+
from docx.oxml.xmlchemy import BaseOxmlElement
8+
9+
10+
class CT_Endnotes(BaseOxmlElement):
11+
"""`w:endnotes` element"""

docx/parts/endnotes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# encoding: utf-8
2+
3+
"""|EndnotesPart| and closely related objects"""
4+
5+
from __future__ import absolute_import, division, print_function, unicode_literals
6+
7+
from docx.opc.part import XmlPart
8+
9+
10+
class EndnotesPart(XmlPart):
11+
"""Package part containing end-notes"""

tests/parts/test_endnotes.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# encoding: utf-8
2+
3+
"""Unit test suite for the docx.parts.endnotes 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, RELATIONSHIP_TYPE as RT
10+
from docx.opc.part import PartFactory
11+
from docx.package import Package
12+
from docx.parts.endnotes import EndnotesPart
13+
14+
from ..unitutil.mock import instance_mock, method_mock
15+
16+
17+
class DescribeEndnotesPart(object):
18+
19+
def it_is_used_by_loader_to_construct_endnotes_part(
20+
self, package_, EndnotesPart_load_, endnotes_part_
21+
):
22+
partname = "endnotes.xml"
23+
content_type = CT.WML_ENDNOTES
24+
reltype = RT.ENDNOTES
25+
blob = "<w:endnotes/>"
26+
EndnotesPart_load_.return_value = endnotes_part_
27+
28+
part = PartFactory(partname, content_type, reltype, blob, package_)
29+
30+
EndnotesPart_load_.assert_called_once_with(
31+
partname, content_type, blob, package_
32+
)
33+
assert part is endnotes_part_
34+
35+
# fixture components ---------------------------------------------
36+
37+
@pytest.fixture
38+
def EndnotesPart_load_(self, request):
39+
return method_mock(request, EndnotesPart, "load")
40+
41+
@pytest.fixture
42+
def endnotes_part_(self, request):
43+
return instance_mock(request, EndnotesPart)
44+
45+
@pytest.fixture
46+
def package_(self, request):
47+
return instance_mock(request, Package)

0 commit comments

Comments
 (0)