Skip to content

Commit 64e1360

Browse files
author
Steve Canny
committed
sect: migrate Sections to docx.section
1 parent 900807f commit 64e1360

File tree

4 files changed

+73
-74
lines changed

4 files changed

+73
-74
lines changed

docx/parts/document.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11-
from collections import Sequence
12-
1311
from ..blkcntnr import BlockItemContainer
1412
from ..document import Document
1513
from .numbering import NumberingPart
1614
from ..opc.constants import RELATIONSHIP_TYPE as RT
1715
from ..opc.part import XmlPart
18-
from ..section import Section
16+
from ..section import Sections
1917
from ..shape import InlineShape
2018
from ..shared import lazyproperty, Parented
2119
from .styles import StylesPart
@@ -221,27 +219,3 @@ def _inline_lst(self):
221219
body = self._body
222220
xpath = '//w:p/w:r/w:drawing/wp:inline'
223221
return body.xpath(xpath)
224-
225-
226-
class Sections(Sequence):
227-
"""
228-
Sequence of |Section| objects corresponding to the sections in the
229-
document. Supports ``len()``, iteration, and indexed access.
230-
"""
231-
def __init__(self, document_elm):
232-
super(Sections, self).__init__()
233-
self._document_elm = document_elm
234-
235-
def __getitem__(self, key):
236-
if isinstance(key, slice):
237-
sectPr_lst = self._document_elm.sectPr_lst[key]
238-
return [Section(sectPr) for sectPr in sectPr_lst]
239-
sectPr = self._document_elm.sectPr_lst[key]
240-
return Section(sectPr)
241-
242-
def __iter__(self):
243-
for sectPr in self._document_elm.sectPr_lst:
244-
yield Section(sectPr)
245-
246-
def __len__(self):
247-
return len(self._document_elm.sectPr_lst)

docx/section.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@
66

77
from __future__ import absolute_import, print_function, unicode_literals
88

9+
from collections import Sequence
10+
11+
12+
class Sections(Sequence):
13+
"""
14+
Sequence of |Section| objects corresponding to the sections in the
15+
document. Supports ``len()``, iteration, and indexed access.
16+
"""
17+
def __init__(self, document_elm):
18+
super(Sections, self).__init__()
19+
self._document_elm = document_elm
20+
21+
def __getitem__(self, key):
22+
if isinstance(key, slice):
23+
sectPr_lst = self._document_elm.sectPr_lst[key]
24+
return [Section(sectPr) for sectPr in sectPr_lst]
25+
sectPr = self._document_elm.sectPr_lst[key]
26+
return Section(sectPr)
27+
28+
def __iter__(self):
29+
for sectPr in self._document_elm.sectPr_lst:
30+
yield Section(sectPr)
31+
32+
def __len__(self):
33+
return len(self._document_elm.sectPr_lst)
34+
935

1036
class Section(object):
1137
"""

tests/parts/test_document.py

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
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, Sections
16+
from docx.parts.document import _Body, DocumentPart, InlineShapes
1717
from docx.parts.image import ImagePart
1818
from docx.parts.numbering import NumberingPart
1919
from docx.parts.styles import StylesPart
20-
from docx.section import Section
2120
from docx.shape import InlineShape
2221
from docx.styles.style import BaseStyle
2322
from docx.styles.styles import Styles
@@ -579,47 +578,3 @@ def rId_(self, request):
579578
@pytest.fixture
580579
def shape_id_(self, request):
581580
return instance_mock(request, int)
582-
583-
584-
class DescribeSections(object):
585-
586-
def it_knows_how_many_sections_it_contains(self, len_fixture):
587-
sections, expected_len = len_fixture
588-
assert len(sections) == expected_len
589-
590-
def it_can_iterate_over_its_Section_instances(self, iter_fixture):
591-
sections, expected_count = iter_fixture
592-
section_count = 0
593-
for section in sections:
594-
section_count += 1
595-
assert isinstance(section, Section)
596-
assert section_count == expected_count
597-
598-
def it_can_access_its_Section_instances_by_index(self, index_fixture):
599-
sections, indicies = index_fixture
600-
assert len(sections[0:2]) == 2
601-
for index in indicies:
602-
assert isinstance(sections[index], Section)
603-
604-
# fixtures -------------------------------------------------------
605-
606-
@pytest.fixture
607-
def index_fixture(self, document_elm):
608-
sections = Sections(document_elm)
609-
return sections, [0, 1]
610-
611-
@pytest.fixture
612-
def iter_fixture(self, document_elm):
613-
sections = Sections(document_elm)
614-
return sections, 2
615-
616-
@pytest.fixture
617-
def len_fixture(self, document_elm):
618-
sections = Sections(document_elm)
619-
return sections, 2
620-
621-
# fixture components ---------------------------------------------
622-
623-
@pytest.fixture
624-
def document_elm(self):
625-
return element('w:document/w:body/(w:p/w:pPr/w:sectPr, w:sectPr)')

tests/test_section.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,56 @@
99
import pytest
1010

1111
from docx.enum.section import WD_ORIENT, WD_SECTION
12-
from docx.section import Section
12+
from docx.section import Section, Sections
1313
from docx.shared import Inches
1414

1515
from .unitutil.cxml import element, xml
1616

1717

18+
class DescribeSections(object):
19+
20+
def it_knows_how_many_sections_it_contains(self, len_fixture):
21+
sections, expected_len = len_fixture
22+
assert len(sections) == expected_len
23+
24+
def it_can_iterate_over_its_Section_instances(self, iter_fixture):
25+
sections, expected_count = iter_fixture
26+
section_count = 0
27+
for section in sections:
28+
section_count += 1
29+
assert isinstance(section, Section)
30+
assert section_count == expected_count
31+
32+
def it_can_access_its_Section_instances_by_index(self, index_fixture):
33+
sections, indicies = index_fixture
34+
assert len(sections[0:2]) == 2
35+
for index in indicies:
36+
assert isinstance(sections[index], Section)
37+
38+
# fixtures -------------------------------------------------------
39+
40+
@pytest.fixture
41+
def index_fixture(self, document_elm):
42+
sections = Sections(document_elm)
43+
return sections, [0, 1]
44+
45+
@pytest.fixture
46+
def iter_fixture(self, document_elm):
47+
sections = Sections(document_elm)
48+
return sections, 2
49+
50+
@pytest.fixture
51+
def len_fixture(self, document_elm):
52+
sections = Sections(document_elm)
53+
return sections, 2
54+
55+
# fixture components ---------------------------------------------
56+
57+
@pytest.fixture
58+
def document_elm(self):
59+
return element('w:document/w:body/(w:p/w:pPr/w:sectPr, w:sectPr)')
60+
61+
1862
class DescribeSection(object):
1963

2064
def it_knows_its_start_type(self, start_type_get_fixture):

0 commit comments

Comments
 (0)