Back to index

plone3  3.1.7
__init__.py
Go to the documentation of this file.
00001 from Products.Archetypes.interfaces.storage import IStorage
00002 from Products.Archetypes.interfaces.layer import ILayer
00003 from Products.Archetypes.debug import log
00004 from Products.Archetypes.utils import shasattr
00005 
00006 from Acquisition import aq_base
00007 from Globals import PersistentMapping
00008 
00009 from AccessControl import ClassSecurityInfo
00010 from Products.Archetypes.Registry import setSecurity, registerStorage
00011 
00012 type_map = {'text':'string',
00013             'datetime':'date',
00014             'lines':'lines',
00015             'integer':'int'
00016             }
00017 
00018 _marker = []
00019 
00020 #XXX subclass from Base?
00021 class Storage:
00022     """Basic, abstract class for Storages. You need to implement
00023     at least those methods"""
00024 
00025     __implements__ = IStorage
00026 
00027     security = ClassSecurityInfo()
00028 
00029     security.declarePublic('getName')
00030     def getName(self):
00031         return self.__class__.__name__
00032 
00033     def __repr__(self):
00034         return "<Storage %s>" % (self.getName())
00035 
00036     def __cmp__(self, other):
00037         return cmp(self.getName(), other.getName())
00038 
00039     security.declarePrivate('get')
00040     def get(self, name, instance, **kwargs):
00041         raise NotImplementedError('%s: get' % self.getName())
00042 
00043     security.declarePrivate('set')
00044     def set(self, name, instance, value, **kwargs):
00045         raise NotImplementedError('%s: set' % self.getName())
00046 
00047     security.declarePrivate('unset')
00048     def unset(self, name, instance, **kwargs):
00049         raise NotImplementedError('%s: unset' % self.getName())
00050 
00051 setSecurity(Storage)
00052 
00053 class ReadOnlyStorage(Storage):
00054     """A marker storage class for used for read-only fields."""
00055     __implements__ = IStorage
00056 
00057     security = ClassSecurityInfo()
00058 
00059 class StorageLayer(Storage):
00060     """Base, abstract StorageLayer. Storages that need to manipulate
00061     how they are initialized per instance and/or per field must
00062     subclass and implement those methods"""
00063 
00064     __implements__ = IStorage, ILayer
00065 
00066     security = ClassSecurityInfo()
00067 
00068     security.declarePrivate('initializeInstance')
00069     def initializeInstance(self, instance, item=None, container=None):
00070         raise NotImplementedError('%s: initializeInstance' % self.getName())
00071 
00072     security.declarePrivate('cleanupInstance')
00073     def cleanupInstance(self, instance, item=None, container=None):
00074         raise NotImplementedError('%s: cleanupInstance' % self.getName())
00075 
00076     security.declarePrivate('initializeField')
00077     def initializeField(self, instance, field):
00078         raise NotImplementedError('%s: initializeField' % self.getName())
00079 
00080     security.declarePrivate('cleanupField')
00081     def cleanupField(self, instance, field):
00082         raise NotImplementedError('%s: cleanupField' % self.getName())
00083 
00084 setSecurity(StorageLayer)
00085 
00086 class AttributeStorage(Storage):
00087     """Stores data as an attribute of the instance. This is the most
00088     commonly used storage"""
00089 
00090     __implements__ = IStorage
00091 
00092     security = ClassSecurityInfo()
00093 
00094     security.declarePrivate('get')
00095     def get(self, name, instance, **kwargs):
00096         if not shasattr(instance, name):
00097             raise AttributeError(name)
00098         return getattr(instance, name)
00099 
00100     security.declarePrivate('set')
00101     def set(self, name, instance, value, **kwargs):
00102         # Remove acquisition wrappers
00103         value = aq_base(value)
00104         setattr(aq_base(instance), name, value)
00105         instance._p_changed = 1
00106 
00107     security.declarePrivate('unset')
00108     def unset(self, name, instance, **kwargs):
00109         try:
00110             delattr(aq_base(instance), name)
00111         except AttributeError:
00112             pass
00113         instance._p_changed = 1
00114 
00115 class ObjectManagedStorage(Storage):
00116     """Stores data using the Objectmanager interface. It's usually
00117     used for BaseFolder-based content"""
00118 
00119     __implements__ = IStorage
00120 
00121     security = ClassSecurityInfo()
00122 
00123     security.declarePrivate('get')
00124     def get(self, name, instance, **kwargs):
00125         try:
00126             return instance._getOb(name)
00127         except Exception, msg:
00128             raise AttributeError(msg)
00129 
00130     security.declarePrivate('set')
00131     def set(self, name, instance, value, **kwargs):
00132         # Remove acquisition wrappers
00133         value = aq_base(value)
00134         try:
00135             instance._delObject(name)
00136         except (AttributeError, KeyError):
00137             pass
00138         instance._setObject(name, value)
00139         instance._p_changed = 1
00140 
00141     security.declarePrivate('unset')
00142     def unset(self, name, instance, **kwargs):
00143         instance._delObject(name)
00144         instance._p_changed = 1
00145 
00146 class MetadataStorage(StorageLayer):
00147     """Storage used for ExtensibleMetadata. Attributes are stored on
00148     a persistent mapping named ``_md`` on the instance."""
00149 
00150     __implements__ = IStorage, ILayer
00151 
00152     security = ClassSecurityInfo()
00153 
00154     security.declarePrivate('initializeInstance')
00155     def initializeInstance(self, instance, item=None, container=None):
00156         if not shasattr(instance, "_md"):
00157             instance._md = PersistentMapping()
00158             instance._p_changed = 1
00159 
00160     security.declarePrivate('initializeField')
00161     def initializeField(self, instance, field):
00162         # Check for already existing field to avoid  the reinitialization
00163         # (which means overwriting) of an already existing field after a
00164         # copy or rename operation
00165         base = aq_base (instance)
00166         if not base._md.has_key(field.getName()):
00167             self.set(field.getName(), instance, field.getDefault(instance))
00168 
00169     security.declarePrivate('get')
00170     def get(self, name, instance, **kwargs):
00171         base = aq_base(instance)
00172         try:
00173             value = base._md[name]
00174         except KeyError, msg:
00175             # We are acting like an attribute, so
00176             # raise AttributeError instead of KeyError
00177             raise AttributeError(name, msg)
00178         return value
00179 
00180     security.declarePrivate('set')
00181     def set(self, name, instance, value, **kwargs):
00182         base = aq_base(instance)
00183         # Remove acquisition wrappers
00184         if not hasattr(base,'_md'):
00185                    base._md=PersistentMapping()
00186 
00187         base._md[name] = aq_base(value)
00188         base._p_changed = 1
00189 
00190     security.declarePrivate('unset')
00191     def unset(self, name, instance, **kwargs):
00192         if not shasattr(instance, "_md"):
00193             log("Broken instance %s, no _md" % instance)
00194         else:
00195             del instance._md[name]
00196             instance._p_changed = 1
00197 
00198     security.declarePrivate('cleanupField')
00199     def cleanupField(self, instance, field, **kwargs):
00200         # Don't clean up the field self to avoid problems with copy/rename. The
00201         # python garbarage system will clean up if needed.
00202         pass
00203 
00204     security.declarePrivate('cleanupInstance')
00205     def cleanupInstance(self, instance, item=None, container=None):
00206         # Don't clean up the instance self to avoid problems with copy/rename. The
00207         # python garbarage system will clean up if needed.
00208         pass
00209 
00210 __all__ = ('ReadOnlyStorage', 'ObjectManagedStorage',
00211            'MetadataStorage', 'AttributeStorage',)
00212 
00213 for name in __all__:
00214     storage = locals()[name]
00215     registerStorage(storage)