5 import xml.etree.ElementTree as ET
10 #-------------------------------------------------------------------------------
12 #-------------------------------------------------------------------------------
13 HEADER_FILE = "/include/anna/diameter/helpers/defines.hpp"
16 #-------------------------------------------------------------------------------
18 #-------------------------------------------------------------------------------
20 result = name.replace("-","_")
21 result = result.replace(" ","_")
22 result = result.replace("+","_plus_")
23 result = result.replace("/","_slash_")
26 def writeOnFile (file, text, mode = 'w'):
31 def launchShellScript(cmd):
32 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
34 output = p.stdout.read()
38 #-------------------------------------------------------------------------------
39 #-------------------------------------------------------------------------------
41 #-------------------------------------------------------------------------------
42 #-------------------------------------------------------------------------------
45 # Git repository and target file:
46 retcode,git_root = launchShellScript("git rev-parse --show-toplevel 2>/dev/null")
47 git_root = git_root.replace("\n","")
48 target = git_root + HEADER_FILE
50 # Strings for content creation
58 license = git_root + "/LICENSE"
60 content_header += str(f.read())
62 content_header += "\n\n"
63 content_header += "#ifndef anna_diameter_helpers_defines_hpp\n"
64 content_header += "#define anna_diameter_helpers_defines_hpp\n"
65 content_header += "\n\n"
66 content_header += "// Local\n"
67 content_header += "#include <anna/diameter/defines.hpp>\n"
68 content_header += "#include <anna/config/defines.hpp>\n"
69 content_header += "\n\n"
70 content_header += "namespace anna {\nnamespace diameter {\nnamespace helpers {\n"
73 # Dictionary elements content creation:
75 for file in glob.glob("*.xml"):
81 attribs = child.attrib
82 name = attribs['name']
83 name = normalize(name)
86 content_vendors += ("/** vendor '" + name + "' */ static const S32 VENDORID__" + name + " = " + attribs['code'] + ";\n")
88 try: vendor_name = attribs['vendor-name']
89 except: vendor_name = "IETF"
90 content_avps += ("/** avp '" + name + "' */ static const AvpId AVPID__" + name + "(" + attribs['code'] + ",VENDORID__" + vendor_name + ");\n")
92 single = child.find('single')
93 if single is not None:
94 label = single.find('label')
96 content_enums += ("/** avp enumeration for '" + name + "' */\nstruct AVPVALUES__" + name + "\n")
97 content_enums += "{\n"
98 content_enums += " enum v_ {\n"
100 for lbl in single.iter('label'):
101 lbl_attrib = lbl.attrib
102 alias = lbl_attrib['alias']
103 alias = normalize(alias)
104 data = lbl_attrib['data']
105 content_enums += (" " + alias + " = " + data + comma + "\n")
106 content_enums += " };\n"
107 content_enums += "};\n"
109 elif tag == "command":
110 if attribs['type'] == "Request":
114 content_commands += ("/** command '" + name + "' */ static const CommandId COMMANDID__" + name + "(" + attribs['code'] + "," + is_request + ");\n")
117 # Write target while sorting uniquely vendors, avps and commands:
118 vendors_file = target + ".vendors"
119 avps_file = target + ".avps"
120 commands_file = target + ".commands"
123 writeOnFile (target, content_header)
124 # 2) append sorted vendors
125 writeOnFile (target, "\n\n/** Vendors */\n", 'a')
126 writeOnFile (vendors_file, content_vendors)
127 retcode = launchShellScript("sort -u " + vendors_file + " >> " + target)
128 # 3) append sorted avps
129 writeOnFile (target, "\n\n/** Avps */\n", 'a')
130 writeOnFile (avps_file, content_avps)
131 retcode = launchShellScript("sort -u " + avps_file + " >> " + target)
133 writeOnFile (target, "\n\n/** Avps Enumerations */\n", 'a')
134 writeOnFile (target, content_enums, 'a')
135 # 5) append sorted commands
136 writeOnFile (target, "\n\n/** Commands */\n", 'a')
137 writeOnFile (commands_file, content_commands)
138 retcode = launchShellScript("sort -u " + commands_file + " >> " + target)
140 writeOnFile (target, "\n}\n}\n}\n\n#endif\n\n", 'a')
143 os.remove(vendors_file)
145 os.remove(commands_file)
148 print "Created file '" + target + "'"
152 if __name__ == "__main__":
154 # Command-line arguments:
155 parser = argparse.ArgumentParser(description='Generates a C++ header file from dictionary elements found under provided directory as xml files')
156 parser.add_argument('path', help='directory containing xml files')
157 args = parser.parse_args()
161 except Exception as e: