slight improvements of cybertools.storage
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1685 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
3107878c7f
commit
3a5daedb83
2 changed files with 54 additions and 3 deletions
|
@ -23,8 +23,11 @@ $Id$
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import cybertools
|
|
||||||
from zope.interface import implements
|
from zope.interface import implements
|
||||||
|
import transaction
|
||||||
|
from transaction.interfaces import IDataManager
|
||||||
|
|
||||||
|
import cybertools
|
||||||
from cybertools.storage.interfaces import IExternalStorage
|
from cybertools.storage.interfaces import IExternalStorage
|
||||||
|
|
||||||
DEFAULT_DIRECTORY = 'extfiles'
|
DEFAULT_DIRECTORY = 'extfiles'
|
||||||
|
@ -34,12 +37,14 @@ class FileSystemStorage(object):
|
||||||
|
|
||||||
implements(IExternalStorage)
|
implements(IExternalStorage)
|
||||||
|
|
||||||
def __init__(self, rootDir, subDir):
|
def __init__(self, rootDir=None, subDir=None):
|
||||||
self.rootDir = rootDir
|
self.rootDir = rootDir
|
||||||
self.subDir = subDir
|
self.subDir = subDir
|
||||||
|
|
||||||
def getDir(self, address, subDir=None):
|
def getDir(self, address, subDir=None):
|
||||||
subDir = subDir or self.subDir
|
subDir = subDir or self.subDir
|
||||||
|
if self.rootDir is None:
|
||||||
|
return os.path.join(subDir, address)
|
||||||
return os.path.join(self.rootDir, subDir, address)
|
return os.path.join(self.rootDir, subDir, address)
|
||||||
|
|
||||||
def setData(self, address, data, params={}):
|
def setData(self, address, data, params={}):
|
||||||
|
@ -49,6 +54,10 @@ class FileSystemStorage(object):
|
||||||
f.write(data)
|
f.write(data)
|
||||||
f.close()
|
f.close()
|
||||||
print 'cybertools.storage: file %s written' % fn
|
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={}):
|
def getData(self, address, params={}):
|
||||||
subDir = params.get('subdirectory')
|
subDir = params.get('subdirectory')
|
||||||
|
@ -59,11 +68,53 @@ class FileSystemStorage(object):
|
||||||
return data
|
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):
|
def explicitDirectoryStorage(dirname):
|
||||||
""" This cannot be used as a utility but must be called explicitly."""
|
""" This cannot be used as a utility but must be called explicitly."""
|
||||||
return FileSystemStorage('', dirname)
|
return FileSystemStorage('', dirname)
|
||||||
|
|
||||||
|
|
||||||
|
def fullPathStorage():
|
||||||
|
return FileSystemStorage()
|
||||||
|
|
||||||
|
|
||||||
def instanceVarSubdirectoryStorage(dirname=DEFAULT_DIRECTORY):
|
def instanceVarSubdirectoryStorage(dirname=DEFAULT_DIRECTORY):
|
||||||
instanceHome = os.path.dirname(os.path.dirname(os.path.dirname(
|
instanceHome = os.path.dirname(os.path.dirname(os.path.dirname(
|
||||||
os.path.dirname(cybertools.__file__))))
|
os.path.dirname(cybertools.__file__))))
|
||||||
|
|
|
@ -27,7 +27,7 @@ from zope.interface import implements
|
||||||
from zope.app.component.hooks import getSite
|
from zope.app.component.hooks import getSite
|
||||||
from zope.app.container.interfaces import IContained
|
from zope.app.container.interfaces import IContained
|
||||||
from zope.app.intid.interfaces import IIntIds
|
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 persistent import Persistent
|
||||||
|
|
||||||
from cybertools.util.adapter import AdapterFactory
|
from cybertools.util.adapter import AdapterFactory
|
||||||
|
|
Loading…
Add table
Reference in a new issue