Browse code

* S3/Utils.py: Added getDictFromTree() and appendXmlTextNode()

git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3cmd/branches/s3cmd-airlock@340 830e0280-6d2a-0410-9c65-932aecc39d9d

Michal Ludvig authored on 2009/01/16 21:53:49
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+2009-01-17  Michal Ludvig  <michal@logix.cz>
2
+
3
+	* S3/Utils.py: Added getDictFromTree() and appendXmlTextNode()
4
+
1 5
 2009-01-16  Michal Ludvig  <michal@logix.cz>
2 6
 
3 7
 	* S3/CloudFront.py: New module for CloudFront support.
... ...
@@ -62,7 +62,21 @@ def getListFromXml(xml, node):
62 62
 	tree = getTreeFromXml(xml)
63 63
 	nodes = tree.findall('.//%s' % (node))
64 64
 	return parseNodes(nodes)
65
-	
65
+
66
+def getDictFromTree(tree):
67
+	ret_dict = {}
68
+	for child in tree.getchildren():
69
+		if child.getchildren():
70
+			## Complex-type child. We're not interested
71
+			continue
72
+		if ret_dict.has_key(child.tag):
73
+			if not type(ret_dict[child.tag]) == list:
74
+				ret_dict[child.tag] = [ret_dict[child.tag]]
75
+			ret_dict[child.tag].append(child.text)
76
+		else:
77
+			ret_dict[child.tag] = child.text
78
+	return ret_dict
79
+
66 80
 def getTextFromXml(xml, xpath):
67 81
 	tree = getTreeFromXml(xml)
68 82
 	if tree.tag.endswith(xpath):
... ...
@@ -74,6 +88,20 @@ def getRootTagName(xml):
74 74
 	tree = getTreeFromXml(xml)
75 75
 	return tree.tag
76 76
 
77
+def xmlTextNode(tag_name, text):
78
+	el = ET.Element(tag_name)
79
+	el.text = unicode(text)
80
+	return el
81
+
82
+def appendXmlTextNode(tag_name, text, parent):
83
+	"""
84
+	Creates a new <tag_name> Node and sets
85
+	its content to 'text'. Then appends the
86
+	created Node to 'parent' element if given.
87
+	Returns the newly created Node.
88
+	"""
89
+	parent.append(xmlTextNode(tag_name, text))
90
+
77 91
 def dateS3toPython(date):
78 92
 	date = re.compile("\.\d\d\dZ").sub(".000Z", date)
79 93
 	return time.strptime(date, "%Y-%m-%dT%H:%M:%S.000Z")