Skip to content

Commit 8202bb7

Browse files
author
Steve Canny
committed
doc: migrate _Body to docx.document
1 parent 655cb56 commit 8202bb7

File tree

4 files changed

+97
-109
lines changed

4 files changed

+97
-109
lines changed

docx/document.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,12 @@ class _Body(BlockItemContainer):
183183
def __init__(self, body_elm, parent):
184184
super(_Body, self).__init__(body_elm, parent)
185185
self._body = body_elm
186+
187+
def clear_content(self):
188+
"""
189+
Return this |_Body| instance after clearing it of all content.
190+
Section properties for the main document story, if present, are
191+
preserved.
192+
"""
193+
self._body.clear_content()
194+
return self

docx/parts/document.py

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

11-
from ..blkcntnr import BlockItemContainer
1211
from ..document import Document
1312
from .numbering import NumberingPart
1413
from ..opc.constants import RELATIONSHIP_TYPE as RT
@@ -132,25 +131,6 @@ def _styles_part(self):
132131
return styles_part
133132

134133

135-
class _Body(BlockItemContainer):
136-
"""
137-
Proxy for ``<w:body>`` element in this document, having primarily a
138-
container role.
139-
"""
140-
def __init__(self, body_elm, parent):
141-
super(_Body, self).__init__(body_elm, parent)
142-
self._body = body_elm
143-
144-
def clear_content(self):
145-
"""
146-
Return this |_Body| instance after clearing it of all content.
147-
Section properties for the main document story, if present, are
148-
preserved.
149-
"""
150-
self._body.clear_content()
151-
return self
152-
153-
154134
class InlineShapes(Parented):
155135
"""
156136
Sequence of |InlineShape| instances, supporting len(), iteration, and

tests/parts/test_document.py

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@
1313
from docx.oxml.parts.document import CT_Body
1414
from docx.oxml.text.run import CT_R
1515
from docx.package import ImageParts, Package
16-
from docx.parts.document import _Body, DocumentPart, InlineShapes
16+
from docx.parts.document import DocumentPart, InlineShapes
1717
from docx.parts.image import ImagePart
1818
from docx.parts.numbering import NumberingPart
1919
from docx.parts.styles import StylesPart
2020
from docx.shape import InlineShape
2121
from docx.styles.style import BaseStyle
2222
from docx.styles.styles import Styles
23-
from docx.table import Table
2423
from docx.text.paragraph import Paragraph
2524
from docx.text.run import Run
2625

2726
from ..oxml.parts.unitdata.document import a_body, a_document
2827
from ..oxml.unitdata.text import a_p
29-
from ..unitutil.cxml import element, xml
28+
from ..unitutil.cxml import element
3029
from ..unitutil.mock import (
3130
instance_mock, class_mock, loose_mock, method_mock, property_mock
3231
)
@@ -303,92 +302,6 @@ def _styles_part_prop_(self, request):
303302
return property_mock(request, DocumentPart, '_styles_part')
304303

305304

306-
class Describe_Body(object):
307-
308-
def it_can_add_a_paragraph(self, add_paragraph_fixture):
309-
body, expected_xml = add_paragraph_fixture
310-
p = body.add_paragraph()
311-
assert body._body.xml == expected_xml
312-
assert isinstance(p, Paragraph)
313-
314-
def it_can_add_a_table(self, add_table_fixture):
315-
body, rows, cols, expected_xml = add_table_fixture
316-
table = body.add_table(rows, cols)
317-
assert body._element.xml == expected_xml
318-
assert isinstance(table, Table)
319-
320-
def it_can_clear_itself_of_all_content_it_holds(self, clear_fixture):
321-
body, expected_xml = clear_fixture
322-
_body = body.clear_content()
323-
assert body._body.xml == expected_xml
324-
assert _body is body
325-
326-
def it_provides_access_to_the_paragraphs_it_contains(
327-
self, paragraphs_fixture):
328-
body = paragraphs_fixture
329-
paragraphs = body.paragraphs
330-
assert len(paragraphs) == 2
331-
for p in paragraphs:
332-
assert isinstance(p, Paragraph)
333-
334-
def it_provides_access_to_the_tables_it_contains(self, tables_fixture):
335-
body = tables_fixture
336-
tables = body.tables
337-
assert len(tables) == 2
338-
for table in tables:
339-
assert isinstance(table, Table)
340-
341-
# fixtures -------------------------------------------------------
342-
343-
@pytest.fixture(params=[
344-
('w:body', 'w:body/w:p'),
345-
('w:body/w:p', 'w:body/(w:p, w:p)'),
346-
('w:body/w:sectPr', 'w:body/(w:p, w:sectPr)'),
347-
('w:body/(w:p, w:sectPr)', 'w:body/(w:p, w:p, w:sectPr)'),
348-
])
349-
def add_paragraph_fixture(self, request):
350-
before_cxml, after_cxml = request.param
351-
body = _Body(element(before_cxml), None)
352-
expected_xml = xml(after_cxml)
353-
return body, expected_xml
354-
355-
@pytest.fixture(params=[
356-
('w:body', 0, 0, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
357-
'tblGrid)'),
358-
('w:body', 1, 0, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
359-
'tblGrid,w:tr)'),
360-
('w:body', 0, 1, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
361-
'tblGrid/w:gridCol)'),
362-
('w:body', 1, 1, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
363-
'tblGrid/w:gridCol,w:tr/w:tc/w:p)'),
364-
])
365-
def add_table_fixture(self, request):
366-
body_cxml, rows, cols, after_cxml = request.param
367-
body = _Body(element(body_cxml), None)
368-
expected_xml = xml(after_cxml)
369-
return body, rows, cols, expected_xml
370-
371-
@pytest.fixture(params=[
372-
('w:body', 'w:body'),
373-
('w:body/w:p', 'w:body'),
374-
('w:body/w:sectPr', 'w:body/w:sectPr'),
375-
('w:body/(w:p, w:sectPr)', 'w:body/w:sectPr'),
376-
])
377-
def clear_fixture(self, request):
378-
before_cxml, after_cxml = request.param
379-
body = _Body(element(before_cxml), None)
380-
expected_xml = xml(after_cxml)
381-
return body, expected_xml
382-
383-
@pytest.fixture
384-
def paragraphs_fixture(self):
385-
return _Body(element('w:body/(w:p, w:p)'), None)
386-
387-
@pytest.fixture
388-
def tables_fixture(self):
389-
return _Body(element('w:body/(w:tbl, w:tbl)'), None)
390-
391-
392305
class DescribeInlineShapes(object):
393306

394307
def it_knows_how_many_inline_shapes_it_contains(

tests/test_document.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,89 @@ def table_(self, request):
316316
@pytest.fixture
317317
def tables_(self, request):
318318
return instance_mock(request, list)
319+
320+
321+
class Describe_Body(object):
322+
323+
def it_can_add_a_paragraph(self, add_paragraph_fixture):
324+
body, expected_xml = add_paragraph_fixture
325+
p = body.add_paragraph()
326+
assert body._body.xml == expected_xml
327+
assert isinstance(p, Paragraph)
328+
329+
def it_can_add_a_table(self, add_table_fixture):
330+
body, rows, cols, expected_xml = add_table_fixture
331+
table = body.add_table(rows, cols)
332+
assert body._element.xml == expected_xml
333+
assert isinstance(table, Table)
334+
335+
def it_can_clear_itself_of_all_content_it_holds(self, clear_fixture):
336+
body, expected_xml = clear_fixture
337+
_body = body.clear_content()
338+
assert body._body.xml == expected_xml
339+
assert _body is body
340+
341+
def it_provides_access_to_the_paragraphs_it_contains(
342+
self, paragraphs_fixture):
343+
body = paragraphs_fixture
344+
paragraphs = body.paragraphs
345+
assert len(paragraphs) == 2
346+
for p in paragraphs:
347+
assert isinstance(p, Paragraph)
348+
349+
def it_provides_access_to_the_tables_it_contains(self, tables_fixture):
350+
body = tables_fixture
351+
tables = body.tables
352+
assert len(tables) == 2
353+
for table in tables:
354+
assert isinstance(table, Table)
355+
356+
# fixtures -------------------------------------------------------
357+
358+
@pytest.fixture(params=[
359+
('w:body', 'w:body/w:p'),
360+
('w:body/w:p', 'w:body/(w:p, w:p)'),
361+
('w:body/w:sectPr', 'w:body/(w:p, w:sectPr)'),
362+
('w:body/(w:p, w:sectPr)', 'w:body/(w:p, w:p, w:sectPr)'),
363+
])
364+
def add_paragraph_fixture(self, request):
365+
before_cxml, after_cxml = request.param
366+
body = _Body(element(before_cxml), None)
367+
expected_xml = xml(after_cxml)
368+
return body, expected_xml
369+
370+
@pytest.fixture(params=[
371+
('w:body', 0, 0, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
372+
'tblGrid)'),
373+
('w:body', 1, 0, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
374+
'tblGrid,w:tr)'),
375+
('w:body', 0, 1, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
376+
'tblGrid/w:gridCol)'),
377+
('w:body', 1, 1, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
378+
'tblGrid/w:gridCol,w:tr/w:tc/w:p)'),
379+
])
380+
def add_table_fixture(self, request):
381+
body_cxml, rows, cols, after_cxml = request.param
382+
body = _Body(element(body_cxml), None)
383+
expected_xml = xml(after_cxml)
384+
return body, rows, cols, expected_xml
385+
386+
@pytest.fixture(params=[
387+
('w:body', 'w:body'),
388+
('w:body/w:p', 'w:body'),
389+
('w:body/w:sectPr', 'w:body/w:sectPr'),
390+
('w:body/(w:p, w:sectPr)', 'w:body/w:sectPr'),
391+
])
392+
def clear_fixture(self, request):
393+
before_cxml, after_cxml = request.param
394+
body = _Body(element(before_cxml), None)
395+
expected_xml = xml(after_cxml)
396+
return body, expected_xml
397+
398+
@pytest.fixture
399+
def paragraphs_fixture(self):
400+
return _Body(element('w:body/(w:p, w:p)'), None)
401+
402+
@pytest.fixture
403+
def tables_fixture(self):
404+
return _Body(element('w:body/(w:tbl, w:tbl)'), None)

0 commit comments

Comments
 (0)