forked from cms-sw/cmssw-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genClassesH
executable file
·65 lines (57 loc) · 2.22 KB
/
genClassesH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
from optparse import OptionParser
from xml.sax import parseString
from xml.sax.handler import ContentHandler
from sys import exit
def die(msg):
print msg
exit(1)
class ClassesDefContentHandler(ContentHandler):
def __init__(self):
self.classes = []
self.headers = []
def startElement(self, name, attrs):
# <*> is used to indicate "whatever specified in classes.h" which obviously
# in this case does not work anymore, because we want to generate such a
# file. Therefore we exit with an error.
#
# If a class has argument "concrete", classes_def.xml declaration is enough
# to generate the dictionary and we do not need to create an instance in
# classes.h.
if name == "class":
if "pattern" in attrs:
die("Cannot use pattern '%s': please declare actual class names to use automatic classes.h generation." % attrs["pattern"])
if "type" in attrs and attrs["type"] == "concrete":
return
self.classes.append(attrs["name"])
if name == "include":
if "file" in attrs:
self.headers.append("\"" + attrs["file"] + "\"")
elif "system" in attrs:
self.headers.append("<"+attrs["system"] + ">")
else:
die("Malformed classes_def.xml")
def endDocument(self):
print "\n".join("#include %s" % x for x in self.headers)
if self.headers: print ""
print "namespace {\n struct dictionary {"
print "\n".join(" %s a%s;" % (n, i) for (i,n) in enumerate(self.classes))
print "};\n}"
if __name__ == "__main__":
parser = OptionParser(usage="%{progname}s <classes_def.xml>")
opts, args = parser.parse_args()
if not args:
parser.error("Please specify the input <classes_def.xml>")
if len(args) > 1:
parser.error("Too many input files specified")
# Replace any occurence of <>& in the attribute values by the xml parameter
rxml, nxml = file(args[0]).read(), ''
q1,q2 = 0,0
for c in rxml :
if (q1 or q2) and c == '<' : nxml += '<'
elif (q1 or q2) and c == '>' : nxml += '>'
# elif (q1 or q2) and c == '&' : nxml += '&'
else : nxml += c
if c == '"' : q1 = not q1
if c == "'" : q2 = not q2
parseString(nxml, ClassesDefContentHandler())