[Opensrf-commits] r1168 - trunk/src/python/osrf
svn at svn.open-ils.org
svn at svn.open-ils.org
Sun Dec 16 00:03:33 EST 2007
Author: dbs
Date: 2007-12-15 23:42:25 -0500 (Sat, 15 Dec 2007)
New Revision: 1168
Added:
trunk/src/python/osrf/xml.py
Removed:
trunk/src/python/osrf/utils.py
Log:
This file will hold only OpenSRF XML functions.
osrfObjectFindPath will be moved to the net_obj module.
Deleted: trunk/src/python/osrf/utils.py
===================================================================
--- trunk/src/python/osrf/utils.py 2007-12-14 19:01:12 UTC (rev 1167)
+++ trunk/src/python/osrf/utils.py 2007-12-16 04:42:25 UTC (rev 1168)
@@ -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
-
-
-
-
Copied: trunk/src/python/osrf/xml.py (from rev 1165, trunk/src/python/osrf/utils.py)
===================================================================
--- trunk/src/python/osrf/xml.py (rev 0)
+++ trunk/src/python/osrf/xml.py 2007-12-16 04:42:25 UTC (rev 1168)
@@ -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
+
+
+
+
More information about the opensrf-commits
mailing list