| 1 |
# An example showing how to use the python doxmlparser module to extract some metrics from |
| 2 |
# the XML output generated by doxygen for a project. |
| 3 |
|
| 4 |
import sys |
| 5 |
|
| 6 |
import doxmlparser |
| 7 |
|
| 8 |
from doxmlparser.compound import DoxCompoundKind, DoxMemberKind, DoxSectionKind, MixedContainer |
| 9 |
|
| 10 |
class Metrics: |
| 11 |
def __init__(self): |
| 12 |
self.numClasses = 0 |
| 13 |
self.numDocClasses = 0 |
| 14 |
self.numStructs = 0 |
| 15 |
self.numUnions = 0 |
| 16 |
self.numInterfaces = 0 |
| 17 |
self.numExceptions = 0 |
| 18 |
self.numNamespaces = 0 |
| 19 |
self.numFiles = 0 |
| 20 |
self.numDocFiles = 0 |
| 21 |
self.numGroups = 0 |
| 22 |
self.numPages = 0 |
| 23 |
self.numPubMethods = 0 |
| 24 |
self.numDocPubMethods = 0 |
| 25 |
self.numProMethods = 0 |
| 26 |
self.numDocProMethods = 0 |
| 27 |
self.numPriMethods = 0 |
| 28 |
self.numDocPriMethods = 0 |
| 29 |
self.numAttributes = 0 |
| 30 |
self.numDocAttributes = 0 |
| 31 |
self.numFunctions = 0 |
| 32 |
self.numDocFunctions = 0 |
| 33 |
self.numVariables = 0 |
| 34 |
self.numDocVariables = 0 |
| 35 |
self.numParams = 0 |
| 36 |
def print(self): |
| 37 |
numMethods = self.numPubMethods + self.numProMethods + self.numPriMethods |
| 38 |
numDocMethods = self.numDocPubMethods + self.numDocProMethods + self.numDocPriMethods |
| 39 |
print("Metrics:") |
| 40 |
print("-----------------------------------") |
| 41 |
if self.numClasses>0: |
| 42 |
print("Classes: {:=10} ({} documented)".format(self.numClasses,self.numDocClasses)) |
| 43 |
if self.numStructs>0: |
| 44 |
print("Structs: {:=10}".format(self.numStructs)) |
| 45 |
if self.numUnions>0: |
| 46 |
print("Unions: {:=10}".format(self.numUnions)) |
| 47 |
if self.numExceptions>0: |
| 48 |
print("Exceptions: {:=10}".format(self.numExceptions)) |
| 49 |
if self.numNamespaces>0: |
| 50 |
print("Namespaces: {:=10}".format(self.numNamespaces)) |
| 51 |
if self.numFiles>0: |
| 52 |
print("Files: {:=10} ({} documented)".format(self.numFiles,self.numDocFiles)) |
| 53 |
if self.numGroups>0: |
| 54 |
print("Groups: {:=10}".format(self.numGroups)) |
| 55 |
if self.numPages>0: |
| 56 |
print("Pages: {:=10}".format(self.numPages)) |
| 57 |
if numMethods>0: |
| 58 |
print("Methods: {:=10} ({} documented)".format(numMethods,numDocMethods)) |
| 59 |
if self.numPubMethods>0: |
| 60 |
print(" Public: {:=10} ({} documented)".format(self.numPubMethods,self.numDocPubMethods)) |
| 61 |
if self.numProMethods>0: |
| 62 |
print(" Protected: {:=10} ({} documented)".format(self.numProMethods,self.numDocProMethods)) |
| 63 |
if self.numPriMethods>0: |
| 64 |
print(" Private: {:=10} ({} documented)".format(self.numPriMethods,self.numDocPriMethods)) |
| 65 |
if self.numFunctions>0: |
| 66 |
print("Functions: {:=10} ({} documented)".format(self.numFunctions,self.numDocFunctions)) |
| 67 |
if self.numAttributes>0: |
| 68 |
print("Attributes: {:=10} ({} documented)".format(self.numAttributes,self.numDocAttributes)) |
| 69 |
if self.numVariables>0: |
| 70 |
print("Variables: {:=10} ({} documented)".format(self.numVariables,self.numDocVariables)) |
| 71 |
if self.numParams>0: |
| 72 |
print("Params: {:=10}".format(self.numParams)) |
| 73 |
print("-----------------------------------") |
| 74 |
if self.numClasses>0: |
| 75 |
print("Avg. #methods/compound: {:=10}".format(float(numMethods)/float(self.numClasses))) |
| 76 |
if numMethods>0: |
| 77 |
print("Avg. #params/method: {:=10}".format(float(self.numParams)/float(numMethods))) |
| 78 |
print("-----------------------------------") |
| 79 |
|
| 80 |
|
| 81 |
def description_is_empty(description): |
| 82 |
for content in description.content_: |
| 83 |
if content.getCategory()==MixedContainer.CategoryText: |
| 84 |
if not content.getValue().isspace(): |
| 85 |
return False # non space-only text |
| 86 |
elif not content.getCategory()==MixedContainer.CategoryNone: |
| 87 |
return False # some internal object like a paragraph |
| 88 |
return True |
| 89 |
|
| 90 |
def is_documented(definition): |
| 91 |
return not description_is_empty(definition.get_briefdescription()) or \ |
| 92 |
not description_is_empty(definition.get_detaileddescription()) |
| 93 |
|
| 94 |
def section_is_protected(sectionkind): |
| 95 |
return sectionkind in [DoxSectionKind.PROTECTEDTYPE, |
| 96 |
DoxSectionKind.PROTECTEDFUNC, |
| 97 |
DoxSectionKind.PROTECTEDATTRIB, |
| 98 |
DoxSectionKind.PROTECTEDSLOT, |
| 99 |
DoxSectionKind.PROTECTEDSTATICFUNC, |
| 100 |
DoxSectionKind.PROTECTEDSTATICATTRIB] |
| 101 |
|
| 102 |
def section_is_private(sectionkind): |
| 103 |
return sectionkind in [DoxSectionKind.PRIVATETYPE, |
| 104 |
DoxSectionKind.PRIVATEFUNC, |
| 105 |
DoxSectionKind.PRIVATEATTRIB, |
| 106 |
DoxSectionKind.PRIVATESLOT, |
| 107 |
DoxSectionKind.PRIVATESTATICFUNC, |
| 108 |
DoxSectionKind.PRIVATESTATICATTRIB] |
| 109 |
|
| 110 |
def section_is_public(sectionkind): |
| 111 |
return not section_is_protected(sectionkind) and not section_is_private(sectionkind) |
| 112 |
|
| 113 |
def linked_text_to_string(linkedtext): |
| 114 |
str='' |
| 115 |
if linkedtext: |
| 116 |
for text_or_ref in linkedtext.content_: |
| 117 |
if text_or_ref.getCategory()==MixedContainer.CategoryText: |
| 118 |
str+=text_or_ref.getValue() |
| 119 |
else: |
| 120 |
str+=text_or_ref.getValue().get_valueOf_() |
| 121 |
return str |
| 122 |
|
| 123 |
def parse_members(compounddef,sectiondef,metrics): |
| 124 |
functionLikeKind = [DoxMemberKind.FUNCTION, |
| 125 |
DoxMemberKind.PROTOTYPE, |
| 126 |
DoxMemberKind.SIGNAL, |
| 127 |
DoxMemberKind.SLOT, |
| 128 |
DoxMemberKind.DCOP] |
| 129 |
variableLikeKind = [DoxMemberKind.VARIABLE, DoxMemberKind.PROPERTY] |
| 130 |
for memberdef in sectiondef.get_memberdef(): |
| 131 |
if compounddef.get_kind() in [DoxCompoundKind.CLASS, DoxCompoundKind.STRUCT, DoxCompoundKind.INTERFACE]: |
| 132 |
if memberdef.get_kind() in functionLikeKind: |
| 133 |
if section_is_public(sectiondef.get_kind()): |
| 134 |
metrics.numPubMethods+=1 |
| 135 |
if is_documented(memberdef): |
| 136 |
metrics.numDocPubMethods+=1 |
| 137 |
elif section_is_protected(sectiondef.get_kind()): |
| 138 |
metrics.numProMethods+=1 |
| 139 |
if is_documented(memberdef): |
| 140 |
metrics.numDocProMethods+=1 |
| 141 |
elif section_is_private(sectiondef.get_kind()): |
| 142 |
metrics.numPriMethods+=1 |
| 143 |
if is_documented(memberdef): |
| 144 |
metrics.numDocPriMethods+=1 |
| 145 |
elif memberdef.get_kind() in variableLikeKind: |
| 146 |
metrics.numAttributes+=1 |
| 147 |
if is_documented(memberdef): |
| 148 |
metrics.numDocAttributes+=1 |
| 149 |
elif compounddef.get_kind() in [DoxCompoundKind.FILE, DoxCompoundKind.NAMESPACE]: |
| 150 |
if memberdef.get_kind() in functionLikeKind: |
| 151 |
metrics.numFunctions+=1 |
| 152 |
if is_documented(memberdef): |
| 153 |
metrics.numDocFunctions+=1 |
| 154 |
elif memberdef.get_kind() in variableLikeKind: |
| 155 |
metrics.numVariables+=1 |
| 156 |
if is_documented(memberdef): |
| 157 |
metrics.numDocVariables+=1 |
| 158 |
#for param in memberdef.get_param(): |
| 159 |
# name = '' |
| 160 |
# if param.get_defname(): |
| 161 |
# name = param.get_defname() |
| 162 |
# if param.get_declname(): |
| 163 |
# name = param.get_declname() |
| 164 |
# print("param '{}':'{}'".format(linked_text_to_string(param.get_type()),name)) |
| 165 |
metrics.numParams+=len(memberdef.get_param()) |
| 166 |
if memberdef.get_type() and memberdef.get_type()!="void" and linked_text_to_string(memberdef.get_type()): |
| 167 |
metrics.numParams+=1 # count non-void return types as well |
| 168 |
#print("returns '{}'".format(linked_text_to_string(memberdef.get_type()))) |
| 169 |
|
| 170 |
def parse_sections(compounddef,metrics): |
| 171 |
for sectiondef in compounddef.get_sectiondef(): |
| 172 |
parse_members(compounddef,sectiondef,metrics) |
| 173 |
|
| 174 |
def parse_compound(inDirName,baseName,metrics): |
| 175 |
rootObj = doxmlparser.compound.parse(inDirName+"/"+baseName+".xml",True) |
| 176 |
for compounddef in rootObj.get_compounddef(): |
| 177 |
kind = compounddef.get_kind() |
| 178 |
if kind==DoxCompoundKind.CLASS: |
| 179 |
metrics.numClasses+=1 |
| 180 |
if is_documented(compounddef): |
| 181 |
metrics.numDocClasses+=1 |
| 182 |
elif kind==DoxCompoundKind.STRUCT: |
| 183 |
metrics.numStructs+=1 |
| 184 |
elif kind==DoxCompoundKind.UNION: |
| 185 |
metrics.numUnions+=1 |
| 186 |
elif kind==DoxCompoundKind.INTERFACE: |
| 187 |
metrics.numInterfaces+=1 |
| 188 |
elif kind==DoxCompoundKind.EXCEPTION: |
| 189 |
metrics.numExceptions+=1 |
| 190 |
elif kind==DoxCompoundKind.NAMESPACE: |
| 191 |
metrics.numNamespaces+=1 |
| 192 |
elif kind==DoxCompoundKind.FILE: |
| 193 |
metrics.numFiles+=1 |
| 194 |
if is_documented(compounddef): |
| 195 |
metrics.numDocFiles+=1 |
| 196 |
elif kind==DoxCompoundKind.GROUP: |
| 197 |
metrics.numGroups+=1 |
| 198 |
elif kind==DoxCompoundKind.PAGE: |
| 199 |
metrics.numPages+=1 |
| 200 |
else: |
| 201 |
continue |
| 202 |
parse_sections(compounddef,metrics) |
| 203 |
|
| 204 |
def parse_index(inDirName): |
| 205 |
metrics = Metrics() |
| 206 |
rootObj = doxmlparser.index.parse(inDirName+"/index.xml",True) |
| 207 |
for compound in rootObj.get_compound(): # for each compound defined in the index |
| 208 |
print("Processing {0}...".format(compound.get_name())) |
| 209 |
parse_compound(inDirName,compound.get_refid(),metrics) |
| 210 |
metrics.print() |
| 211 |
|
| 212 |
def usage(): |
| 213 |
print("Usage {0} <xml_output_dir>".format(sys.argv[0])) |
| 214 |
sys.exit(1) |
| 215 |
|
| 216 |
def main(): |
| 217 |
args = sys.argv[1:] |
| 218 |
if len(args)==1: |
| 219 |
parse_index(args[0]) |
| 220 |
else: |
| 221 |
usage() |
| 222 |
|
| 223 |
if __name__ == '__main__': |
| 224 |
main() |
| 225 |
|