File: test_case.py

package info (click to toggle)
shared-mime-info 2.4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,800 kB
  • sloc: cpp: 3,175; xml: 2,182; ansic: 612; python: 168; sh: 121; javascript: 113; lisp: 29; cobol: 20; makefile: 16; cs: 13; perl: 11; java: 7; objc: 6; php: 1; vhdl: 1
file content (45 lines) | stat: -rwxr-xr-x 1,167 bytes parent folder | download | duplicates (2)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3

from collections import defaultdict
import itertools
import sys
import xml.etree.ElementTree as ET

FDO_XMLNS = "{http://www.freedesktop.org/standards/shared-mime-info}"


def test_case(fdo_xml: str) -> int:

    with open(fdo_xml, "rb") as f:
        tree = ET.parse(f)

    errors = 0

    types = defaultdict(set)
    for elem in itertools.chain(
        tree.findall(f"{FDO_XMLNS}mime-type"),
        tree.findall(f"{FDO_XMLNS}mime-type/{FDO_XMLNS}alias"),
        tree.findall(f"{FDO_XMLNS}mime-type/{FDO_XMLNS}sub-class-of"),
    ):
        type_ = elem.attrib["type"]
        types[type_.lower()].add(type_)
    assert types

    for type_, spellings in types.items():
        if len(spellings) != 1:
            print(
                f"{fdo_xml}: multiple spellings differing only by case "
                f"for {', '.join(sorted(spellings))}",
                file=sys.stderr,
            )
            errors += 1

    return errors


if __name__ == "__main__":
    if len(sys.argv) != 2:
        print(f"Usage: {__file__} FREEDESKTOP_ORG_XML", file=sys.stderr)
        sys.exit(2)

    sys.exit(test_case(sys.argv[1]) and 1 or 0)