Merge pull request #37 from tripzero/master
[profile/ivi/automotive-message-broker.git] / tools / genmapping.py
1 #!/usr/bin/python
2
3 import os
4 import argparse
5
6 parser = argparse.ArgumentParser(description='Process DBus mappings.')
7 parser.add_argument('mappingFiles', metavar='N', nargs='+',
8                         help='dbus headers to find mappings in')
9 parser.add_argument('--output', dest='output',
10                         help='output file to write idl to')
11 args = parser.parse_args()
12 print "parsing"
13 print args.mappingFiles
14
15 interfaces = []
16
17 class Member:
18                 ambName = ""
19                 memberName = ""
20
21                 def __repr__(self):
22                         return "Member"
23                 def toString(self):
24                         return "{" + self.ambName + " => " + self.memberName + "}"
25                 def toIdl(self):
26                         return '  const DOMString ' + self.ambName + ' = "' + self.memberName + '";\n'
27
28 class Interface:
29                 def __init__(self):
30                         self.name = ""
31                         self.members = []
32                 def __repr__(self):
33                         return "Interface('" + name + "')"
34                 def toString(self):
35                         output = self.name + ":"
36                         for member in self.members:
37                                 output += member.toString() + ","
38                         return output
39                 def toIdl(self):
40                         output = "interface " + self.name + " {\n"
41                         for member in self.members:
42                                 output += member.toIdl()
43                         output += "\n};\n"
44                         return output
45
46 for input in args.mappingFiles:
47         try: file = open(input)
48         except IOError:
49                         print "Failed to open " + input
50         with file:
51                         for line in file:
52                                         i = line.find("DBusSink(\"");
53                                         if i != -1:
54                                                         interface = Interface()
55                                                         ifaceNameBeg = line.find('("')
56                                                         ifaceNameEnd = line.find('",')
57                                                         interface.name = line[ifaceNameBeg+2 : ifaceNameEnd]
58                                                         interfaces.append(interface)
59                                         wantPropertyVariant = 'wantPropertyVariant('
60                                         i = line.find(wantPropertyVariant)
61                                         if i!= -1:
62                                                         member = Member()
63                                                         ambNameEnd = line.find(', "')-2
64                                                         member.ambName = line[i+len(wantPropertyVariant) : i + ambNameEnd].replace("VehicleProperty::", "")
65                                                         memberNameBeg = line.find(', "')+3
66                                                         memberNameEnd = line.find('",')
67                                                         member.memberName = line[memberNameBeg : memberNameEnd]
68                                                         interfaces[-1].members.append(member)
69                         file.close()
70
71 try: outputFile = open(args.output, 'w')
72 except IOError:
73                 print "Error could not open output file: " + args.output
74 with outputFile:
75         header =("/*!\n"
76                 " * \\name AMB to AMB-DBus Mapping Tables\n"
77                 " * \\file " + os.path.basename(args.output) + "\n"
78                 " * \\brief This describes the AMB internal property names to AMB DBus interface property names\n"
79                 " * AMB internal property names are designed to be flat variable names (ie, 'ConvertableRoofStatus').  The DBus\n"
80                 " * properties however follow the naming scheme defined in the W3C automotive business group vehicle <a href='http://w3c.github.io/automotive-bg/data_spec.html'>data specification</a>\n"
81                 " * The pattern each interface is 'const DOMString AMBProperty = DBusProperty' where 'AMBProperty' is the internal name and 'DBusProperty' is the DBus property name")
82         header += " */\n\n"
83         outputFile.write(header)
84         for iface in interfaces:
85                 outputFile.write(iface.toIdl())
86                 outputFile.write("\n")
87         outputFile.close()
88
89
90