From: dbs Date: Sun, 16 Dec 2007 05:59:19 +0000 (+0000) Subject: xml.py conflicts with other xml module namespace (boo). Renaming. X-Git-Tag: osrf_rel_2_0_1~798 X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=65c847c56d5cc7e95bcc36500011b31226b4cd43;p=OpenSRF.git xml.py conflicts with other xml module namespace (boo). Renaming. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1169 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/python/osrf/xml.py b/src/python/osrf/xml.py deleted file mode 100644 index ed24a87..0000000 --- a/src/python/osrf/xml.py +++ /dev/null @@ -1,113 +0,0 @@ -import xml.dom.minidom, re - -def osrfXMLFileToObject(filename): - """Turns the contents of an XML file into a Python object""" - doc = xml.dom.minidom.parse(filename) - obj = osrfXMLNodeToObject(doc.documentElement) - doc.unlink() - return obj - -def osrfXMLStringToObject(string): - """Turns an XML string into a Python object""" - doc = xml.dom.minidom.parseString(string) - obj = osrfXMLNodeToObject(doc.documentElement) - doc.unlink() - return obj - -def osrfXMLNodeToObject(xmlNode): - """Turns an XML node into a Python object""" - obj = {} - - if xmlNode.nodeType != xmlNode.ELEMENT_NODE: - return obj - - done = False - nodeName = xmlNode.nodeName - - for nodeChild in xmlNode.childNodes: - if nodeChild.nodeType == xmlNode.ELEMENT_NODE: - subObj = osrfXMLNodeToObject(nodeChild); - __appendChildNode(obj, nodeName, nodeChild.nodeName, subObj) - done = True - - for attr in xmlNode.attributes.values(): - __appendChildNode(obj, nodeName, attr.name, dict([(attr.name, attr.value)])) - - - if not done and len(xmlNode.childNodes) > 0: - # If the node has no element children, clean up the text - # content and use that as the data - textNode = xmlNode.childNodes[0] # extract the text node - data = unicode(textNode.nodeValue).replace('^\s*','') - data = data.replace('\s*$','') - - if nodeName in obj: - # the current element contains attributes and text - obj[nodeName]['#text'] = data - else: - # the current element contains text only - obj[nodeName] = data - - return obj - - -def __appendChildNode(obj, nodeName, childName, subObj): - """ If a node has element children, create a new sub-object - for this node, attach an array for each type of child - and recursively collect the children data into the array(s) """ - - if not obj.has_key(nodeName): - obj[nodeName] = {} - - if not obj[nodeName].has_key(childName): - # we've encountered 1 sub-node with nodeChild's name - if childName in subObj: - obj[nodeName][childName] = subObj[childName] - else: - obj[nodeName][childName] = None - - else: - if isinstance(obj[nodeName][childName], list): - # we already have multiple sub-nodes with nodeChild's name - obj[nodeName][childName].append(subObj[childName]) - - else: - # we already have 1 sub-node with nodeChild's name, make - # it a list and append the current node - val = obj[nodeName][childName] - obj[nodeName][childName] = [ val, subObj[childName] ] - - -def osrfObjectFindPath(obj, path, idx=None): - """Searches an object along the given path for a value to return. - - Path separaters can be '/' or '.', '/' is tried first.""" - - parts = [] - - if re.search('/', path): - parts = path.split('/') - else: - parts = path.split('.') - - for part in parts: - try: - o = obj[part] - except Exception: - return None - if isinstance(o,str): - return o - if isinstance(o,list): - if( idx != None ): - return o[idx] - return o - if isinstance(o,dict): - obj = o - else: - return o - - return obj - - - - diff --git a/src/python/osrf/xml_obj.py b/src/python/osrf/xml_obj.py new file mode 100644 index 0000000..ed24a87 --- /dev/null +++ b/src/python/osrf/xml_obj.py @@ -0,0 +1,113 @@ +import xml.dom.minidom, re + +def osrfXMLFileToObject(filename): + """Turns the contents of an XML file into a Python object""" + doc = xml.dom.minidom.parse(filename) + obj = osrfXMLNodeToObject(doc.documentElement) + doc.unlink() + return obj + +def osrfXMLStringToObject(string): + """Turns an XML string into a Python object""" + doc = xml.dom.minidom.parseString(string) + obj = osrfXMLNodeToObject(doc.documentElement) + doc.unlink() + return obj + +def osrfXMLNodeToObject(xmlNode): + """Turns an XML node into a Python object""" + obj = {} + + if xmlNode.nodeType != xmlNode.ELEMENT_NODE: + return obj + + done = False + nodeName = xmlNode.nodeName + + for nodeChild in xmlNode.childNodes: + if nodeChild.nodeType == xmlNode.ELEMENT_NODE: + subObj = osrfXMLNodeToObject(nodeChild); + __appendChildNode(obj, nodeName, nodeChild.nodeName, subObj) + done = True + + for attr in xmlNode.attributes.values(): + __appendChildNode(obj, nodeName, attr.name, dict([(attr.name, attr.value)])) + + + if not done and len(xmlNode.childNodes) > 0: + # If the node has no element children, clean up the text + # content and use that as the data + textNode = xmlNode.childNodes[0] # extract the text node + data = unicode(textNode.nodeValue).replace('^\s*','') + data = data.replace('\s*$','') + + if nodeName in obj: + # the current element contains attributes and text + obj[nodeName]['#text'] = data + else: + # the current element contains text only + obj[nodeName] = data + + return obj + + +def __appendChildNode(obj, nodeName, childName, subObj): + """ If a node has element children, create a new sub-object + for this node, attach an array for each type of child + and recursively collect the children data into the array(s) """ + + if not obj.has_key(nodeName): + obj[nodeName] = {} + + if not obj[nodeName].has_key(childName): + # we've encountered 1 sub-node with nodeChild's name + if childName in subObj: + obj[nodeName][childName] = subObj[childName] + else: + obj[nodeName][childName] = None + + else: + if isinstance(obj[nodeName][childName], list): + # we already have multiple sub-nodes with nodeChild's name + obj[nodeName][childName].append(subObj[childName]) + + else: + # we already have 1 sub-node with nodeChild's name, make + # it a list and append the current node + val = obj[nodeName][childName] + obj[nodeName][childName] = [ val, subObj[childName] ] + + +def osrfObjectFindPath(obj, path, idx=None): + """Searches an object along the given path for a value to return. + + Path separaters can be '/' or '.', '/' is tried first.""" + + parts = [] + + if re.search('/', path): + parts = path.split('/') + else: + parts = path.split('.') + + for part in parts: + try: + o = obj[part] + except Exception: + return None + if isinstance(o,str): + return o + if isinstance(o,list): + if( idx != None ): + return o[idx] + return o + if isinstance(o,dict): + obj = o + else: + return o + + return obj + + + +