summaryrefslogtreecommitdiff
path: root/xml/generate-introspection.py
blob: 7208f334c396ebcb439d3121cd530365b54b43b2 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
#
# Takes a DBus XML file and writes out introspection.[ch] files for inclusion
# in C code.  Both files are written to the OUTPUT_DIRECTORY as per below.
#
# Usage: python3 versioned-introspection.py INPUT_FILE.XML OUTPUT_DIRECTORY

import os
import sys
from xml.etree import ElementTree

CTEMPLATE = """
/*
 * This file has been auto-generated from the introspection data available
 * in the at-spi2-core repository. The D-Bus protocol is defined in this
 * repository, which can be found at:
 *
 * https://gitlab.gnome.org/GNOME/at-spi2-core
 *
 * DO NOT EDIT.
 */

%s
"""

HTEMPLATE = """
/*
 * This file has been auto-generated from the introspection data available
 * in the at-spi2-core repository. The D-Bus protocol is defined in this
 * repository, which can be found at:
 *
 * https://gitlab.gnome.org/GNOME/at-spi2-core
 *
 * DO NOT EDIT.
 */

#ifndef SPI_INTROSPECTION_DATA_H_
#define SPI_INTROSPECTION_DATA_H_

%s

#endif /* SPI_INTROSPECTION_DATA_H_ */
"""

DECTEMPLATE = """
extern const char *%s;
"""

DEFTEMPLATE = """
const char *%s = 
%s;
"""

VERSION = "0.1.7"

def convert_name (name):
    return "spi_" + name.replace (".", "_")

def convert_contents (contents):
    contents = contents.replace ("\"", "\\\"")
    literals = ["\"%s\"" % (line) for line in contents.split ("\n")]
    return "\n".join (literals)

def generate_introspection (input_filename, c_output_filename, h_output_filename):
    #Open the XML file and process includes.
    tree = ElementTree.parse (input_filename)
    root = tree.getroot ()

    #Open the output files.
    cfile = open (c_output_filename, "w")
    hfile = open (h_output_filename, "w")

    ccontents = ""
    hcontents = ""

    for itf in root.findall ("node/interface"):
        #Get and convert the name of the interface.
        name = convert_name (itf.attrib["name"])

        #Create the introspection string with version information.
        itf.attrib["version"] = VERSION
        contents = convert_contents (ElementTree.tostring (itf, encoding="unicode"))

        hcontents += DECTEMPLATE % (name)
        ccontents += DEFTEMPLATE % (name, contents)

    cfile.write (CTEMPLATE % (ccontents))
    hfile.write (HTEMPLATE % (hcontents))

    cfile.close ()
    hfile.close ()  
        
if __name__ == "__main__":
    argv = sys.argv

    if len (argv) != 4:
        print("usage: versioned-introspection.py INPUT.XML OUTPUT.C, OUTPUT.H", file=sys.stderr)
        sys.exit(1)

    input_filename = sys.argv[1]
    c_output_filename = sys.argv[2]
    h_output_filename = sys.argv[3]

    generate_introspection (input_filename, c_output_filename, h_output_filename)