class PersistentDictConfigStorage(DictConfigStorage): def __init__(self, *a, **kw): self._dict = PersistentDict() ConfigStorage.__init__(self, *a, **kw) def getDict(self): site = getSite() if site is not None: ann = IAnnotations(site) if ASSETS_SETTING_KEY not in ann: ann[ASSETS_SETTING_KEY] = self._dict return ann[ASSETS_SETTING_KEY] return self._dict def __getitem__(self, key): key = key.lower() value = self._get_deprecated(key) if not value is None: return value self._dict = self.getDict() return self._dict.__getitem__(key) def __setitem__(self, key, value): key = key.lower() if not self._set_deprecated(key, value): self._dict = self.getDict() self._dict.__setitem__(key, value) self._dict._p_changed = True
def __setitem__(self, name, item): if item.__name__ != name: raise ValueError('name and item.__name__ must be equal') PersistentDict.__setitem__(self, name, item) item.__parent__ = self
def __setitem__(self, name, item): if item.__name__ != name: raise ValueError('name and item.__name__ must be equal') PersistentDict.__setitem__(self, name, item) item.__parent__ = self
def __add(self, item): PersistentDict.__setitem__(self, item.__name__, item) item.__parent__ = self
def __add(self, item): PersistentDict.__setitem__(self, item.__name__, item) item.__parent__ = self
class VideoContainer(PersistentMapping): """ A simple container for Video objects >>> from mint.repoze.interfaces import IVideoContainer >>> from mint.repoze.models import VideoContainer, Video >>> ob = VideoContainer() >>> IVideoContainer.providedBy(ob) True >>> len(ob) 0 >>> ob[u'vid1'] = Video('vid1', 'Video 1', 'description', []) >>> len(ob) 1 >>> ob.keys() [u'vid1'] """ __acl__ = [ (Allow, Everyone, 'view'), (Allow, 'admin', 'add'), (Allow, 'admin', 'edit'), ] implements(IVideoContainer,ILocation) encode_dir = 'var/videos/' def __init__(self, *args, **kwargs): self.data = PersistentDict() for data in args: self.add_video(*data) for v in kwargs.values(): pass def __getitem__(self, key): return self.data.__getitem__(key) def __setitem__(self, key, value): return self.data.__setitem__(key, value) def items(self): return self.data.items() def keys(self): return self.data.keys() def values(self): return self.data.values() def __repr__(self): return u'<VideoContainer object>' def add_video(self, name, description, tags, encodes={}): uid = name.lower().replace(' ', '_') counter = 1 while uid in self: uid = '%s_%03d' % (uid, counter) counter += 1 self.data[uid] = Video(uid, name, description, tags, encodes, self.encode_dir) import transaction transaction.commit() def get_videos_by_tag(self, tag): """ Returns a list of video objects with the given tag >>> from mint.repoze.test.data import video_container >>> video_container.get_videos_by_tag('feature') # doctest: +ELLIPSIS [<Video name=...] """ return [video for video in self.data.values() if tag in video.tags]
class BaseContainer(PersistentMapping): """ Provides a basis for `container` objects >>> container = BaseContainer() >>> container[u'foo'] = u'bar' >>> container[u'foo'] u'bar' >>> container.items() [(u'foo', u'bar')] >>> container.keys() [u'foo'] >>> container.values() [u'bar'] """ def __init__(self): self.data = PersistentDict() def __getitem__(self, key): return self.data.__getitem__(key) def __setitem__(self, key, value): """ Acts as a proxy to the self.data PersistentDict. As it is a persistent object, it will also try and assign the __parent__ attrubute to any object stored through this interface. >>> container = BaseContainer() >>> container.__setitem__('foo', 'bar') >>> 'foo' in container.data True >>> container['foo'].__parent__ # doctest: +ELLIPSIS Traceback (most recent call last): ... AttributeError: 'str' object has no attribute '__parent__' >>> class Child(object): ... __parent__ = None ... >>> container.__setitem__('baz', Child()) >>> 'baz' in container.data True >>> container['baz'].__parent__ == container True """ ret = self.data.__setitem__(key, value) try: self.data[key].__parent__ = self except: pass return ret def items(self): return self.data.items() def keys(self): return self.data.keys() def values(self): return self.data.values() def update(self, _data={}, **kwargs): """ BaseContainers can be updated much the same as any Python dictionary. By passing another mapping object: >>> container = BaseContainer() >>> container.update({'foo':'bar'}) By passing a list of iterables with length 2: >>> container = BaseContainer() >>> container.update([('foo', 'bar'),]) By passing a set of keyword arguments: >>> container = BaseContainer() >>> container.update(foo='bar') """ if kwargs: for k,v in kwargs.items(): self.__setitem__(k,v) return elif isinstance(_data, dict): for k,v in _data.items(): self.__setitem__(k,v) elif isinstance(_data, dict): for k,v in _data: self.__setitem__(k,v)