From ejcasler@us.ibm.com Thu Aug 14 17:06:18 2008 Date: Thu, 14 Aug 2008 14:05:40 -0700 From: Eric Casler To: ssmoser@linux.vnet.ibm.com Subject: [PATCH] Added getChildData - returns data from child of element When passed a tag name, returns a lambda function that accepts a DOM Element and retrieves the child with the specified tag name, and returns its data. If the child is not found, it returns None. If multiple children are found, it raises an error. Signed-off-by: Eric Casler --- py/ovf/Ovf.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 files changed, 38 insertions(+), 0 deletions(-) diff --git a/py/ovf/Ovf.py b/py/ovf/Ovf.py index 41798a9..ad3ff1c 100644 --- a/py/ovf/Ovf.py +++ b/py/ovf/Ovf.py @@ -145,6 +145,44 @@ def getParentAttribute(attrName): """ return lambda xmlElement: xmlElement.parentNode.getAttribute(attrName) +def getChildData(elemTagName): + """ + Returns a function that accepts a DOM Element and returns the data from + a child element with the uniquely specified tag name. + + @param elemTagName: Element tag name + @type elemTagName: String + + @return: function for getting child data + @rtype: lambda function + """ + def __helper(elem, childTagName): + """ + Returns data from the unique child of the passed in element + with the given tag name. + + @param xmlElement: child Element + @type xmlElement: DOM Element + + @param childTagName: child Element tag name + @type childTagName: String + + @return: child data + @rtype: String + + @raise ValueError: + """ + elemList = elem.getElementsByTagName(childTagName) + if len(elemList) == 1: + return elemList[0].firstChild.data + elif len(elemList) == 0: + return None + else: + raise ValueError("getChildData: Non-unique tag name: " + + str(childTagName)) + + return lambda elem: __helper(elem, elemTagName) + def getContentEntities(ovfNode, ovfId=None): """ Returns a list of OVF Entities, optionally restricted by id. -- 1.5.4.3