diff options
author | Dipesh Amin <yaypunkrock@gmail.com> | 2011-08-24 10:25:03 +0100 |
---|---|---|
committer | Dipesh Amin <yaypunkrock@gmail.com> | 2011-08-24 10:25:03 +0100 |
commit | 8029332e70ed71ca386c5f411f9ab138a26a53c5 (patch) | |
tree | 950a4ef2c460258da943f113aa2ee019a3afd33c | |
parent | 9c47ad7295661bf3577dfb48d92358f11cff3276 (diff) | |
download | manamarket-8029332e70ed71ca386c5f411f9ab138a26a53c5.tar.gz manamarket-8029332e70ed71ca386c5f411f9ab138a26a53c5.tar.bz2 manamarket-8029332e70ed71ca386c5f411f9ab138a26a53c5.tar.xz manamarket-8029332e70ed71ca386c5f411f9ab138a26a53c5.zip |
Re-add pretty printing.
The problem was caused by the xml file being read in indented, so once the
prettyprintxml() was called it'd indent it again with a further 4 spaces.
I wrote a little function to clean it before the prettyprintxml() call.
-rw-r--r-- | tradey.py | 24 |
1 files changed, 19 insertions, 5 deletions
@@ -8,9 +8,20 @@ """ import time import os +import xml.dom.minidom from subprocess import call from xml.etree.ElementTree import * +def clean_xml(parse): + data = '' + pos_start = 0 + while parse.find('<', pos_start) != -1: + pos_start = parse.find('<', pos_start) + pos_end = parse.find('>', pos_start+1) + data += parse[pos_start:pos_end+1] + pos_start = pos_end + return data + class UserTree: def __init__(self): self.tree = ElementTree(file="data/user.xml") @@ -43,8 +54,10 @@ class UserTree: def save(self): # Be sure to call save() after any changes to the tree. - self.tree = ElementTree(self.root) - self.tree.write("data/user.xml") + f = open('data/user.xml', 'w') + dom = xml.dom.minidom.parseString(clean_xml(tostring(self.root))) + f.write(dom.toprettyxml(' ')) + f.close() class ItemTree: def __init__(self): @@ -94,8 +107,10 @@ class ItemTree: def save(self): # Be sure to call save() after any changes to the tree. - self.tree = ElementTree(self.root) - self.tree.write("data/sale.xml") + f = open('data/sale.xml', 'w') + dom = xml.dom.minidom.parseString(clean_xml(tostring(self.root))) + f.write(dom.toprettyxml(' ')) + f.close() def saveData(commitmessage = "commit"): # This assumes the current working directory is the tradey directory. @@ -103,6 +118,5 @@ def saveData(commitmessage = "commit"): call(["git", "commit","-a", '-m "' + commitmessage + '"']) os.chdir("..") - if __name__ == '__main__': print "Do not run this file directly. Run main.py" |