def test_TemplateXMLTreeBuilder_nosub(self): txtb = xml_utils.TemplateXMLTreeBuilder() # elementree pukes on identifiers starting with $ txtb.feed(self.RESULTCHECK) et = txtb.close() result = ElementTree.tostring(et) self.assertEqual(result, self.RESULTCHECK)
def get_disk_info(vm_name, options): """ Return disk info dict. :param vm_name: vm name :param options: domblkinfo command options :return: dict of disk info """ disk_info_dict = {} if "--inactive" in options: option = "--inactive" else: option = "" sourcelist = vm_xml.VMXML.get_disk_source(vm_name, option) new_disk = disk.Disk() for i in range(len(sourcelist)): new_disk['xml'] = ElementTree.tostring(sourcelist[i]) logging.debug("Current disk xml is: %s" % new_disk.xmltreefile) if hasattr(new_disk, 'source'): for key in list(new_disk.source.attrs.keys()): if key in SOURCE_LIST: source_path = new_disk.source.attrs[key] else: source_path = '-' disk_info_dict[i] = [ new_disk.type_name, new_disk.device, new_disk.target['dev'], source_path ] return disk_info_dict
def get_disk_info(vm_name, options): """ Return disk info dict. :param vm_name: vm name :param options: domblkinfo command options :return: dict of disk info """ disk_info_dict = {} if "--inactive" in options: option = "--inactive" else: option = "" sourcelist = vm_xml.VMXML.get_disk_source(vm_name, option) new_disk = disk.Disk() for i in range(len(sourcelist)): new_disk['xml'] = ElementTree.tostring(sourcelist[i]) logging.debug("Current disk xml is: %s" % new_disk.xmltreefile) for key in new_disk.source.attrs.keys(): if key in SOURCE_LIST: source_path = new_disk.source.attrs[key] disk_info_dict[i] = [new_disk.type_name, new_disk.device, new_disk.target['dev'], source_path] return disk_info_dict
def __call__(self): # Parent structure cannot be pre-determined as in other classes parent = self.xmltreefile().find(self.parent_xpath) if parent is None: # Used as "undefined" signal, raising exception may # not be appropriate when other accessors are used # to generate missing structure. return None result = [] # Give user-defined marshal functions a way to act on # item order if needed, and/or help with error reporting. index = 0 # user-defined marshal functions might want to use # index numbers to filter/skip certain elements # but also support specific item ordering. for child in parent.getchildren(): # Call user-defined helper to translate Element # into simple pre-defined format. # To support converting xml elements directly to a list of # xml objects, first create xmltreefile for new object if self.has_subclass: new_xmltreefile = xml_utils.XMLTreeFile( element_tree.tostring(child)) item = self.marshal_to(child.tag, new_xmltreefile, index, self.libvirtxml) else: try: # To support an optional text parameter, compatible # with no text parameter. item = self.marshal_to(child.tag, dict(list(child.items())), index, self.libvirtxml, child.text) except TypeError: item = self.marshal_to(child.tag, dict(list(child.items())), index, self.libvirtxml) if item is not None: result.append(item) # Always use absolute index (even if item was None) index += 1 return result
def __call__(self): parent = self.xmltreefile().find(self.parent_xpath) if parent is None: raise xcepts.LibvirtXMLNotFoundError("Parent element %s not " "found" % self.parent_xpath) # Don't delete while traversing list todel = [] index = 0 for child in parent.getchildren(): # To support directly deleting xml elements xml objects, # first create xmltreefile for new object if self.has_subclass: new_xmltreefile = xml_utils.XMLTreeFile( element_tree.tostring(child)) item = self.marshal_to(child.tag, new_xmltreefile, index, self.libvirtxml) else: try: # To support an optional text parameter, compatible # with no text parameter. item = self.marshal_to(child.tag, dict(list(child.items())), index, self.libvirtxml, child.text) except TypeError: item = self.marshal_to(child.tag, dict(list(child.items())), index, self.libvirtxml) # Always use absolute index (even if item was None) index += 1 # Account for case where child elements are mixed in # with other elements not supported by this class. # Also permits marshal functions to do element filtering # if the class should only address specificly attributed # elements. if item is not None: todel.append(child) for child in todel: parent.remove(child)
def get_element_string(self, xpath): """ Returns the string for the element on xpath. """ return ElementTree.tostring(self.find(xpath))
def test_TemplateXML(self): tx = xml_utils.TemplateXML(self.FULLREPLACE, **self.MAPPING) et = ElementTree.ElementTree(None, tx.name) check = ElementTree.tostring(et.getroot()) self.assertEqual(check, self.RESULTCHECK)
def test_MappingTreeBuilder_standalone(self): txtb = xml_utils.TemplateXMLTreeBuilder(**self.MAPPING) txtb.feed(self.FULLREPLACE) et = txtb.close() result = ElementTree.tostring(et) self.assertEqual(result, self.RESULTCHECK)
def get_element_string(self, xpath, index=0): """ Returns the string for the element on xpath. """ return ElementTree.tostring(self.findall(xpath)[index])