From: Brandon King Date: Fri, 6 Jun 2008 21:21:45 +0000 (+0000) Subject: Back porting Diane's ethelp.py from the trunk to v0.1.x branch X-Git-Tag: 0.1.10~3 X-Git-Url: http://woldlab.caltech.edu/gitweb/?a=commitdiff_plain;h=d010f1e57acbeedd38471f3d424c159b290109db;p=htsworkflow.git Back porting Diane's ethelp.py from the trunk to v0.1.x branch --- diff --git a/gaworkflow/util/ethelp.py b/gaworkflow/util/ethelp.py new file mode 100644 index 0000000..19f6c9f --- /dev/null +++ b/gaworkflow/util/ethelp.py @@ -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 +