Alexandre Lision | 864f9b8 | 2014-08-01 14:26:45 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # |
| 4 | # PACKAGE DEPENDENCIES: python python-lxml |
| 5 | # |
| 6 | |
| 7 | #import easy to use xml parser called minidom: |
| 8 | from lxml import etree |
| 9 | #from copy import deepcopy |
| 10 | from lxml import objectify |
| 11 | import re, sys, getopt |
| 12 | |
| 13 | def rreplace(s, old, new, occurrence): |
| 14 | li = s.rsplit(old, occurrence) |
| 15 | return new.join(li) |
| 16 | |
| 17 | def usage(): |
| 18 | print "jni-xml2cpp.py --file <file> | -i <file>" |
| 19 | |
| 20 | # main |
| 21 | inputfile = "./dbus/callmanager-introspec.xml" |
| 22 | outputfile = "./dbus/callmanager-jni.h" |
| 23 | try: |
| 24 | opts, args = getopt.getopt(sys.argv[1:], "hi:o:", ["help", "input=", "output="]) |
| 25 | except getopt.GetoptError, err: |
| 26 | usage() |
| 27 | print str(err) |
| 28 | #print opts |
| 29 | sys.exit(2) |
| 30 | |
| 31 | for 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 | |
| 43 | print "inputfile = %s" % (inputfile) |
| 44 | print "outputfile = %s" % (outputfile) |
| 45 | source = "".join(args) |
| 46 | |
| 47 | # lxml.objectify |
| 48 | # FIXME relative path |
| 49 | cm_obj_tree = objectify.parse(inputfile) |
| 50 | cm_obj_root = cm_obj_tree.getroot() |
| 51 | # http://www.skymind.com/~ocrow/python_string/ |
| 52 | # method 4: list of strings |
| 53 | prototype = [] |
| 54 | # iteration on methods |
| 55 | for 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 | |
| 79 | p = re.compile(", \);") |
| 80 | prototypes = p.sub(");", ''.join(prototype)) |
| 81 | |
| 82 | # FIXME relative path |
| 83 | outfile = open(outputfile, "w") |
| 84 | outfile.write(prototypes) |
| 85 | outfile.close() |