Skip to content

Commit ce2f4cd

Browse files
author
Steve Canny
committed
doc: add Document._body
1 parent eadd737 commit ce2f4cd

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

docx/document.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11+
from .blkcntnr import BlockItemContainer
1112
from .shared import ElementProxy
1213

1314

@@ -16,8 +17,28 @@ class Document(ElementProxy):
1617
WordprocessingML (WML) document.
1718
"""
1819

19-
__slots__ = ('_part',)
20+
__slots__ = ('_part', '__body')
2021

2122
def __init__(self, element, part):
2223
super(Document, self).__init__(element)
2324
self._part = part
25+
self.__body = None
26+
27+
@property
28+
def _body(self):
29+
"""
30+
The |_Body| instance containing the content for this document.
31+
"""
32+
if self.__body is None:
33+
self.__body = _Body(self._element.body, self)
34+
return self.__body
35+
36+
37+
class _Body(BlockItemContainer):
38+
"""
39+
Proxy for ``<w:body>`` element in this document, having primarily a
40+
container role.
41+
"""
42+
def __init__(self, body_elm, parent):
43+
super(_Body, self).__init__(body_elm, parent)
44+
self._body = body_elm

tests/test_document.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Test suite for the docx.document module
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
import pytest
12+
13+
from docx.document import _Body, Document
14+
15+
from .unitutil.cxml import element
16+
from .unitutil.mock import class_mock, instance_mock
17+
18+
19+
class DescribeDocument(object):
20+
21+
def it_provides_access_to_the_document_body(self, body_fixture):
22+
document, body_elm, _Body_, body_ = body_fixture
23+
body = document._body
24+
_Body_.assert_called_once_with(body_elm, document)
25+
assert body is body_
26+
27+
# fixtures -------------------------------------------------------
28+
29+
@pytest.fixture
30+
def body_fixture(self, _Body_, body_):
31+
document_elm = element('w:document/w:body')
32+
body_elm = document_elm[0]
33+
document = Document(document_elm, None)
34+
return document, body_elm, _Body_, body_
35+
36+
# fixture components ---------------------------------------------
37+
38+
@pytest.fixture
39+
def _Body_(self, request, body_):
40+
return class_mock(request, 'docx.document._Body', return_value=body_)
41+
42+
@pytest.fixture
43+
def body_(self, request):
44+
return instance_mock(request, _Body)

0 commit comments

Comments
 (0)