From 3a5daedb83710a7846db3a3f2a209da56d58aaa3 Mon Sep 17 00:00:00 2001 From: helmutm Date: Thu, 12 Apr 2007 15:28:17 +0000 Subject: [PATCH] slight improvements of cybertools.storage git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1685 fd906abe-77d9-0310-91a1-e0d9ade77398 --- storage/filesystem.py | 55 +++++++++++++++++++++++++++++++++++++++++-- storage/pzope/base.py | 2 +- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/storage/filesystem.py b/storage/filesystem.py index 9384775..5664aa5 100644 --- a/storage/filesystem.py +++ b/storage/filesystem.py @@ -23,8 +23,11 @@ $Id$ """ import os -import cybertools from zope.interface import implements +import transaction +from transaction.interfaces import IDataManager + +import cybertools from cybertools.storage.interfaces import IExternalStorage DEFAULT_DIRECTORY = 'extfiles' @@ -34,12 +37,14 @@ class FileSystemStorage(object): implements(IExternalStorage) - def __init__(self, rootDir, subDir): + def __init__(self, rootDir=None, subDir=None): self.rootDir = rootDir self.subDir = subDir def getDir(self, address, subDir=None): subDir = subDir or self.subDir + if self.rootDir is None: + return os.path.join(subDir, address) return os.path.join(self.rootDir, subDir, address) def setData(self, address, data, params={}): @@ -49,6 +54,10 @@ class FileSystemStorage(object): f.write(data) f.close() print 'cybertools.storage: file %s written' % fn + # TODO: transaction management: + # write to temp file in subDir, keep address in internal dictionary + # transaction.manager.get().join(FSSDataManager(address, temp)) + # then rename in tpc_finish() and remove temp file in tpc_abort def getData(self, address, params={}): subDir = params.get('subdirectory') @@ -59,11 +68,53 @@ class FileSystemStorage(object): return data +class FSSDataManager(object): + + implements(IDataManager) + + transaction_manager = None + + def __init__(self, address, temp): + self.address = address + self.temp = temp + self.transaction_manager = transaction.manager + + def abort(self, transaction): + # remove temp file + pass + + def tpc_begin(self, transaction): + pass + + def commit(self, transaction): + # rename original file if present to a temporary name + # rename temp file to original name + pass + + def tpc_vote(self, transaction): + pass + + def tpc_finish(self, transaction): + # remove renamed original file + pass + + def tpc_abort(self, transaction): + # rename back original file (removing renamed temp file) + pass + + def sortKey(self): + return 'cybertools.storage.FileSystemStorage:' + self.address + + def explicitDirectoryStorage(dirname): """ This cannot be used as a utility but must be called explicitly.""" return FileSystemStorage('', dirname) +def fullPathStorage(): + return FileSystemStorage() + + def instanceVarSubdirectoryStorage(dirname=DEFAULT_DIRECTORY): instanceHome = os.path.dirname(os.path.dirname(os.path.dirname( os.path.dirname(cybertools.__file__)))) diff --git a/storage/pzope/base.py b/storage/pzope/base.py index bf12424..182525c 100644 --- a/storage/pzope/base.py +++ b/storage/pzope/base.py @@ -27,7 +27,7 @@ from zope.interface import implements from zope.app.component.hooks import getSite from zope.app.container.interfaces import IContained from zope.app.intid.interfaces import IIntIds -from zope.app.traversing.api import traverse, getPath +from zope.traversing.api import traverse, getPath from persistent import Persistent from cybertools.util.adapter import AdapterFactory