some improvements on the zope-/persistent-based storage adapter

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1543 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2007-01-05 12:49:45 +00:00
parent 51a0a212ef
commit 3751952d34
2 changed files with 51 additions and 25 deletions

View file

@ -33,9 +33,9 @@ We can save the object in the storage by getting a storage adapter
from the corresponding factory in the `manager` module and calling
`save()` on it.
>>> from cybertools.storage.pzope.manager import storages
>>> from cybertools.storage.pzope.base import storages
>>> persistent = storages(c1)
>>> c1Uid = persistent.save('c1')
>>> c1Uid = persistent.save('/c1')
For loading an object we get a storage adapter to the object's class and
call `load()` on it, providing the UID we had got back when saving the
@ -46,6 +46,13 @@ object.
>>> c2.title
'changed'
If an object has been stored to the storage or loaded from it it has got
an `address` attribut that will be used for subsequent accesses to the
storage.
>>> persistent.address
u'/c1'
Fin de partie
=============

View file

@ -27,8 +27,8 @@ 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, traverseName
from persistent import Persistent as BasePersistent
from zope.app.traversing.api import traverse, getPath
from persistent import Persistent
from cybertools.util.adapter import AdapterFactory
@ -36,21 +36,43 @@ from cybertools.util.adapter import AdapterFactory
storages = AdapterFactory()
class PersistentObject(Persistent):
def update(self, data):
self.__dict__.update(data)
def get(self):
return self.__dict__
class Adapter(object):
persistentFactory = PersistentObject
def __init__(self, context):
self.context = context
self.persistent = None
self.address = None
def save(self, name, path='/'):
def save(self, address=None):
intids = component.getUtility(IIntIds)
persistent = self.persistent
if persistent is None:
persistent = Persistent()
site = getSite()
container = traverse(site, path)
container[name] = persistent
uid = intids.register(persistent)
if self.address is None:
self.address = address
else:
address = self.address
path, name = address.rsplit('/', 1)
container = traverse(getSite(), path + '/')
if name in container:
persistent = container[name]
uid = intids.getId(persistent)
else:
persistent = self.persistentFactory()
container[name] = persistent
persistent.__name__ = name
persistent.__parent__ = container
uid = intids.register(persistent)
else:
uid = intids.getId(persistent)
persistent.update(self.context.__dict__)
@ -58,26 +80,23 @@ class Adapter(object):
self.persistent = persistent
return uid
def load(self, idOrPath):
if type(idOrPath) is int:
def load(self, address=None):
if type(address) is int: # seems to be an intId
intids = component.getUtility(IIntIds)
persistent = intids.getObject(idOrPath)
persistent = intids.getObject(address)
self.address = getPath(persistent)
else:
site = getSite()
persistent = traverse(site, path)
if self.address is None:
self.address = address
else:
address = self.address
persistent = traverse(getSite(), address)
t = type(self.context)
class_ = t is type and self.context or t
obj = self.context = class_()
factory = t is type and self.context or t
obj = self.context = factory()
obj.__dict__.update(persistent.get())
self.persistent = persistent
return obj
storages.register(Adapter, object)
class Persistent(BasePersistent):
def update(self, data):
self.__dict__.update(data)
def get(self):
return self.__dict__