summaryrefslogtreecommitdiff
path: root/tools/assets/descriptor/xml_errors.py
blob: 7d7f6e0197ea47cf7bc296a498b6d706f787b49c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# SPDX-FileCopyrightText: © 2025 ZeldaRET
# SPDX-License-Identifier: CC0-1.0

from xml.etree import ElementTree


class XMLDescError(Exception):
    pass


def check_attrib(
    elem: ElementTree.Element,
    required: set[str],
    optional: set[str] = set(),
):
    required_and_missing = required - elem.attrib.keys()
    if required_and_missing:
        raise XMLDescError(
            "Missing attributes on "
            + ElementTree.tostring(elem, encoding="unicode")
            + ": "
            + ", ".join(required_and_missing)
        )
    unknown = elem.attrib.keys() - required - optional
    if unknown:
        raise XMLDescError(
            "Unknown attributes on "
            + ElementTree.tostring(elem, encoding="unicode")
            + ": "
            + ", ".join(unknown)
        )