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:
parent
51a0a212ef
commit
3751952d34
2 changed files with 51 additions and 25 deletions
|
@ -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
|
from the corresponding factory in the `manager` module and calling
|
||||||
`save()` on it.
|
`save()` on it.
|
||||||
|
|
||||||
>>> from cybertools.storage.pzope.manager import storages
|
>>> from cybertools.storage.pzope.base import storages
|
||||||
>>> persistent = storages(c1)
|
>>> 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
|
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
|
call `load()` on it, providing the UID we had got back when saving the
|
||||||
|
@ -46,6 +46,13 @@ object.
|
||||||
>>> c2.title
|
>>> c2.title
|
||||||
'changed'
|
'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
|
Fin de partie
|
||||||
=============
|
=============
|
||||||
|
|
|
@ -27,8 +27,8 @@ 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, traverseName
|
from zope.app.traversing.api import traverse, getPath
|
||||||
from persistent import Persistent as BasePersistent
|
from persistent import Persistent
|
||||||
|
|
||||||
from cybertools.util.adapter import AdapterFactory
|
from cybertools.util.adapter import AdapterFactory
|
||||||
|
|
||||||
|
@ -36,21 +36,43 @@ from cybertools.util.adapter import AdapterFactory
|
||||||
storages = AdapterFactory()
|
storages = AdapterFactory()
|
||||||
|
|
||||||
|
|
||||||
|
class PersistentObject(Persistent):
|
||||||
|
|
||||||
|
def update(self, data):
|
||||||
|
self.__dict__.update(data)
|
||||||
|
|
||||||
|
def get(self):
|
||||||
|
return self.__dict__
|
||||||
|
|
||||||
|
|
||||||
class Adapter(object):
|
class Adapter(object):
|
||||||
|
|
||||||
|
persistentFactory = PersistentObject
|
||||||
|
|
||||||
def __init__(self, context):
|
def __init__(self, context):
|
||||||
self.context = context
|
self.context = context
|
||||||
self.persistent = None
|
self.persistent = None
|
||||||
|
self.address = None
|
||||||
|
|
||||||
def save(self, name, path='/'):
|
def save(self, address=None):
|
||||||
intids = component.getUtility(IIntIds)
|
intids = component.getUtility(IIntIds)
|
||||||
persistent = self.persistent
|
persistent = self.persistent
|
||||||
if persistent is None:
|
if persistent is None:
|
||||||
persistent = Persistent()
|
if self.address is None:
|
||||||
site = getSite()
|
self.address = address
|
||||||
container = traverse(site, path)
|
else:
|
||||||
container[name] = persistent
|
address = self.address
|
||||||
uid = intids.register(persistent)
|
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:
|
else:
|
||||||
uid = intids.getId(persistent)
|
uid = intids.getId(persistent)
|
||||||
persistent.update(self.context.__dict__)
|
persistent.update(self.context.__dict__)
|
||||||
|
@ -58,26 +80,23 @@ class Adapter(object):
|
||||||
self.persistent = persistent
|
self.persistent = persistent
|
||||||
return uid
|
return uid
|
||||||
|
|
||||||
def load(self, idOrPath):
|
def load(self, address=None):
|
||||||
if type(idOrPath) is int:
|
if type(address) is int: # seems to be an intId
|
||||||
intids = component.getUtility(IIntIds)
|
intids = component.getUtility(IIntIds)
|
||||||
persistent = intids.getObject(idOrPath)
|
persistent = intids.getObject(address)
|
||||||
|
self.address = getPath(persistent)
|
||||||
else:
|
else:
|
||||||
site = getSite()
|
if self.address is None:
|
||||||
persistent = traverse(site, path)
|
self.address = address
|
||||||
|
else:
|
||||||
|
address = self.address
|
||||||
|
persistent = traverse(getSite(), address)
|
||||||
t = type(self.context)
|
t = type(self.context)
|
||||||
class_ = t is type and self.context or t
|
factory = t is type and self.context or t
|
||||||
obj = self.context = class_()
|
obj = self.context = factory()
|
||||||
obj.__dict__.update(persistent.get())
|
obj.__dict__.update(persistent.get())
|
||||||
|
self.persistent = persistent
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
storages.register(Adapter, object)
|
storages.register(Adapter, object)
|
||||||
|
|
||||||
|
|
||||||
class Persistent(BasePersistent):
|
|
||||||
|
|
||||||
def update(self, data):
|
|
||||||
self.__dict__.update(data)
|
|
||||||
|
|
||||||
def get(self):
|
|
||||||
return self.__dict__
|
|
Loading…
Add table
Reference in a new issue