blob: 067f488d3f644bbfc5afefe7c68c3938b0568328 [file] [log] [blame]
Alexandre Lision864f9b82014-08-01 14:26:45 -04001#!/usr/bin/python
2
3#
4# PACKAGE DEPENDENCIES: python python-lxml
5#
6
7#import easy to use xml parser called minidom:
8from lxml import etree
9#from copy import deepcopy
10from lxml import objectify
11import re, sys, getopt
12
13def rreplace(s, old, new, occurrence):
14 li = s.rsplit(old, occurrence)
15 return new.join(li)
16
17def usage():
18 print "jni-xml2cpp.py --file <file> | -i <file>"
19
20# main
21inputfile = "./dbus/callmanager-introspec.xml"
22outputfile = "./dbus/callmanager-jni.h"
23try:
24 opts, args = getopt.getopt(sys.argv[1:], "hi:o:", ["help", "input=", "output="])
25except getopt.GetoptError, err:
26 usage()
27 print str(err)
28 #print opts
29 sys.exit(2)
30
31for opt, arg in opts:
32 if opt in ("-h", "--help"):
33 usage()
34 sys.exit(0)
35 elif opt in ("-i", "--input"):
36 inputfile = arg
37 elif opt in ("-o", "--output"):
38 outputfile = arg
39 else:
40 print "error: argument not recognized"
41 sys.exit(3)
42
43print "inputfile = %s" % (inputfile)
44print "outputfile = %s" % (outputfile)
45source = "".join(args)
46
47# lxml.objectify
48# FIXME relative path
49cm_obj_tree = objectify.parse(inputfile)
50cm_obj_root = cm_obj_tree.getroot()
51# http://www.skymind.com/~ocrow/python_string/
52# method 4: list of strings
53prototype = []
54# iteration on methods
55for meth in cm_obj_root.interface.iter(tag="method"):
56# iteration on arguments
57 prototype.append(meth.get("name"))
58 prototype.append("(")
59 for argum in meth.iter(tag="arg"):
60 name = argum.get("name")
61 typ = argum.get("type")
62# FIXME
63 if typ == 's':
64 prototype.append("string %s, " % (name))
65 elif typ == 'i':
66 prototype.append("int %s, " % (name))
67 elif typ == 'd':
68 prototype.append("unsigned int %s, " % (name))
69 elif typ == 'as':
70 prototype.append("std::vector< std::string > &%s, " % (name))
71 else:
72 prototype.append("void %s, " % (name))
73 prototype.append(");\n")
74
75# starting from the end of string,
76# replace the first and 1-only comma by nothing
77#rreplace(prototype[tostring(), ',', '', 1)
78
79p = re.compile(", \);")
80prototypes = p.sub(");", ''.join(prototype))
81
82# FIXME relative path
83outfile = open(outputfile, "w")
84outfile.write(prototypes)
85outfile.close()