def test_strip_ns(self): """Test the strip_ns() function.""" self.assertEqual( "required", XML.strip_ns("{http://schemas.dmtf.org/ovf/envelope/1}required")) self.assertEqual( "required", XML.strip_ns("required"))
def test_strip_ns(self): """Test the strip_ns() function.""" self.assertEqual( "required", XML.strip_ns("{http://schemas.dmtf.org/ovf/envelope/1}required")) self.assertEqual("required", XML.strip_ns("required")) self.assertLogged(levelname="ERROR", msg="no associated namespace")
def test_strip_ns(self): """Test the strip_ns() function.""" self.assertEqual( "required", XML.strip_ns("{http://schemas.dmtf.org/ovf/envelope/1}required")) self.assertEqual( "required", XML.strip_ns("required")) self.assertLogged(levelname="ERROR", msg="no associated namespace")
def add_item(self, item): """Add the given ``Item`` element to this OVFItem. Args: item (xml.etree.ElementTree.Element): XML ``Item`` element Raises: ValueUnsupportedError: if the ``item`` is not a recognized Item variant. OVFItemDataError: if the new Item conflicts with existing data already in the OVFItem. """ logger.spam("Adding new %s", item.tag) self.namespace = self.namespace_for_item_tag(item.tag) if not self.namespace: raise ValueUnsupportedError("item", item.tag, "Item, StorageItem, EthernetPortItem") profiles = set(item.get(self.ITEM_CONFIG, "").split()) # Store any attributes of the Item itself: for (attrib, value) in item.attrib.items(): if attrib == self.ITEM_CONFIG: continue attrib_string = attrib + self.ATTRIB_KEY_SUFFIX self.set_property(attrib_string, value, profiles, overwrite=False) # Store any child elements of the Item. # We save the ElementName and Description elements for last because # they may include references to the VirtualQuantity, ResourceSubType, # and/or Connection entries, which we won't know until we process them. children = list(item) name_child = next((child for child in children if XML.strip_ns(child.tag) == self.ELEMENT_NAME), None) desc_child = next( (child for child in children if XML.strip_ns(child.tag) == self.ITEM_DESCRIPTION), None) if name_child is not None: children.remove(name_child) children.append(name_child) # Description is *after* name because it may reference name if desc_child is not None: children.remove(desc_child) children.append(desc_child) for child in children: tag = XML.strip_ns(child.tag) if tag not in self.ITEM_CHILDREN: # Non-standard elements may not follow the standard rules - # for example, VMware OVF extensions may have multiple # vmw:Config elements, each distinguished by its vmw:key attr. # Rather than try to guess how these items do or do not match, # we simply store the whole item self.set_property((ET.tostring(child).decode().strip() + self.ELEMENT_KEY_SUFFIX), ET.tostring(child).decode(), profiles, overwrite=False) continue # Store the value of this element: self.set_property(tag, child.text, profiles, overwrite=False) # Store any attributes of this element for (attrib, value) in child.attrib.items(): attrib_string = tag + "_attrib_" + attrib self.set_property(attrib_string, value, profiles, overwrite=False) self.modified = True logger.spam("Added %s - new status:\n%s", item.tag, str(self)) self.validate()
def add_item(self, item): """Add the given ``Item`` element to this OVFItem. Args: item (xml.etree.ElementTree.Element): XML ``Item`` element Raises: ValueUnsupportedError: if the ``item`` is not a recognized Item variant. OVFItemDataError: if the new Item conflicts with existing data already in the OVFItem. """ logger.spam("Adding new %s", item.tag) self.namespace = self.namespace_for_item_tag(item.tag) if not self.namespace: raise ValueUnsupportedError("item", item.tag, "Item, StorageItem, EthernetPortItem") profiles = set(item.get(self.ITEM_CONFIG, "").split()) # Store any attributes of the Item itself: for (attrib, value) in item.attrib.items(): if attrib == self.ITEM_CONFIG: continue attrib_string = attrib + self.ATTRIB_KEY_SUFFIX self.set_property(attrib_string, value, profiles, overwrite=False) # Store any child elements of the Item. # We save the ElementName and Description elements for last because # they may include references to the VirtualQuantity, ResourceSubType, # and/or Connection entries, which we won't know until we process them. children = list(item) name_child = next( (child for child in children if XML.strip_ns(child.tag) == self.ELEMENT_NAME), None) desc_child = next( (child for child in children if XML.strip_ns(child.tag) == self.ITEM_DESCRIPTION), None) if name_child is not None: children.remove(name_child) children.append(name_child) # Description is *after* name because it may reference name if desc_child is not None: children.remove(desc_child) children.append(desc_child) for child in children: tag = XML.strip_ns(child.tag) if tag not in self.ITEM_CHILDREN: # Non-standard elements may not follow the standard rules - # for example, VMware OVF extensions may have multiple # vmw:Config elements, each distinguished by its vmw:key attr. # Rather than try to guess how these items do or do not match, # we simply store the whole item self.set_property((ET.tostring(child).decode().strip() + self.ELEMENT_KEY_SUFFIX), ET.tostring(child).decode(), profiles, overwrite=False) continue # Store the value of this element: self.set_property(tag, child.text, profiles, overwrite=False) # Store any attributes of this element for (attrib, value) in child.attrib.items(): attrib_string = tag + "_attrib_" + attrib self.set_property(attrib_string, value, profiles, overwrite=False) self.modified = True logger.spam("Added %s - new status:\n%s", item.tag, str(self)) self.validate()