index ccf3976ec7be9b5e87c9c1548dd7f95e30b28ad3..0008ba60c468aacf0f7bfe91acb97fb23a72d9fa 100644 (file)
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#
-#$Id: blobfiles.py,v 1.3 2002-02-27 07:33:34 grubert Exp $
+#$Id: blobfiles.py,v 1.9 2002-09-10 00:11:50 richard Exp $
'''
This module exports file storage for roundup backends.
Files are stored into a directory hierarchy.
'''
'''
This module exports file storage for roundup backends.
Files are stored into a directory hierarchy.
'''
-import os, os.path
+import os
def files_in_dir(dir):
if not os.path.exists(dir):
def files_in_dir(dir):
if not os.path.exists(dir):
class FileStorage:
"""Store files in some directory structure"""
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.
def filename(self, classname, nodeid, property=None):
'''Determine what the filename for the given node and optionally
property is.
None, in which case the filename does not indicate which property
is being saved.
'''
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)
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))
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)
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.
def getfile(self, classname, nodeid, property):
'''Get the content of the file in the database.
try:
return open(filename, 'rb').read()
except:
try:
return open(filename, 'rb').read()
except:
+ # now try the temp pre-commit filename
try:
return open(filename+'.tmp', 'rb').read()
except:
try:
return open(filename+'.tmp', 'rb').read()
except:
files_dir = os.path.join(self.dir, 'files')
return files_in_dir(files_dir)
files_dir = os.path.join(self.dir, 'files')
return files_in_dir(files_dir)
- def _doStoreFile(self, name, **databases):
- '''Must be implemented by subclass
+ def doStoreFile(self, classname, nodeid, property, **databases):
+ '''Store the file as part of a transaction commit.
'''
'''
- raise NotImplementedError
+ # 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)
+
+ # 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")
-# $Log: not supported by cvs2svn $
-#
# vim: set filetype=python ts=4 sw=4 et si
# vim: set filetype=python ts=4 sw=4 et si