Skip to content

Commit f4b305a

Browse files
author
Steve Canny
committed
oxml: extract nspfxmap() to docx.oxml.ns
Changes to OxmlElement were just to satisfy flake8, although it’s probably a little bit better style anyway.
1 parent 337734f commit f4b305a

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

docx/oxml/ns.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,12 @@ def nsdecls(*prefixes):
7878
namespace prefix strings, e.g. 'p', 'ct', passed as *prefixes*.
7979
"""
8080
return ' '.join(['xmlns:%s="%s"' % (pfx, nsmap[pfx]) for pfx in prefixes])
81+
82+
83+
def nspfxmap(*nspfxs):
84+
"""
85+
Return a dict containing the subset namespace prefix mappings specified by
86+
*nspfxs*. Any number of namespace prefixes can be supplied, e.g.
87+
namespaces('a', 'r', 'p').
88+
"""
89+
return dict((pfx, nsmap[pfx]) for pfx in nspfxs)

docx/oxml/shape.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
Custom element classes for shape-related elements like ``<w:inline>``
55
"""
66

7-
from .shared import nspfxmap, OxmlBaseElement, OxmlElement, qn
7+
from .shared import OxmlBaseElement, OxmlElement, qn
88
from ..shared import Emu
9-
from .ns import nsmap
9+
from .ns import nsmap, nspfxmap
1010

1111

1212
class CT_Blip(OxmlBaseElement):

docx/oxml/shared.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
# ===========================================================================
2121

2222

23-
def nspfxmap(*nspfxs):
24-
"""
25-
Return a dict containing the subset namespace prefix mappings specified by
26-
*nspfxs*. Any number of namespace prefixes can be supplied, e.g.
27-
namespaces('a', 'r', 'p').
28-
"""
29-
return dict((pfx, nsmap[pfx]) for pfx in nspfxs)
30-
31-
3223
def OxmlElement(nsptag_str, attrs=None, nsmap=None):
3324
"""
3425
Return a 'loose' lxml element having the tag specified by *nsptag_str*.
@@ -38,9 +29,9 @@ def OxmlElement(nsptag_str, attrs=None, nsmap=None):
3829
provided as *attrs*; they are set if present.
3930
"""
4031
nsptag = NamespacePrefixedTag(nsptag_str)
41-
nsmap = nsmap if nsmap is not None else nsptag.nsmap
32+
_nsmap = nsptag.nsmap if nsmap is None else nsmap
4233
return oxml_parser.makeelement(
43-
nsptag.clark_name, attrib=attrs, nsmap=nsmap
34+
nsptag.clark_name, attrib=attrs, nsmap=_nsmap
4435
)
4536

4637

0 commit comments

Comments
 (0)