Skip to content

Commit b8a13ad

Browse files
author
Steve Canny
committed
style: add _Styles.__len__()
1 parent d6a46fc commit b8a13ad

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

docx/oxml/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
register_custom_element_class('w:num', CT_Num)
3737
register_custom_element_class('w:numbering', CT_Numbering)
3838

39+
from docx.oxml.parts.styles import CT_Styles
40+
register_custom_element_class('w:styles', CT_Styles)
41+
3942
from docx.oxml.table import CT_Row, CT_Tbl, CT_TblGrid, CT_TblPr, CT_Tc
4043
register_custom_element_class('w:tbl', CT_Tbl)
4144
register_custom_element_class('w:tblGrid', CT_TblGrid)

docx/oxml/parts/styles.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
Custom element classes related to the styles part
55
"""
66

7-
from docx.oxml.shared import OxmlBaseElement
7+
from docx.oxml.shared import OxmlBaseElement, qn
88

99

1010
class CT_Styles(OxmlBaseElement):
1111
"""
1212
``<w:styles>`` element, the root element of a styles part, i.e.
1313
styles.xml
1414
"""
15+
@property
16+
def style_lst(self):
17+
"""
18+
List of <w:style> child elements.
19+
"""
20+
return self.findall(qn('w:style'))

docx/parts/styles.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ class _Styles(object):
5858
def __init__(self, styles_elm):
5959
super(_Styles, self).__init__()
6060
self._styles_elm = styles_elm
61+
62+
def __len__(self):
63+
return len(self._styles_elm.style_lst)

features/sty-get-styles-part.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Feature: Get the document styles part
33
As a programmer using the advanced python-docx API
44
I need access to the styles part of the document
55

6-
@wip
76
Scenario: Get an existing styles part from document
87
Given a document having a styles part
98
When I get the styles part from the document

tests/oxml/unitdata/styles.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Test data builders for styles part XML elements
5+
"""
6+
7+
from ...unitdata import BaseBuilder
8+
9+
10+
class CT_StyleBuilder(BaseBuilder):
11+
__tag__ = 'w:style'
12+
__nspfxs__ = ('w',)
13+
__attrs__ = ('w:type', 'w:styleId', 'w:default', 'w:customStyle')
14+
15+
16+
class CT_StylesBuilder(BaseBuilder):
17+
__tag__ = 'w:styles'
18+
__nspfxs__ = ('w',)
19+
__attrs__ = ()
20+
21+
22+
def a_style():
23+
return CT_StyleBuilder()
24+
25+
26+
def a_styles():
27+
return CT_StylesBuilder()

tests/parts/test_styles.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from docx.package import Package
1616
from docx.parts.styles import StylesPart, _Styles
1717

18+
from ..oxml.unitdata.styles import a_style, a_styles
1819
from ..unitutil import (
1920
function_mock, class_mock, initializer_mock, instance_mock, method_mock
2021
)
@@ -130,3 +131,22 @@ def styles_part_(self, request):
130131
@pytest.fixture
131132
def styles_part_load_(self, request):
132133
return method_mock(request, StylesPart, 'load')
134+
135+
136+
class Describe_Styles(object):
137+
138+
def it_knows_how_many_styles_it_contains(self, len_fixture):
139+
styles, style_count = len_fixture
140+
assert len(styles) == style_count
141+
142+
# fixtures -------------------------------------------------------
143+
144+
@pytest.fixture(params=[0, 1, 2, 3])
145+
def len_fixture(self, request):
146+
style_count = request.param
147+
styles_bldr = a_styles().with_nsdecls()
148+
for idx in range(style_count):
149+
styles_bldr.with_child(a_style())
150+
styles_elm = styles_bldr.element
151+
styles = _Styles(styles_elm)
152+
return styles, style_count

0 commit comments

Comments
 (0)