Skip to content

Commit 3828b11

Browse files
author
Steve Canny
committed
style: add LatentStyles.__getitem__()
1 parent faf2dee commit 3828b11

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

docx/oxml/styles.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ class CT_LatentStyles(BaseOxmlElement):
3838
"""
3939
lsdException = ZeroOrMore('w:lsdException', successors=())
4040

41+
def get_by_name(self, name):
42+
"""
43+
Return the `w:lsdException` child having *name*, or |None| if not
44+
found.
45+
"""
46+
found = self.xpath('w:lsdException[@w:name="%s"]' % name)
47+
if not found:
48+
return None
49+
return found[0]
50+
4151

4252
class CT_Style(BaseOxmlElement):
4353
"""

docx/styles/latent.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ class LatentStyles(ElementProxy):
2020

2121
__slots__ = ()
2222

23+
def __getitem__(self, key):
24+
"""
25+
Enables dictionary-style access to a latent style by name.
26+
"""
27+
lsdException = self._element.get_by_name(key)
28+
if lsdException is None:
29+
raise KeyError("no latent style with name '%s'" % key)
30+
return _LatentStyle(lsdException)
31+
2332
def __iter__(self):
2433
return (_LatentStyle(ls) for ls in self._element.lsdException_lst)
2534

features/sty-access-latent-styles.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Feature: Access latent styles for a document
1010
And len(latent_styles) is 137
1111

1212

13-
@wip
1413
Scenario: Access latent style in collection
1514
Given a latent style collection
1615
Then I can iterate over the latent styles

tests/styles/test_latent.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,37 @@ def it_can_iterate_over_its_latent_styles(self, iter_fixture):
2828
for latent_style in lst:
2929
assert isinstance(latent_style, _LatentStyle)
3030

31+
def it_can_get_a_latent_style_by_name(self, getitem_fixture):
32+
latent_styles, name, lsdException = getitem_fixture
33+
latent_style = latent_styles[name]
34+
assert isinstance(latent_style, _LatentStyle)
35+
assert latent_style._element is lsdException
36+
37+
def it_raises_on_latent_style_not_found(self, getitem_raises_fixture):
38+
latent_styles, name = getitem_raises_fixture
39+
with pytest.raises(KeyError):
40+
latent_styles[name]
41+
3142
# fixture --------------------------------------------------------
3243

44+
@pytest.fixture(params=[
45+
('w:lsdException{w:name=Ab},w:lsdException,w:lsdException', 'Ab', 0),
46+
('w:lsdException,w:lsdException{w:name=Cd},w:lsdException', 'Cd', 1),
47+
('w:lsdException,w:lsdException,w:lsdException{w:name=Ef}', 'Ef', 2),
48+
])
49+
def getitem_fixture(self, request):
50+
cxml, name, idx = request.param
51+
latentStyles_cxml = 'w:latentStyles/(%s)' % cxml
52+
latentStyles = element(latentStyles_cxml)
53+
lsdException = latentStyles[idx]
54+
latent_styles = LatentStyles(latentStyles)
55+
return latent_styles, name, lsdException
56+
57+
@pytest.fixture
58+
def getitem_raises_fixture(self):
59+
latent_styles = LatentStyles(element('w:latentStyles'))
60+
return latent_styles, 'Foobar'
61+
3362
@pytest.fixture(params=[
3463
('w:latentStyles', 0),
3564
('w:latentStyles/w:lsdException', 1),

0 commit comments

Comments
 (0)