Back porting Diane's ethelp.py from the trunk to v0.1.x branch
authorBrandon King <kingb@caltech.edu>
Fri, 6 Jun 2008 21:21:45 +0000 (21:21 +0000)
committerBrandon King <kingb@caltech.edu>
Fri, 6 Jun 2008 21:21:45 +0000 (21:21 +0000)
gaworkflow/util/ethelp.py [new file with mode: 0644]

diff --git a/gaworkflow/util/ethelp.py b/gaworkflow/util/ethelp.py
new file mode 100644 (file)
index 0000000..19f6c9f
--- /dev/null
@@ -0,0 +1,32 @@
+"""
+ElementTree helper functions
+"""
+def indent(elem, level=0):
+    """
+    reformat an element tree to be 'pretty' (indented)
+    """
+    i = "\n" + level*"  "
+    if len(elem):
+        if not elem.text or not elem.text.strip():
+            elem.text = i + "  "
+        for child in elem:
+            indent(child, level+1)
+        # we don't want the closing tag indented too far
+        child.tail = i
+        if not elem.tail or not elem.tail.strip():
+            elem.tail = i
+    else:
+        if level and (not elem.tail or not elem.tail.strip()):
+            elem.tail = i
+
+def flatten(elem, include_tail=0):
+    """
+    Extract the text from an element tree 
+    (AKA extract the text that not part of XML tags)
+    """
+    text = elem.text or ""
+    for e in elem:
+        text += flatten(e, 1)
+    if include_tail and elem.tail: text += elem.tail
+    return text
+