]> code.delx.au - pymsnt/blobdiff - src/xdb.py
Updated to twistfix-0.4
[pymsnt] / src / xdb.py
index 225e892b33403d76d58a7b4e8b9f12e3d3c5c646..a3e3b5d0adefd90dc431064394f97d731d0ff6f5 100644 (file)
@@ -1,12 +1,13 @@
-# Copyright 2004-2005 James Bunton <james@delx.cjb.net>
+# Copyright 2004-2006 James Bunton <james@delx.cjb.net>
 # Licensed for distribution under the GPL version 2, check COPYING for details
 
-from tlib import xmlw
+from twisted.words.xish.domish import parseFile, Element
 from debug import LogEvent, INFO, WARN
 import os
 import os.path
+import shutil
+import md5
 import config
-import legacy
 
 X = os.path.sep
 SPOOL_UMASK = 0077
@@ -18,8 +19,11 @@ def unmangle(file):
        file = "%s@%s" % ("%".join(chunks), end)
        return file
 
-def mangle(jid):
-       return jid.replace("@", "%")
+def mangle(file):
+       return file.replace("@", "%")
+
+def makeHash(file):
+       return md5.md5(file).hexdigest()[0:3]
 
 
 class XDB:
@@ -40,8 +44,8 @@ class XDB:
                if(self.mangle):
                        file = mangle(file)
                
-               hash = file[0:2]
-               document = xmlw.parseFile(self.name + X + hash + X + file + ".xml")
+               hash = makeHash(file)
+               document = parseFile(self.name + X + hash + X + file + ".xml")
                
                return document
        
@@ -50,13 +54,18 @@ class XDB:
                        file = mangle(file)
                
                prev_umask = os.umask(SPOOL_UMASK)
-               hash = file[0:2]
+               hash = makeHash(file)
                pre = self.name + X + hash + X
                if not os.path.exists(pre):
                        os.makedirs(pre)
-               f = open(pre + file + ".xml", "w")
-               f.write(text)
-               f.close()
+               try:
+                       f = open(pre + file + ".xml.new", "w")
+                       f.write(text)
+                       f.close()
+                       shutil.move(pre + file + ".xml.new", pre + file + ".xml")
+               except IOError, e:
+                       LogEvent(WARN, "", "IOError " + str(e))
+                       raise
                os.umask(prev_umask)
        
        def files(self):
@@ -95,7 +104,7 @@ class XDB:
                        except IOError:
                                pass
                        if(not document):
-                               document = xmlw.Element((None, "xdb"))
+                               document = Element((None, "xdb"))
                        
                        # Remove the existing node (if any)
                        for child in document.elements():
@@ -111,9 +120,10 @@ class XDB:
        
        def remove(self, file):
                """ Removes an XDB file """
-               file = self.name + X + file[0:2] + X + file + ".xml"
-               if(self.mangle):
+               if self.mangle:
                        file = mangle(file)
+               hash = makeHash(file)
+               file = self.name + X + hash + X + file + ".xml"
                try:
                        os.remove(file)
                except IOError, e: