|
1 | 1 | """Namespace-related objects.""" |
2 | 2 |
|
| 3 | +from typing import Any, Dict |
| 4 | + |
| 5 | +from typing_extensions import Self |
| 6 | + |
3 | 7 | nsmap = { |
4 | 8 | "a": "http://schemas.openxmlformats.org/drawingml/2006/main", |
5 | 9 | "c": "http://schemas.openxmlformats.org/drawingml/2006/chart", |
|
25 | 29 | class NamespacePrefixedTag(str): |
26 | 30 | """Value object that knows the semantics of an XML tag having a namespace prefix.""" |
27 | 31 |
|
28 | | - def __new__(cls, nstag, *args): |
| 32 | + def __new__(cls, nstag: str, *args: Any): |
29 | 33 | return super(NamespacePrefixedTag, cls).__new__(cls, nstag) |
30 | 34 |
|
31 | | - def __init__(self, nstag): |
| 35 | + def __init__(self, nstag: str): |
32 | 36 | self._pfx, self._local_part = nstag.split(":") |
33 | 37 | self._ns_uri = nsmap[self._pfx] |
34 | 38 |
|
35 | 39 | @property |
36 | | - def clark_name(self): |
| 40 | + def clark_name(self) -> str: |
37 | 41 | return "{%s}%s" % (self._ns_uri, self._local_part) |
38 | 42 |
|
39 | 43 | @classmethod |
40 | | - def from_clark_name(cls, clark_name): |
| 44 | + def from_clark_name(cls, clark_name: str) -> Self: |
41 | 45 | nsuri, local_name = clark_name[1:].split("}") |
42 | 46 | nstag = "%s:%s" % (pfxmap[nsuri], local_name) |
43 | 47 | return cls(nstag) |
44 | 48 |
|
45 | 49 | @property |
46 | | - def local_part(self): |
47 | | - """Return the local part of the tag as a string. |
| 50 | + def local_part(self) -> str: |
| 51 | + """The local part of this tag. |
48 | 52 |
|
49 | | - E.g. 'foobar' is returned for tag 'f:foobar'. |
| 53 | + E.g. "foobar" is returned for tag "f:foobar". |
50 | 54 | """ |
51 | 55 | return self._local_part |
52 | 56 |
|
53 | 57 | @property |
54 | | - def nsmap(self): |
55 | | - """Return a dict having a single member, mapping the namespace prefix of this |
56 | | - tag to it's namespace name (e.g. {'f': 'http://foo/bar'}). |
| 58 | + def nsmap(self) -> Dict[str, str]: |
| 59 | + """Single-member dict mapping prefix of this tag to it's namespace name. |
57 | 60 |
|
58 | | - This is handy for passing to xpath calls and other uses. |
| 61 | + Example: `{"f": "http://foo/bar"}`. This is handy for passing to xpath calls |
| 62 | + and other uses. |
59 | 63 | """ |
60 | 64 | return {self._pfx: self._ns_uri} |
61 | 65 |
|
62 | 66 | @property |
63 | | - def nspfx(self): |
64 | | - """Return the string namespace prefix for the tag, e.g. 'f' is returned for tag |
65 | | - 'f:foobar'.""" |
| 67 | + def nspfx(self) -> str: |
| 68 | + """The namespace-prefix for this tag. |
| 69 | +
|
| 70 | + For example, "f" is returned for tag "f:foobar". |
| 71 | + """ |
66 | 72 | return self._pfx |
67 | 73 |
|
68 | 74 | @property |
69 | | - def nsuri(self): |
70 | | - """Return the namespace URI for the tag, e.g. 'http://foo/bar' would be returned |
71 | | - for tag 'f:foobar' if the 'f' prefix maps to 'http://foo/bar' in nsmap.""" |
| 75 | + def nsuri(self) -> str: |
| 76 | + """The namespace URI for this tag. |
| 77 | +
|
| 78 | + For example, "http://foo/bar" would be returned for tag "f:foobar" if the "f" |
| 79 | + prefix maps to "http://foo/bar" in nsmap. |
| 80 | + """ |
72 | 81 | return self._ns_uri |
73 | 82 |
|
74 | 83 |
|
75 | | -def nsdecls(*prefixes): |
76 | | - """Return a string containing a namespace declaration for each of the namespace |
77 | | - prefix strings, e.g. 'p', 'ct', passed as `prefixes`.""" |
| 84 | +def nsdecls(*prefixes: str) -> str: |
| 85 | + """Namespace declaration including each namespace-prefix in `prefixes`. |
| 86 | +
|
| 87 | + Handy for adding required namespace declarations to a tree root element. |
| 88 | + """ |
78 | 89 | return " ".join(['xmlns:%s="%s"' % (pfx, nsmap[pfx]) for pfx in prefixes]) |
79 | 90 |
|
80 | 91 |
|
81 | | -def nspfxmap(*nspfxs): |
82 | | - """Return a dict containing the subset namespace prefix mappings specified by |
83 | | - `nspfxs`. |
| 92 | +def nspfxmap(*nspfxs: str) -> Dict[str, str]: |
| 93 | + """Subset namespace-prefix mappings specified by *nspfxs*. |
84 | 94 |
|
85 | | - Any number of namespace prefixes can be supplied, e.g. namespaces('a', 'r', 'p'). |
| 95 | + Any number of namespace prefixes can be supplied, e.g. namespaces("a", "r", "p"). |
86 | 96 | """ |
87 | 97 | return {pfx: nsmap[pfx] for pfx in nspfxs} |
88 | 98 |
|
89 | 99 |
|
90 | | -def qn(tag): |
91 | | - """Stands for "qualified name", a utility function to turn a namespace prefixed tag |
92 | | - name into a Clark-notation qualified tag name for lxml. |
| 100 | +def qn(tag: str) -> str: |
| 101 | + """Stands for "qualified name". |
93 | 102 |
|
94 | | - For |
95 | | - example, ``qn('p:cSld')`` returns ``'{http://schemas.../main}cSld'``. |
| 103 | + This utility function converts a familiar namespace-prefixed tag name like "w:p" |
| 104 | + into a Clark-notation qualified tag name for lxml. For example, `qn("w:p")` returns |
| 105 | + "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}p". |
96 | 106 | """ |
97 | 107 | prefix, tagroot = tag.split(":") |
98 | 108 | uri = nsmap[prefix] |
|
0 commit comments