Skip to content

Commit 9c05418

Browse files
author
Steve Canny
committed
parts: add _Document.inline_shapes
Along the way: * refactor Describe_Document.it_can_serialize_to_xml()
1 parent f763833 commit 9c05418

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

docx/api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ def body(self):
4949
"""
5050
return self._document.body
5151

52+
@property
53+
def inline_shapes(self):
54+
"""
55+
Return a reference to the |InlineShapes| instance for this document.
56+
"""
57+
return self._document.inline_shapes
58+
5259
def save(self, file_):
5360
"""
5461
Save this document to *file_*, where *file_* can be either a path to

docx/parts.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from docx.opc.oxml import serialize_part_xml
88
from docx.opc.package import Part
99
from docx.oxml.shared import oxml_fromstring
10+
from docx.shared import lazyproperty
1011
from docx.table import Table
1112
from docx.text import Paragraph
1213

@@ -32,6 +33,14 @@ def body(self):
3233
"""
3334
return _Body(self._element.body)
3435

36+
@lazyproperty
37+
def inline_shapes(self):
38+
"""
39+
The |InlineShapes| instance containing the inline shapes in the
40+
document.
41+
"""
42+
return InlineShapes(self._element.body)
43+
3544
@staticmethod
3645
def load(partname, content_type, blob, package):
3746
document_elm = oxml_fromstring(blob)
@@ -102,3 +111,6 @@ class InlineShapes(object):
102111
Sequence of |InlineShape| instances, supporting len(), iteration, and
103112
indexed access.
104113
"""
114+
def __init__(self, body_elm):
115+
super(InlineShapes, self).__init__()
116+
self._body = body_elm

tests/test_parts.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from mock import Mock
1212

13+
from docx.oxml.parts import CT_Document
1314
from docx.parts import _Body, _Document
1415
from docx.table import Table
1516
from docx.text import Paragraph
@@ -20,7 +21,7 @@
2021
)
2122
from .oxml.unitdata.text import a_p, a_sectPr
2223
from .unitutil import (
23-
function_mock, class_mock, initializer_mock
24+
function_mock, class_mock, initializer_mock, instance_mock
2425
)
2526

2627

@@ -50,17 +51,27 @@ def it_has_a_body(self, document_body_fixture):
5051
_Body_.assert_called_once_with(body_elm)
5152
assert _body is _Body_.return_value
5253

53-
def it_can_serialize_to_xml(self, serialize_part_xml_):
54-
# mockery ----------------------
55-
doc = _Document(None, None, None, None)
56-
doc._element = Mock(name='_element')
57-
# exercise ---------------------
58-
doc.blob
59-
# verify -----------------------
60-
serialize_part_xml_.assert_called_once_with(doc._element)
54+
def it_can_serialize_to_xml(self, document_blob_fixture):
55+
document, document_elm, serialize_part_xml_ = document_blob_fixture
56+
blob = document.blob
57+
serialize_part_xml_.assert_called_once_with(document_elm)
58+
assert blob is serialize_part_xml_.return_value
59+
60+
def it_provides_access_to_the_inline_shapes_in_the_document(
61+
self, inline_shapes_fixture):
62+
document, InlineShapes_, body_elm = inline_shapes_fixture
63+
inline_shapes = document.inline_shapes
64+
InlineShapes_.assert_called_once_with(body_elm)
65+
assert inline_shapes is InlineShapes_.return_value
6166

6267
# fixtures -------------------------------------------------------
6368

69+
@pytest.fixture
70+
def document_blob_fixture(self, request, serialize_part_xml_):
71+
document_elm = instance_mock(request, CT_Document)
72+
document = _Document(None, None, document_elm, None)
73+
return document, document_elm, serialize_part_xml_
74+
6475
@pytest.fixture
6576
def document_body_fixture(self, request, _Body_):
6677
document_elm = (
@@ -79,6 +90,20 @@ def _Body_(self, request):
7990
def init(self, request):
8091
return initializer_mock(request, _Document)
8192

93+
@pytest.fixture
94+
def InlineShapes_(self, request):
95+
return class_mock(request, 'docx.parts.InlineShapes')
96+
97+
@pytest.fixture
98+
def inline_shapes_fixture(self, request, InlineShapes_):
99+
document_elm = (
100+
a_document().with_nsdecls().with_child(
101+
a_body())
102+
).element
103+
body_elm = document_elm[0]
104+
document = _Document(None, None, document_elm, None)
105+
return document, InlineShapes_, body_elm
106+
82107
@pytest.fixture
83108
def oxml_fromstring_(self, request):
84109
return function_mock(request, 'docx.parts.oxml_fromstring')

0 commit comments

Comments
 (0)