Best Python code snippet using pyatom_python
doxymlparser.py
Source:doxymlparser.py  
1"""2Name: doxymlparser.py3Author: Kevin Ollivier4Licence: wxWindows licence5"""6__description__ = """7Takes the output of Doxygen XML and parses it to retrieve metadata about the classes and methods.8To create the Doxygen XML files, from the wxWidgets/docs/doxygen directory, do:9./regen.sh xml10To see the results from parsing a particular class, do:11python doxymlparser.py --report out/xml/classwx_<whatever>.xml12"""13#!/usr/bin/env python14import optparse15import os16import string17import sys18import types19from common import *20from xml.dom import minidom21class ClassDefinition:22    def __init__(self):23        self.name = ""24        self.constructors = []25        self.destructors = []26        self.methods = []27        self.brief_description = ""28        self.detailed_description = ""29        self.includes = []30        self.bases = []31        self.enums = {}32        33    def __str__(self):34        str_repr = """35Class: %s36Bases: %s37Includes: %s38Brief Description: 39%s40Detailed Description:41%s42""" % (self.name, string.join(self.bases, ", "), self.includes, self.brief_description, self.detailed_description)43        str_repr += "Methods:\n"44        45        for method in self.methods:46            str_repr += str(method)47        48        return str_repr49class MethodDefinition:50    def __init__(self):51        self.name = ""52        self.return_type = ""53        self.argsstring = ""54        self.definition = ""55        self.params = []56        self.brief_description = ""57        self.detailed_description = ""58        59    def __str__(self):60        str_repr = """61Method: %s62Return Type: %s63Params: %r64Prototype: %s65Brief Description: 66%s67Detailed Description:68%s69""" % (self.name, self.return_type, self.params, self.definition + self.argsstring, self.brief_description, self.detailed_description)70        return str_repr     71def getTextValue(node, recursive=False):72    text = ""73    for child in node.childNodes:74        if child.nodeType == child.ELEMENT_NODE and child.nodeName == "ref":75            text += getTextValue(child)76        if child.nodeType == child.TEXT_NODE:77            # Add a space to ensure we have a space between qualifiers and parameter names78            text += child.nodeValue.strip() + " "79            80    return text.strip()81def doxyMLToText(node):82    return text83class DoxyMLParser:84    def __init__(self, verbose = False):85        self.classes = []86        self.verbose = verbose87    def find_class(self, name):88        for aclass in self.classes:89            if aclass.name == name:90                return aclass91                92        return None93    def get_enums_and_functions(self, filename, aclass):94        file_path = os.path.dirname(filename)95        enum_filename = os.path.join(file_path, aclass.name[2:] + "_8h.xml")96        if os.path.exists(enum_filename):97            root = minidom.parse(enum_filename).documentElement98            for method in root.getElementsByTagName("memberdef"):99                if method.getAttribute("kind") == "enum":100                    self.parse_enum(aclass, method, root)101    def is_derived_from_base(self, aclass, abase):102        base = get_first_value(aclass.bases)103        while base and base != "":104            105            if base == abase:106                return True107                108            parentclass = self.find_class(base)109            110            if parentclass:111                base = get_first_value(parentclass.bases)112            else:113                base = None114                115        return False116    def parse(self, filename):117        self.xmldoc = minidom.parse(filename).documentElement118        for node in self.xmldoc.getElementsByTagName("compounddef"):119            new_class = self.parse_class(node)120            self.classes.append(new_class)121            self.get_enums_and_functions(filename, new_class)122            123    def parse_class(self, class_node):124        new_class = ClassDefinition()125        new_class.name = getTextValue(class_node.getElementsByTagName("compoundname")[0])126        for node in class_node.childNodes:127            if node.nodeName == "basecompoundref":128                new_class.bases.append(getTextValue(node))129            elif node.nodeName == "briefdescription":130                # let the post-processor determ131                new_class.brief_description = node.toxml()132            elif node.nodeName == "detaileddescription":133                new_class.detailed_description = node.toxml()134            elif node.nodeName == "includes":135                new_class.includes.append(getTextValue(node))136        self.parse_methods(new_class, class_node)137        return new_class138        139    def parse_enum(self, new_class, enum, root):140        enum_name = ""141        enum_values = []142        143        for node in enum.childNodes:144            if node.nodeName == "name":145                enum_name = getTextValue(node)146            elif node.nodeName == "enumvalue":147                enum_values.append(getTextValue(node.getElementsByTagName("name")[0]))148        149        new_class.enums[enum_name] = enum_values150        151    def parse_methods(self, new_class, root):152        for method in root.getElementsByTagName("memberdef"):                153            new_method = MethodDefinition()154            for node in method.childNodes:155                if node.nodeName == "name":156                    new_method.name = getTextValue(node)157                elif node.nodeName == "type":158                    new_method.return_type = getTextValue(node)159                elif node.nodeName == "definition":160                    new_method.definition = getTextValue(node)161                elif node.nodeName == "argsstring":162                    new_method.argsstring = getTextValue(node)163                elif node.nodeName == "param":164                    param = {}165                    for child in node.childNodes:166                        if child.nodeType == child.ELEMENT_NODE:167                            param[child.nodeName] = getTextValue(child)168                    new_method.params.append(param)169            170            if self.verbose:171                print "Adding %s" % (new_method.name + new_method.argsstring)172            173            if new_method.name == new_class.name:174                new_class.constructors.append(new_method)175            elif new_method.name == "~" + new_class.name:176                new_class.destructors.append(new_method)177            else:178                new_class.methods.append(new_method)179if __name__ == "__main__":180    option_dict = { 181                "report"        : (False, "Print out the classes and methods found by this script."),182                "verbose"       : (False, "Provide status updates and other information."),183              }184        185    parser = optparse.OptionParser(usage="usage: %prog [options] <doxyml files to parse>\n" + __description__, version="%prog 1.0")186    187    for opt in option_dict:188        default = option_dict[opt][0]189        190        action = "store"191        if type(default) == types.BooleanType:192            action = "store_true"193        parser.add_option("--" + opt, default=default, action=action, dest=opt, help=option_dict[opt][1])194    195    options, arguments = parser.parse_args()196    if len(arguments) < 1:197        parser.print_usage()198        sys.exit(1)199    200    doxyparse = DoxyMLParser(verbose = options.verbose)201    for arg in arguments:202        doxyparse.parse(arg)        203    if options.report:204        for aclass in doxyparse.classes:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
