Skip to content

Commit 7eb8d66

Browse files
author
Steve Canny
committed
style: add Styles._get_by_id()
1 parent a902464 commit 7eb8d66

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

docx/styles/styles.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ def _get_by_id(self, style_id, style_type):
6161
default for *style_type* if *style_id* is not found or if the style
6262
having *style_id* is not of *style_type*.
6363
"""
64-
raise NotImplementedError
64+
style = self._element.get_by_id(style_id)
65+
if style is None or style.type != style_type:
66+
return self.default(style_type)
67+
return StyleFactory(style)
6568

6669
@staticmethod
6770
def _translate_special_case_names(name):

tests/styles/test_styles.py

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

1111
import pytest
1212

13+
from docx.enum.style import WD_STYLE_TYPE
1314
from docx.styles.style import BaseStyle
1415
from docx.styles.styles import Styles
1516

@@ -52,11 +53,23 @@ def it_raises_on_style_not_found(self, get_raises_fixture):
5253
def it_can_get_a_style_of_type_by_id(self, get_by_id_fixture):
5354
styles, style_id, style_type = get_by_id_fixture[:3]
5455
default_calls, _get_by_id_calls, style_ = get_by_id_fixture[3:]
56+
5557
style = styles.get_by_id(style_id, style_type)
58+
5659
assert styles.default.call_args_list == default_calls
5760
assert styles._get_by_id.call_args_list == _get_by_id_calls
5861
assert style is style_
5962

63+
def it_gets_a_style_by_id_to_help(self, _get_by_id_fixture):
64+
styles, style_id, style_type, default_calls = _get_by_id_fixture[:4]
65+
StyleFactory_, StyleFactory_calls, style_ = _get_by_id_fixture[4:]
66+
67+
style = styles._get_by_id(style_id, style_type)
68+
69+
assert styles.default.call_args_list == default_calls
70+
assert StyleFactory_.call_args_list == StyleFactory_calls
71+
assert style is style_
72+
6073
# fixture --------------------------------------------------------
6174

6275
@pytest.fixture(params=[None, 'Foo'])
@@ -73,6 +86,27 @@ def get_by_id_fixture(self, request, default_, _get_by_id_, style_):
7386
style_
7487
)
7588

89+
@pytest.fixture(params=[
90+
('w:styles/w:style{w:type=paragraph,w:styleId=Foo}', 'Foo',
91+
WD_STYLE_TYPE.PARAGRAPH),
92+
('w:styles/w:style{w:type=paragraph,w:styleId=Foo}', 'Bar',
93+
WD_STYLE_TYPE.PARAGRAPH),
94+
('w:styles/w:style{w:type=table,w:styleId=Bar}', 'Bar',
95+
WD_STYLE_TYPE.PARAGRAPH),
96+
])
97+
def _get_by_id_fixture(self, request, default_, StyleFactory_, style_):
98+
styles_cxml, style_id, style_type = request.param
99+
styles_elm = element(styles_cxml)
100+
style_elm = styles_elm[0]
101+
styles = Styles(styles_elm)
102+
default_calls = [] if style_id == 'Foo' else [call(style_type)]
103+
StyleFactory_calls = [call(style_elm)] if style_id == 'Foo' else []
104+
default_.return_value = StyleFactory_.return_value = style_
105+
return (
106+
styles, style_id, style_type, default_calls, StyleFactory_,
107+
StyleFactory_calls, style_
108+
)
109+
76110
@pytest.fixture(params=[
77111
('w:styles/(w:style{%s,w:styleId=Foobar},w:style,w:style)', 0),
78112
('w:styles/(w:style,w:style{%s,w:styleId=Foobar},w:style)', 1),

0 commit comments

Comments
 (0)