| 1 |
# Copyright Mozilla Foundation |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from __future__ import annotations |
| 16 |
|
| 17 |
from collections import defaultdict |
| 18 |
from collections.abc import Iterator |
| 19 |
|
| 20 |
from lxml.etree import QName, _Comment, _Element |
| 21 |
|
| 22 |
from ..data import Metadata |
| 23 |
|
| 24 |
xliff_ns = { |
| 25 |
"urn:oasis:names:tc:xliff:document:1.0", |
| 26 |
"urn:oasis:names:tc:xliff:document:1.1", |
| 27 |
"urn:oasis:names:tc:xliff:document:1.2", |
| 28 |
} |
| 29 |
xml_ns = "http://www.w3.org/XML/1998/namespace" |
| 30 |
|
| 31 |
|
| 32 |
def element_as_metadata( |
| 33 |
el: _Element, base: str, with_attrib: bool |
| 34 |
) -> Iterator[Metadata[str]]: |
| 35 |
is_empty = True |
| 36 |
seen: dict[str, int] = defaultdict(int) |
| 37 |
if with_attrib: |
| 38 |
am = attrib_as_metadata(el, base) |
| 39 |
if am: |
| 40 |
yield from am |
| 41 |
is_empty = False |
| 42 |
if el.text and not el.text.isspace(): |
| 43 |
yield Metadata(base, el.text) |
| 44 |
is_empty = False |
| 45 |
for child in el: |
| 46 |
if isinstance(child, _Comment): |
| 47 |
yield Metadata(f"{base}/comment()" if base else "comment()", child.text) |
| 48 |
elif isinstance(child.tag, str): |
| 49 |
name = pretty_name(child, child.tag) |
| 50 |
idx = seen[name] + 1 |
| 51 |
child_base = f"{base}/{name}" if base else name |
| 52 |
if idx > 1: |
| 53 |
child_base += f"[{idx}]" |
| 54 |
yield from element_as_metadata(child, child_base, True) |
| 55 |
seen[name] = idx |
| 56 |
else: |
| 57 |
raise ValueError(f"Unsupported metadata element in {base}: {el}") |
| 58 |
if child.tail and not child.tail.isspace(): |
| 59 |
yield Metadata(base, child.tail) |
| 60 |
is_empty = False |
| 61 |
if is_empty and with_attrib: |
| 62 |
yield Metadata(base, "") |
| 63 |
|
| 64 |
|
| 65 |
def attrib_as_metadata( |
| 66 |
el: _Element, base: str | None = None, exclude: tuple[str] | None = None |
| 67 |
) -> list[Metadata[str]]: |
| 68 |
res = [] |
| 69 |
for key, value in el.attrib.items(): |
| 70 |
if not exclude or key not in exclude: |
| 71 |
name = pretty_name(el, key) |
| 72 |
pk = f"{base}/@{name}" if base else f"@{name}" |
| 73 |
res.append(Metadata(pk, value)) |
| 74 |
return res |
| 75 |
|
| 76 |
|
| 77 |
def pretty_name(el: _Element, name: str) -> str: |
| 78 |
if not name.startswith("{"): |
| 79 |
return name |
| 80 |
q = QName(name) |
| 81 |
ns = q.namespace |
| 82 |
if not ns or ns in xliff_ns: |
| 83 |
return q.localname |
| 84 |
if ns == xml_ns: |
| 85 |
return f"xml:{q.localname}" |
| 86 |
ns_key = next(iter(k for k, v in el.nsmap.items() if v == ns), None) |
| 87 |
if ns_key: |
| 88 |
return f"{ns_key}:{q.localname}" |
| 89 |
else: |
| 90 |
raise ValueError(f"Name with unknown namespace: {name}") |
| 91 |
|
| 92 |
|
| 93 |
def clark_name(nsmap: dict[str | None, str], name: str) -> str: |
| 94 |
"""See https://lxml.de/tutorial.html#namespaces""" |
| 95 |
if ":" not in name: |
| 96 |
return name |
| 97 |
ns, local = name.split(":", 1) |
| 98 |
if ns in nsmap: |
| 99 |
return QName(nsmap[ns], local).text |
| 100 |
if ns == "xml": |
| 101 |
return f"{{{xml_ns}}}{local}" |
| 102 |
raise ValueError(f"Name with unknown namespace: {name}") |