Best Python code snippet using autotest_python
bkr_xml.py
Source:bkr_xml.py  
...27    try:28        return str(node.attributes[key].value)29    except Exception:30        return default31def xml_get_nodes(node, tag):32    return [n for n in node.childNodes if n.nodeName == tag]33class Recipe(object):34    def __init__(self):35        self.tasks = []36        self.id = -137        self.job_id = -138        self.exclude_dir = []39class Task(object):40    """41        Simple record to store task properties42    """43    def __init__(self):44        self.name = ''45        self.params = {}46        self.rpmName = ''47        self.rpmPath = ''48        self.timeout = ''49        self.exclude_dir = []50        self.id = -151    def __str__(self):52        return "- %s %s" % (self.name, str(self.params))53    def __repr__(self):54        return "%s %s" % (self.name, str(self.params))55    def get_param(self, key, default=None):56        if key in self.params:57            return self.params[key]58        else:59            return default60class BeakerXMLParser(object):61    """62        Handles parsing of beaker job xml63    """64    def __init__(self):65        self.recipes = {}66    def parse_from_file(self, file_name):67        log.debug('BeakerXMLParser - opening file: %s', file_name)68        f = open(os.path.expanduser(file_name), 'r')69        contents = f.read()70        f.close()71        log.debug('BeakerXMLParser - content read ok')72        return self.parse_xml(contents)73    def parse_xml(self, xml):74        """75        Returns dict, mapping hostname to recipe76        """77        log.debug('Parsing recipes')78        log.debug("xml type is %s" % type(xml))79        doc = minidom.parseString(xml)80        recipe_nodes = doc.getElementsByTagName('recipe')81        self.handle_recipes(recipe_nodes)82        log.debug('Parsing recipes ok')83        return self.recipes84    def handle_recipes(self, recipe_nodes):85        for recipe_node in recipe_nodes:86            self.handle_recipe(recipe_node)87    def handle_recipe(self, recipe_node):88        hostname = recipe_node.getAttribute('system')89        recipe = Recipe()90        recipe.id = recipe_node.getAttribute('id')91        recipe.job_id = recipe_node.getAttribute('job_id')92        log.debug('Parsing recipe with id: <%s>', recipe.id)93        #tasks = recipe.getElementsByTagName('task')94        task_nodes = xml_get_nodes(recipe_node, 'task')95        self.handle_tasks(recipe, task_nodes)96        self.recipes[hostname] = recipe97        return True98    def handle_tasks(self, recipe, task_nodes):99        for task_node in task_nodes:100            self.handle_task(recipe, task_node)101    def handle_task(self, recipe, task_node):102        task = Task()103        task.name = task_node.getAttribute('name')104        task.id = task_node.getAttribute('id')105        task.timeout = task_node.getAttribute('max_time') or task_node.getAttribute('avg_time')106        task.status = task_node.getAttribute('status')107        log.debug('Parsing task with id: <%s>', task.id)108        recipe.tasks.append(task)109        params_tags = xml_get_nodes(task_node, 'params')110        if params_tags:111            param_nodes = params_tags[0].getElementsByTagName('param')112            self.handle_task_params(task, param_nodes)113        rpm_nodes = xml_get_nodes(task_node, 'rpm')114        task.rpmName = rpm_nodes[0].getAttribute('name')115        task.rpmPath = rpm_nodes[0].getAttribute('path')116    def handle_task_params(self, task, param_nodes):117        for param_node in param_nodes:118            self.handle_task_param(task, param_node)119    def handle_task_param(self, task, param_node):120        param_name = param_node.getAttribute('name')121        param_value = param_node.getAttribute('value')...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!!
