]>
Commit | Line | Data |
---|---|---|
f613f81a KO |
1 | """ |
2 | Name: doxymlparser.py | |
3 | Author: Kevin Ollivier | |
4 | License: wxWidgets License | |
5 | """ | |
6 | ||
7 | __description__ = """ | |
8 | Takes the output of Doxygen XML and parses it to retrieve metadata about the classes and methods. | |
9 | ||
10 | To create the Doxygen XML files, from the wxWidgets/docs/doxygen directory, do: | |
11 | ||
12 | ./regen.sh xml | |
13 | ||
14 | To see the results from parsing a particular class, do: | |
15 | ||
16 | python doxymlparser.py --report out/xml/classwx_<whatever>.xml | |
17 | """ | |
18 | ||
19 | #!/usr/bin/env python | |
20 | import optparse | |
21 | import os | |
22 | import string | |
23 | ||
24 | import sys | |
25 | import types | |
26 | from xml.dom import minidom | |
27 | ||
28 | option_dict = { | |
29 | "report" : (False, "Print out the classes and methods found by this script."), | |
30 | } | |
31 | ||
32 | parser = optparse.OptionParser(usage="usage: %prog [options] <doxyml files to parse>\n" + __description__, version="%prog 1.0") | |
33 | ||
34 | for opt in option_dict: | |
35 | default = option_dict[opt][0] | |
36 | ||
37 | action = "store" | |
38 | if type(default) == types.BooleanType: | |
39 | action = "store_true" | |
40 | parser.add_option("--" + opt, default=default, action=action, dest=opt, help=option_dict[opt][1]) | |
41 | ||
42 | options, arguments = parser.parse_args() | |
43 | ||
44 | class ClassDefinition: | |
45 | def __init__(self): | |
46 | self.name = "" | |
47 | self.methods = [] | |
48 | self.brief_description = "" | |
49 | self.detailed_description = "" | |
50 | self.includes = [] | |
51 | self.bases = [] | |
52 | ||
53 | def __str__(self): | |
54 | str_repr = """ | |
55 | Class: %s | |
56 | Bases: %s | |
fc76d7df | 57 | Includes: %s |
f613f81a KO |
58 | Brief Description: |
59 | %s | |
60 | ||
61 | Detailed Description: | |
62 | %s | |
63 | """ % (self.name, string.join(self.bases, ", "), self.includes, self.brief_description, self.detailed_description) | |
64 | str_repr += "Methods:\n" | |
65 | ||
66 | for method in self.methods: | |
67 | str_repr += str(method) | |
68 | ||
69 | return str_repr | |
70 | ||
71 | class MethodDefinition: | |
72 | def __init__(self): | |
73 | self.name = "" | |
74 | self.return_type = "" | |
75 | self.argsstring = "" | |
76 | self.definition = "" | |
77 | self.params = [] | |
78 | self.brief_description = "" | |
79 | self.detailed_description = "" | |
80 | ||
81 | def __str__(self): | |
82 | str_repr = """ | |
83 | Method: %s | |
84 | Return Type: %s | |
85 | Params: %r | |
86 | Prototype: %s | |
87 | Brief Description: | |
88 | %s | |
89 | ||
90 | Detailed Description: | |
91 | %s | |
92 | """ % (self.name, self.return_type, self.params, self.definition + self.argsstring, self.brief_description, self.detailed_description) | |
93 | return str_repr | |
94 | ||
95 | def getTextValue(node, recursive=False): | |
96 | text = "" | |
97 | for child in node.childNodes: | |
0ed9af4e KO |
98 | if child.nodeType == child.ELEMENT_NODE and child.nodeName == "ref": |
99 | text += getTextValue(child) | |
f613f81a | 100 | if child.nodeType == child.TEXT_NODE: |
fc76d7df KO |
101 | # Add a space to ensure we have a space between qualifiers and parameter names |
102 | text += child.nodeValue.strip() + " " | |
f613f81a | 103 | |
fc76d7df | 104 | return text.strip() |
f613f81a KO |
105 | |
106 | def doxyMLToText(node): | |
107 | return text | |
108 | ||
109 | class DoxyMLParser: | |
110 | def __init__(self): | |
111 | self.classes = [] | |
112 | ||
113 | def parse(self, filename): | |
114 | self.xmldoc = minidom.parse(filename).documentElement | |
115 | for node in self.xmldoc.getElementsByTagName("compounddef"): | |
116 | self.classes.append(self.parse_class(node)) | |
117 | ||
118 | def parse_class(self, class_node): | |
119 | new_class = ClassDefinition() | |
120 | for node in class_node.childNodes: | |
121 | if node.nodeName == "compoundname": | |
122 | new_class.name = getTextValue(node) | |
123 | print "Parsing class %s" % new_class.name | |
124 | elif node.nodeName == "basecompoundref": | |
125 | new_class.bases.append(getTextValue(node)) | |
126 | elif node.nodeName == "briefdescription": | |
127 | # let the post-processor determ | |
128 | new_class.brief_description = node.toxml() | |
129 | elif node.nodeName == "detaileddescription": | |
130 | new_class.detailed_description = node.toxml() | |
131 | ||
132 | self.parse_methods(new_class, class_node) | |
133 | return new_class | |
134 | ||
135 | def parse_methods(self, new_class, root): | |
136 | for method in root.getElementsByTagName("memberdef"): | |
137 | new_method = MethodDefinition() | |
138 | for node in method.childNodes: | |
139 | if node.nodeName == "name": | |
140 | new_method.name = getTextValue(node) | |
141 | elif node.nodeName == "type": | |
142 | new_method.return_type = getTextValue(node) | |
143 | elif node.nodeName == "definition": | |
144 | new_method.definition = getTextValue(node) | |
145 | elif node.nodeName == "argsstring": | |
146 | new_method.argsstring = getTextValue(node) | |
147 | elif node.nodeName == "param": | |
148 | param = {} | |
149 | for child in node.childNodes: | |
150 | if child.nodeType == child.ELEMENT_NODE: | |
151 | param[child.nodeName] = getTextValue(child) | |
152 | new_method.params.append(param) | |
0ed9af4e | 153 | print "Adding %s" % (new_method.name + new_method.argsstring) |
f613f81a KO |
154 | new_class.methods.append(new_method) |
155 | ||
156 | if __name__ == "__main__": | |
157 | if len(arguments) < 1: | |
158 | parser.print_usage() | |
159 | sys.exit(1) | |
160 | ||
161 | doxyparse = DoxyMLParser() | |
162 | for arg in arguments: | |
163 | doxyparse.parse(arg) | |
164 | ||
165 | if options.report: | |
166 | for aclass in doxyparse.classes: | |
167 | print str(aclass) | |
168 |