File: generate-dbus-interface.py

package info (click to toggle)
fwupd 2.0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,504 kB
  • sloc: ansic: 277,388; python: 11,485; xml: 9,493; sh: 1,625; makefile: 167; cpp: 19; asm: 11; javascript: 9
file content (50 lines) | stat: -rwxr-xr-x 1,608 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
46
47
48
49
50
#!/usr/bin/env python3
# pylint: disable=invalid-name,missing-docstring
#
# Copyright 2022 Richard Hughes <richard@hughsie.com>
#
# SPDX-License-Identifier: LGPL-2.1-or-later

import sys
import argparse
import xml.etree.ElementTree as ET


def _remove_docs(parent):
    namespaces = {"doc": "http://www.freedesktop.org/dbus/1.0/doc.dtd"}
    for node in parent.findall("doc:doc", namespaces):
        parent.remove(node)
    parent.text = ""


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("src", action="store", type=str, help="metainfo source")
    parser.add_argument("dst", action="store", type=str, help="metainfo destination")
    args = parser.parse_args()

    tree = ET.parse(args.src)
    tree.text = ""
    for node in tree.findall("interface"):
        for node_prop in node.findall("property"):
            _remove_docs(node_prop)
        for node_signal in node.findall("signal"):
            for node_arg in node_signal.findall("arg"):
                _remove_docs(node_arg)
            _remove_docs(node_signal)
        for node_method in node.findall("method"):
            for node_arg in node_method.findall("arg"):
                _remove_docs(node_arg)
            _remove_docs(node_method)
        _remove_docs(node)

    try:
        ET.indent(tree, space=" ", level=0)
    except AttributeError:
        print(
            f"WARNING: indenting of {args.dst} disabled as python is too old",
            file=sys.stderr,
        )
        pass
    with open(args.dst, "wb") as f:
        tree.write(f, encoding="UTF-8", xml_declaration=True)