[CalendarServer-changes] [7622] CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore

source_changes at macosforge.org source_changes at macosforge.org
Mon Jun 20 11:45:12 PDT 2011


Revision: 7622
          http://trac.macosforge.org/projects/calendarserver/changeset/7622
Author:   glyph at apple.com
Date:     2011-06-20 11:45:12 -0700 (Mon, 20 Jun 2011)
Log Message:
-----------
Factor common AttachmentStorageTransport logic into base class.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/file.py
    CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/sql.py
    CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/util.py

Modified: CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/file.py
===================================================================
--- CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/file.py	2011-06-20 18:45:01 UTC (rev 7621)
+++ CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/file.py	2011-06-20 18:45:12 UTC (rev 7622)
@@ -56,10 +56,9 @@
     IndexSchedule as OldInboxIndex
 from txdav.caldav.datastore.util import (
     validateCalendarComponent, dropboxIDFromCalendarObject, CalendarObjectBase,
-    CalendarHomeBase
+    CalendarHomeBase, StorageTransportBase
 )
 
-from txdav.caldav.icalendarstore import IAttachmentStorageTransport
 from txdav.common.datastore.file import (
     CommonDataStore, CommonStoreTransaction, CommonHome, CommonHomeChild,
     CommonObjectResource, CommonStubResource)
@@ -580,10 +579,8 @@
 
 
 
-class AttachmentStorageTransport(object):
+class AttachmentStorageTransport(StorageTransportBase):
 
-    implements(IAttachmentStorageTransport)
-
     def __init__(self, attachment, contentType):
         """
         Initialize this L{AttachmentStorageTransport} and open its file for
@@ -592,8 +589,8 @@
         @param attachment: The attachment whose data is being filled out.
         @type attachment: L{Attachment}
         """
-        self._attachment = attachment
-        self._contentType = contentType
+        super(AttachmentStorageTransport, self).__init__(
+            attachment, contentType)
         self._path = self._attachment._path.temporarySibling()
         self._file = self._path.open("w")
 
@@ -630,21 +627,7 @@
         return succeed(None)
 
 
-    def getPeer(self):
-        raise NotImplementedError()
-        return 'Storing attachment <%r>' % (self.attachment._path,)
 
