quota.py
2008-01-11 19:18...
from zope.app import zapi
from zope.interface import implements
from interfaces import IQuota,IQuotaContained,IQuotaSize
from zope.app.intid.interfaces import IIntIds
from persistent import Persistent
from zope.app.container.contained import Contained
from BTrees.IOBTree import IOBTree
class Quota(Contained,Persistent):
__doc__ = IQuota.__doc__
def __init__(self) :
self.osz = IOBTree()
implements(IQuota,IQuotaContained)
# See quota.interfaces.IQuota
quota = 0
# See quota.interfaces.IQuota
size = 0
def handleAdded(self, object):
size = IQuotaSize(object).size
self.osz[zapi.getUtility(IIntIds, context=self).getId(object)] = size
self.size+=size
if self.size > self.quota :
raise ValueError
def handleModified(self, object):
size = IQuotaSize(object).size
try :
self.size-=self.osz[zapi.getUtility(IIntIds, context=self).getId(object)]
except KeyError :
pass
self.osz[zapi.getUtility(IIntIds, context=self).getId(object)] = size
self.size+=size
if self.size > self.quota :
raise ValueError
def handleRemoved(self, object):
self.size-=IQuotaSize(object).size
try :
del(self.osz[zapi.getUtility(IIntIds, context=self).getId(object)])
except KeyError :
pass



