Code

documentation cleanup
[roundup.git] / roundup / backends / blobfiles.py
index 86ff228482e63f767a8c3b5c3a73f38f5e66e46f..673bd938557ebe1579e2eed45c7f999ad4cea7e1 100644 (file)
 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 # 
-#$Id: blobfiles.py,v 1.5 2002-07-08 06:58:15 richard Exp $
-'''
-This module exports file storage for roundup backends.
+#$Id: blobfiles.py,v 1.11 2004-02-11 23:55:09 richard Exp $
+'''This module exports file storage for roundup backends.
 Files are stored into a directory hierarchy.
 '''
+__docformat__ = 'restructuredtext'
 
 import os
 
@@ -37,10 +37,6 @@ def files_in_dir(dir):
 
 class FileStorage:
     """Store files in some directory structure"""
-# TODO: maybe set "files"
-#    def __init__(self):
-#        pass
-
     def filename(self, classname, nodeid, property=None):
         '''Determine what the filename for the given node and optionally 
            property is.
@@ -74,26 +70,38 @@ class FileStorage:
            None, in which case the filename does not indicate which property
            is being saved.
         '''
+        # determine the name of the file to write to
         name = self.filename(classname, nodeid, property)
+
+        # make sure the file storage dir exists
         if not os.path.exists(os.path.dirname(name)):
             os.makedirs(os.path.dirname(name))
+
+        # open the temp file for writing
         open(name + '.tmp', 'wb').write(content)
-        self.transactions.append((self._doStoreFile, (name, )))
 
+        # save off the commit action
+        self.transactions.append((self.doStoreFile, (classname, nodeid,
+            property)))
 
     def getfile(self, classname, nodeid, property):
         '''Get the content of the file in the database.
         '''
+        # try a variety of different filenames - the file could be in the
+        # usual place, or it could be in a temp file pre-commit *or* it
+        # could be in an old-style, backwards-compatible flat directory
         filename = self.filename(classname, nodeid, property)
-        try:
-            return open(filename, 'rb').read()
-        except:
-            try:
-                return open(filename+'.tmp', 'rb').read()
-            except:
-                # fallback to flat file storage
-                filename = self.filename_flat(classname, nodeid, property)
-                return open(filename, 'rb').read()
+        flat_filename = self.filename_flat(classname, nodeid, property)
+        for filename in (filename, filename+'.tmp', flat_filename):
+            if os.path.exists(filename):
+                f = open(filename, 'rb')
+                break
+        else:
+            raise IOError, 'content file not found'
+        # snarf the contents and make sure we close the file
+        content = f.read()
+        f.close()
+        return content
 
     def numfiles(self):
         '''Get number of files in storage, even across subdirectories.
@@ -101,20 +109,24 @@ class FileStorage:
         files_dir = os.path.join(self.dir, 'files')
         return files_in_dir(files_dir)
 
-    def _doStoreFile(self, name, **databases):
+    def doStoreFile(self, classname, nodeid, property, **databases):
         '''Store the file as part of a transaction commit.
         '''
+        # determine the name of the file to write to
+        name = self.filename(classname, nodeid, property)
+
         # the file is currently ".tmp" - move it to its real name to commit
         os.rename(name+".tmp", name)
-        self.indexer.add_file(name)
-        self.indexer.save_index()
 
-# $Log: not supported by cvs2svn $
-# Revision 1.4  2002/06/19 03:07:19  richard
-# Moved the file storage commit into blobfiles where it belongs.
-#
-# Revision 1.3  2002/02/27 07:33:34  grubert
-#  . add, vim line and cvs log key.
-#
-#
+        # return the classname, nodeid so we reindex this content
+        return (classname, nodeid)
+
+    def rollbackStoreFile(self, classname, nodeid, property, **databases):
+        '''Remove the temp file as a part of a rollback
+        '''
+        # determine the name of the file to delete
+        name = self.filename(classname, nodeid, property)
+        if os.path.exists(name+".tmp"):
+            os.remove(name+".tmp")
+
 # vim: set filetype=python ts=4 sw=4 et si