]> code.delx.au - pymsnt/blobdiff - src/xdb.py
Google Talk fixes
[pymsnt] / src / xdb.py
index 83d4f748a0ec0d24b2225c0a19487dd2ebe34c3f..1e3e33e64c91a3a93f3a454a52a75809a998b674 100644 (file)
@@ -1,17 +1,14 @@
 # Copyright 2004-2005 James Bunton <james@delx.cjb.net>
 # Licensed for distribution under the GPL version 2, check COPYING for details
 
-import utils
-if(utils.checkTwisted()):
-       from twisted.xish.domish import Element
-else:
-       from tlib.domish import Element
+from tlib import xmlw
 from debug import LogEvent, INFO, WARN
 import os
 import os.path
+import shutil
 import config
-import legacy
 
+X = os.path.sep
 SPOOL_UMASK = 0077
 
 
@@ -34,7 +31,7 @@ class XDB:
        """
        def __init__(self, name, mangle=False):
                """ Creates an XDB object. If mangle is True then any '@' signs in filenames will be changed to '%' """
-               self.name = os.path.abspath(config.spooldir) + '/' + name
+               self.name = os.path.join(os.path.abspath(config.spooldir), name)
                if not os.path.exists(self.name):
                        os.makedirs(self.name)
                self.mangle = mangle
@@ -44,7 +41,7 @@ class XDB:
                        file = mangle(file)
                
                hash = file[0:2]
-               document = utils.parseFile(self.name + "/" + hash + "/" + file + ".xml")
+               document = xmlw.parseFile(self.name + X + hash + X + file + ".xml")
                
                return document
        
@@ -54,20 +51,25 @@ class XDB:
                
                prev_umask = os.umask(SPOOL_UMASK)
                hash = file[0:2]
-               pre = self.name + "/" + hash + "/"
+               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):
                """ Returns a list containing the files in the current XDB database """
                files = []
                for dir in os.listdir(self.name):
-                       if(os.path.isdir(self.name + "/" + dir)):
-                               files.extend(os.listdir(self.name + "/" + dir))
+                       if(os.path.isdir(self.name + X + dir)):
+                               files.extend(os.listdir(self.name + X + dir))
                if self.mangle:
                        files = [unmangle(x)[:-4] for x in files]
                else:
@@ -98,7 +100,7 @@ class XDB:
                        except IOError:
                                pass
                        if(not document):
-                               document = Element((None, "xdb"))
+                               document = xmlw.Element((None, "xdb"))
                        
                        # Remove the existing node (if any)
                        for child in document.elements():
@@ -114,7 +116,7 @@ class XDB:
        
        def remove(self, file):
                """ Removes an XDB file """
-               file = self.name + "/" + file[0:2] + "/" + file + ".xml"
+               file = self.name + X + file[0:2] + X + file + ".xml"
                if(self.mangle):
                        file = mangle(file)
                try: