#!/usr/bin/env python3
#
# Takes a DBus XML file and writes out introspection.[ch] files for inclusion
# in C code.

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 ("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: generate-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)

@@ -26,18 +26,16 @@
 # Takes 
-a 
 DBus XML
@@ -35,24 +35,25 @@
 Bus XML file
+s
  and writes 
@@ -56,16 +56,26 @@
 tes out 
+a pair of 
 introspe
@@ -119,16 +119,32 @@
  code.%0A%0A
+import argparse%0A
 import o
@@ -954,17 +954,16 @@
 ar *%25s =
- 
 %0A%25s;%0A%22%22%22
@@ -2148,19 +2148,9 @@
 e ()
-  %0A        
+%0A
 %0Aif 
@@ -2181,163 +2181,444 @@
     
+p
 ar
-gv = sys.argv%0A%0A    if len (argv) != 4:%0A        print(%22usage: generate-introspection.py INPUT.XML OUTP
+ser = argparse.ArgumentParser(description=%22Create a C source file and header file from DBus XML files%22)%0A    parser.add_argument('sources', metavar='FILE.XML', nargs='+', help='DBus XML interface file')%0A    parser.add_argument('--c-output', metavar='O
 UT.C
+'
 , 
-OUTPUT.H%22, file=sys.stderr)%0A        sys.exit(1
+required=True, help='Name out output C file')%0A    parser.add_argument('--h-output', metavar='OUT.H', required=True, help='Name out output H file')%0A    args = parser.parse_args(
 )%0A%0A 
@@ -2754,58 +2754,51 @@
 on (
-input_filename, c_output_filename, h_output_filename
+args.sources%5B0%5D, args.c_output, args.h_output
 )%0A

Use an ArgumentParser to take multiple input filenames easily