Permalink
Browse files

Add basic ElementTree pretty printer.

  • Loading branch information...
1 parent aa09688 commit 48f8f83dbd70db8e6e246442e83dea8e02f577cc @twpayne twpayne committed Apr 11, 2010
Showing with 14 additions and 0 deletions.
  1. +14 −0 igc2kmz/etree.py
View
@@ -28,3 +28,17 @@ def __enter__(self):
def __exit__(self, type, value, traceback):
self.tb.end(self.name)
+
+
+def pretty_write(io, element, indent='\t', prefix=''):
+ attrs = ''.join(' %s="%s"' % item for item in element.items())
+ children = element.getchildren()
+ if children:
+ io.write('%s<%s%s>\n' % (prefix, element.tag, attrs))
+ for child in children:
+ pretty_write(io, child, indent, prefix + indent)
+ io.write('%s</%s>\n' % (prefix, element.tag))
+ elif element.text:
+ io.write('%s<%s%s>%s</%s>\n' % (prefix, element.tag, attrs, element.text, element.tag))
+ else:
+ io.write('%s<%s%s/>\n' % (prefix, element.tag, attrs))

0 comments on commit 48f8f83

Please sign in to comment.