-
-    def getHost(self):
-        raise NotImplementedError()
-        return 'Storing attachment (host) <%r>' % (self.attachment._path,)
-
-
-    def writeSequence(self, seq):
-        raise NotImplementedError()
-        return self.write(''.join(seq))
-
-
 class Attachment(FileMetaDataMixin):
     """
     An L{Attachment} is a container for the data associated with a I{locally-

Modified: CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/sql.py
===================================================================
--- CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/sql.py	2011-06-20 18:45:01 UTC (rev 7621)
+++ CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/sql.py	2011-06-20 18:45:12 UTC (rev 7622)
@@ -68,9 +68,9 @@
 from twext.enterprise.dal.syntax import Len
 
 from txdav.caldav.datastore.util import CalendarObjectBase, CalendarHomeBase
-from txdav.caldav.icalendarstore import IAttachmentStorageTransport
 from txdav.caldav.icalendarstore import QuotaExceeded
 
+from txdav.caldav.datastore.util import StorageTransportBase
 from txdav.common.icommondatastore import IndexedSearchException
 
 from pycalendar.datetime import PyCalendarDateTime
@@ -737,26 +737,24 @@
 
 
 
-class AttachmentStorageTransport(object):
+class AttachmentStorageTransport(StorageTransportBase):
 
-    implements(IAttachmentStorageTransport)
-
     def __init__(self, attachment, contentType, creating=False):
-        self.attachment = attachment
-        self.contentType = contentType
-        self.buf = ''
-        self.hash = hashlib.md5()
-        self.creating = creating
+        super(AttachmentStorageTransport, self).__init__(
+            attachment, contentType)
+        self._buf = ''
+        self._hash = hashlib.md5()
+        self._creating = creating
 
 
     @property
     def _txn(self):
-        return self.attachment._txn
+        return self._attachment._txn
 
 
     def write(self, data):
-        self.buf += data
-        self.hash.update(data)
+        self._buf += data
+        self._hash.update(data)
 
 
     @inlineCallbacks
@@ -770,60 +768,44 @@
         # prevented from committing successfully.  It's not valid to have an
         # attachment that doesn't point to a real file.
 
-        home = (
-            yield self._txn.calendarHomeWithResourceID(
-                self.attachment._ownerHomeID))
+        home = (yield self._txn.calendarHomeWithResourceID(
+                    self._attachment._ownerHomeID))
 
-        oldSize = self.attachment.size()
+        oldSize = self._attachment.size()
 
         if home.quotaAllowedBytes() < ((yield home.quotaUsedBytes())
-                                       + (len(self.buf) - oldSize)):
-            if self.creating:
-                yield self.attachment._internalRemove()
+                                       + (len(self._buf) - oldSize)):
+            if self._creating:
+                yield self._attachment._internalRemove()
             raise QuotaExceeded()
 
-        self.attachment._path.setContent(self.buf)
-        self.attachment._contentType = self.contentType
-        self.attachment._md5 = self.hash.hexdigest()
-        self.attachment._size = len(self.buf)
+        self._attachment._path.setContent(self._buf)
+        self._attachment._contentType = self._contentType
+        self._attachment._md5 = self._hash.hexdigest()
+        self._attachment._size = len(self._buf)
         att = schema.ATTACHMENT
-        self.attachment._created, self.attachment._modified = map(
+        self._attachment._created, self._attachment._modified = map(
             sqltime,
             (yield Update(
                 {
-                    att.CONTENT_TYPE : generateContentType(self.contentType),
-                    att.SIZE         : self.attachment._size,
-                    att.MD5          : self.attachment._md5,
+                    att.CONTENT_TYPE : generateContentType(self._contentType),
+                    att.SIZE         : self._attachment._size,
+                    att.MD5          : self._attachment._md5,
                     att.MODIFIED     : utcNowSQL
                 },
-                Where=att.PATH == self.attachment.name(),
+                Where=att.PATH == self._attachment.name(),
                 Return=(att.CREATED, att.MODIFIED)).on(self._txn))[0]
         )
 
         if home:
             # Adjust quota
-            yield home.adjustQuotaUsedBytes(self.attachment.size() - oldSize)
+            yield home.adjustQuotaUsedBytes(self._attachment.size() - oldSize)
 
             # Send change notification to home
             yield home.notifyChanged()
 
 
-    def getPeer(self):
-        raise NotImplementedError()
-        return 'Storing attachment <%r>' % (self.attachment._path,)
 
-
-    def getHost(self):
-        raise NotImplementedError()
-        return 'Storing attachment (host) <%r>' % (self.attachment._path,)
-
-
-    def writeSequence(self, seq):
-        raise NotImplementedError()
-        return self.write(''.join(seq))
-
-
-
 def sqltime(value):
     return datetimeMktime(parseSQLTimestamp(value))
 

Modified: CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/util.py
===================================================================
--- CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/util.py	2011-06-20 18:45:01 UTC (rev 7621)
+++ CalendarServer/branches/users/glyph/quota/txdav/caldav/datastore/util.py	2011-06-20 18:45:12 UTC (rev 7622)
@@ -30,6 +30,8 @@
 
 from twistedcaldav.datafilters.privateevents import PrivateEventFilter
 from twistedcaldav.datafilters.peruserdata import PerUserDataFilter
+from zope.interface.declarations import implements
+from txdav.caldav.icalendarstore import IAttachmentStorageTransport
 from twext.python.log import Logger
 log = Logger()
 
@@ -274,3 +276,36 @@
         returnValue(component)
 
 
+
+class StorageTransportBase(object):
+    """
+    Base logic shared between file- and sql-based L{IAttachmentStorageTransport}
+    implementations.
+    """
+
+    implements(IAttachmentStorageTransport)
+
+    def __init__(self, attachment, contentType):
+        """
+        Create a storage transport with a reference to an L{IAttachment} and a
+        L{twext.web2.http_headers.MimeType}.
+        """
+        self._attachment = attachment
+        self._contentType = contentType
+
+
+    def getPeer(self):
+        raise NotImplementedError()
+        return 'Storing attachment <%r>' % (self.attachment._path,)
+
+
+    def getHost(self):
+        raise NotImplementedError()
+        return 'Storing attachment (host) <%r>' % (self.attachment._path,)
+
+
+    def writeSequence(self, seq):
+        raise NotImplementedError()
+        return self.write(''.join(seq))
+
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110620/34e82e8c/attachment-0001.html>


More information about the calendarserver-changes mailing list