Skip to content

Commit 6ef6bce

Browse files
author
Steve Canny
committed
style: deprecate get style by styleId
1 parent d96f45f commit 6ef6bce

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

docx/styles/styles.py

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

11+
from warnings import warn
12+
1113
from . import BabelFish
1214
from .latent import LatentStyles
1315
from ..shared import ElementProxy
@@ -35,14 +37,24 @@ def __contains__(self, name):
3537

3638
def __getitem__(self, key):
3739
"""
38-
Enables dictionary-style access by style id or UI name.
40+
Enables dictionary-style access by UI name. Lookup by style id is
41+
deprecated, triggers a warning, and will be removed in a near-future
42+
release.
3943
"""
40-
style_name = BabelFish.ui2internal(key)
41-
for get in (self._element.get_by_id, self._element.get_by_name):
42-
style_elm = get(style_name)
43-
if style_elm is not None:
44-
return StyleFactory(style_elm)
45-
raise KeyError("no style with id or name '%s'" % key)
44+
style_elm = self._element.get_by_name(BabelFish.ui2internal(key))
45+
if style_elm is not None:
46+
return StyleFactory(style_elm)
47+
48+
style_elm = self._element.get_by_id(key)
49+
if style_elm is not None:
50+
msg = (
51+
'style lookup by style_id is deprecated. Use style name as '
52+
'key instead.'
53+
)
54+
warn(msg, UserWarning)
55+
return StyleFactory(style_elm)
56+
57+
raise KeyError("no style with name '%s'" % key)
4658

4759
def __iter__(self):
4860
return (StyleFactory(style) for style in self._element.style_lst)

0 commit comments

Comments
 (0)