Skip to content

Commit 06ebca3

Browse files
author
Steve Canny
committed
oxml: extract qn() to docx.oxml.ns
1 parent f4b305a commit 06ebca3

File tree

9 files changed

+27
-29
lines changed

9 files changed

+27
-29
lines changed

docx/oxml/ns.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,14 @@ def nspfxmap(*nspfxs):
8787
namespaces('a', 'r', 'p').
8888
"""
8989
return dict((pfx, nsmap[pfx]) for pfx in nspfxs)
90+
91+
92+
def qn(tag):
93+
"""
94+
Stands for "qualified name", a utility function to turn a namespace
95+
prefixed tag name into a Clark-notation qualified tag name for lxml. For
96+
example, ``qn('p:cSld')`` returns ``'{http://schemas.../main}cSld'``.
97+
"""
98+
prefix, tagroot = tag.split(':')
99+
uri = nsmap[prefix]
100+
return '{%s}%s' % (uri, tagroot)

docx/oxml/parts/document.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<w:document>.
66
"""
77

8-
from docx.oxml.shared import OxmlBaseElement, qn
9-
from docx.oxml.table import CT_Tbl
10-
from docx.oxml.text import CT_P
8+
from ..ns import qn
9+
from ..shared import OxmlBaseElement
10+
from ..table import CT_Tbl
11+
from ..text import CT_P
1112

1213

1314
class CT_Document(OxmlBaseElement):

docx/oxml/parts/numbering.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
Custom element classes related to the numbering part
55
"""
66

7-
from ..shared import CT_DecimalNumber, OxmlBaseElement, OxmlElement, qn
8-
from ..ns import nsmap
7+
from ..shared import CT_DecimalNumber, OxmlBaseElement, OxmlElement
8+
from ..ns import nsmap, qn
99

1010

1111
class CT_Num(OxmlBaseElement):

docx/oxml/parts/styles.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
Custom element classes related to the styles part
55
"""
66

7-
from ..shared import OxmlBaseElement, qn
8-
from ..ns import nsmap
7+
from ..shared import OxmlBaseElement
8+
from ..ns import nsmap, qn
99

1010

1111
class CT_Style(OxmlBaseElement):

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 OxmlBaseElement, OxmlElement, qn
7+
from .shared import OxmlBaseElement, OxmlElement
88
from ..shared import Emu
9-
from .ns import nsmap, nspfxmap
9+
from .ns import nsmap, nspfxmap, qn
1010

1111

1212
class CT_Blip(OxmlBaseElement):

docx/oxml/shared.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from . import oxml_parser
1414
from .exceptions import ValidationError
15-
from .ns import NamespacePrefixedTag, nsmap
15+
from .ns import NamespacePrefixedTag, nsmap, qn
1616

1717

1818
# ===========================================================================
@@ -42,17 +42,6 @@ def oxml_fromstring(text):
4242
return etree.fromstring(text, oxml_parser)
4343

4444

45-
def qn(tag):
46-
"""
47-
Stands for "qualified name", a utility function to turn a namespace
48-
prefixed tag name into a Clark-notation qualified tag name for lxml. For
49-
example, ``qn('p:cSld')`` returns ``'{http://schemas.../main}cSld'``.
50-
"""
51-
prefix, tagroot = tag.split(':')
52-
uri = nsmap[prefix]
53-
return '{%s}%s' % (uri, tagroot)
54-
55-
5645
def serialize_for_reading(element):
5746
"""
5847
Serialize *element* to human-readable XML suitable for tests. No XML

docx/oxml/table.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
from __future__ import absolute_import, print_function, unicode_literals
88

9-
from docx.oxml.shared import OxmlBaseElement, OxmlElement, qn
10-
119
from .exceptions import ValidationError
12-
from .shared import CT_String
10+
from .ns import qn
11+
from .shared import CT_String, OxmlBaseElement, OxmlElement
1312
from .text import CT_P
1413

1514

docx/oxml/text.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
"""
77

88
from ..enum.text import WD_UNDERLINE
9-
from .ns import nsdecls
9+
from .ns import nsdecls, qn
1010
from .parts.numbering import CT_NumPr
11-
from .shared import (
12-
CT_String, OxmlBaseElement, OxmlElement, oxml_fromstring, qn
13-
)
11+
from .shared import CT_String, OxmlBaseElement, OxmlElement, oxml_fromstring
1412

1513

1614
class CT_Br(OxmlBaseElement):

features/steps/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from docx import Document
1212
from docx.enum.text import WD_BREAK, WD_UNDERLINE
13-
from docx.oxml.shared import qn
13+
from docx.oxml.ns import qn
1414

1515
from helpers import test_docx, test_text
1616

0 commit comments

Comments
 (0)