summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Pipping <sebastian@pipping.org>2010-03-22 21:32:08 +0100
committerSebastian Pipping <sebastian@pipping.org>2010-03-22 21:36:38 +0100
commit8c4d7440c6d47f7ed690edafb7c0af53742f3297 (patch)
tree46131f958514dd5e9ee48ace9987441b5ddfb2cc /layman/dbtools/sharedutils.py
parentrepsitories.xml converter: New command line interface (diff)
downloadrepositories-xml-format-8c4d7440c6d47f7ed690edafb7c0af53742f3297.tar.gz
repositories-xml-format-8c4d7440c6d47f7ed690edafb7c0af53742f3297.tar.bz2
repositories-xml-format-8c4d7440c6d47f7ed690edafb7c0af53742f3297.zip
Move library files to layman/dbtools/
Diffstat (limited to 'layman/dbtools/sharedutils.py')
-rw-r--r--layman/dbtools/sharedutils.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/layman/dbtools/sharedutils.py b/layman/dbtools/sharedutils.py
new file mode 100644
index 0000000..9837b76
--- /dev/null
+++ b/layman/dbtools/sharedutils.py
@@ -0,0 +1,45 @@
+# Copyright (C) 2009 Sebastian Pipping <sebastian@pipping.org>
+# Licensed under GPL 2 or later
+
+import codecs
+
+# From <http://effbot.org/zone/element-lib.htm>
+# BEGIN
+def indent(elem, level=0):
+ i = "\n" + level*" "
+ if len(elem):
+ if not elem.text or not elem.text.strip():
+ elem.text = i + " "
+ if not elem.tail or not elem.tail.strip():
+ elem.tail = i
+ for elem in elem:
+ indent(elem, level+1)
+ 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
+# END
+
+def recurse_print(e, level=0):
+ t = e.text
+ if t:
+ if t == '' or t.isspace():
+ t = None
+ else:
+ t = t.strip('\n\r')
+ print '%s<%s>%s' % (' '*level, e.tag, t and (' .. %s' % to_ascii(t)) or '')
+ for k, v in sorted(e.attrib.items()):
+ print '%s| %s = %s' % (' '*(level+2), k, v)
+ for c in e.getchildren():
+ recurse_print(c, level=level + 1)
+
+def to_ascii(o, current_encoding='utf-8'):
+ if not isinstance(o, basestring):
+ return o
+
+ if isinstance(o, unicode):
+ s = o
+ else:
+ s = unicode(o, current_encoding)
+ return codecs.encode(s, 'ascii', 'ignore')