[CalendarServer-changes] [9996] CalDAVClientLibrary/trunk/caldavclientlibrary

source_changes at macosforge.org source_changes at macosforge.org
Wed Oct 31 09:06:38 PDT 2012


Revision: 9996
          http://trac.calendarserver.org//changeset/9996
Author:   cdaboo at apple.com
Date:     2012-10-31 09:06:38 -0700 (Wed, 31 Oct 2012)
Log Message:
-----------
Add managed attachments command.

Modified Paths:
--------------
    CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/__init__.py
    CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py
    CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/http/requestresponse.py
    CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/url.py

Added Paths:
-----------
    CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/attach.py

Modified: CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/__init__.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/__init__.py	2012-10-31 16:05:59 UTC (rev 9995)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/__init__.py	2012-10-31 16:06:38 UTC (rev 9996)
@@ -17,6 +17,7 @@
 __all__ = [
     "acl",
     "addressbooks",
+    "attach",
     "cat",
     "cd",
     "calendars",

Added: CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/attach.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/attach.py	                        (rev 0)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/attach.py	2012-10-31 16:06:38 UTC (rev 9996)
@@ -0,0 +1,139 @@
+# #
+# Copyright (c) 2007-2010 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# #
+
+from caldavclientlibrary.browser.command import Command
+from caldavclientlibrary.browser.command import WrongOptions
+from caldavclientlibrary.protocol.url import URL
+import os
+import getopt
+import shlex
+
+class Cmd(Command):
+
+    def __init__(self):
+        super(Command, self).__init__()
+        self.cmds = ("attach",)
+
+
+    def execute(self, cmdname, options):
+
+        fname = None
+        content_type = "text/plain"
+        adding = False
+        updating = None
+        removing = None
+        return_representation = False
+        path = None
+
+        opts, args = getopt.getopt(shlex.split(options), 'f:t:au:r:x')
+
+        for name, value in opts:
+
+            if name == "-f":
+                fname = value
+            elif name == "-t":
+                content_type = value
+            elif name == "-a":
+                if updating is not None or removing is not None:
+                    print "Only one of -a, -u or -r can be specified"
+                    print self.usage(cmdname)
+                    raise WrongOptions
+                adding = True
+            elif name == "-u":
+                if adding or removing is not None:
+                    print "Only one of -a, -u or -r can be specified"
+                    print self.usage(cmdname)
+                    raise WrongOptions
+                updating = value
+            elif name == "-r":
+                if adding or updating is not None:
+                    print "Only one of -a, -u or -r can be specified"
+                    print self.usage(cmdname)
+                    raise WrongOptions
+                removing = value
+            elif name == "-x":
+                return_representation = True
+            else:
+                print "Unknown option: %s" % (name,)
+                print self.usage(cmdname)
+                raise WrongOptions
+
+        if (adding or updating) and not fname:
+            print "File name must be provided"
+            print self.usage(cmdname)
+            raise WrongOptions
+        elif not (adding or updating) and fname:
+            print "File name must not be provided"
+            print self.usage(cmdname)
+            raise WrongOptions
+
+        elif len(args) > 1:
+            print "Wrong number of arguments: %d" % (len(args),)
+            print self.usage(cmdname)
+            raise WrongOptions
+        elif args:
+            path = args[0]
+            if not path.startswith("/"):
+                path = os.path.join(self.shell.wd, path)
+            if path.endswith("/"):
+                print "Cannot attach to a directory: %s" % (path,)
+                print self.usage(cmdname)
+                raise WrongOptions
+        else:
+            print "Path to attach to must be provided"
+            print self.usage(cmdname)
+            raise WrongOptions
+
+        # Read in data
+        if fname:
+            fname = os.path.expanduser(fname)
+            try:
+                data = open(fname, "r").read()
+            except IOError:
+                print "Unable to read data from file: %s" % (fname,)
+                print self.usage(cmdname)
+                raise WrongOptions
+
+        resource = URL(url=path)
+        if adding:
+            self.shell.account.session.addAttachment(resource, os.path.basename(fname), data, content_type, return_representation)
+        elif updating:
+            self.shell.account.session.updateAttachment(resource, updating, os.path.basename(fname), data, content_type, return_representation)
+        elif removing:
+            self.shell.account.session.removeAttachment(resource, removing)
+
+        return True
+
+
+    def complete(self, text):
+        return self.shell.wdcomplete(text)
+
+
+    def usage(self, name):
+        return """Usage: %s OPTIONS PATH
+PATH is a relative or absolute path.
+
+Options:
+-f   file name of data to put [REQUIRED]
+-t   content-type of data being attached [DEFAULT: text/plain]
+-a   adding new attachment
+-u   updating managed-id
+-r   removing managed-id
+""" % (name,)
+
+
+    def helpDescription(self):
+        return "Add, update or remove an attachment for a calendar object resource on the server."

Modified: CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py	2012-10-31 16:05:59 UTC (rev 9995)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py	2012-10-31 16:06:38 UTC (rev 9996)
@@ -1,4 +1,4 @@
-##
+# #
 # Copyright (c) 2006-2012 Apple Inc. All rights reserved.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,7 +12,7 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-##
+# #
 
 from caldavclientlibrary.client.httpshandler import SmartHTTPConnection
 from caldavclientlibrary.protocol.caldav.definitions import headers
@@ -692,6 +692,64 @@
             self.handleHTTPError(request)
 
 
+    def addAttachment(self, rurl, filename, data, contentType, return_representation):
+
+        assert(isinstance(rurl, URL))
+
+        # Create WebDAV POST
+        rurl.extended = "?action=attachment-add"
+        request = Post(self, rurl.relativeURL())
+        dout = RequestDataString(data, contentType)
+        request.setRequestHeader("Content-Disposition", "attachment;filename=%s" % (filename,))
+        if return_representation:
+            request.setRequestHeader("Prefer", "return-representation")
+        request.setData(dout, None)
+
+        # Process it
+        self.runSession(request)
+
+        # Check response status
+        if request.getStatusCode() not in (statuscodes.OK, statuscodes.Created, statuscodes.NoContent,):
+            self.handleHTTPError(request)
+
+
+    def updateAttachment(self, rurl, managed_id, filename, data, contentType, return_representation):
+
+        assert(isinstance(rurl, URL))
+
+        # Create WebDAV POST
+        rurl.extended = "?action=attachment-update&managed-id=%s" % (managed_id,)
+        request = Post(self, rurl.relativeURL())
+        request.setRequestHeader("Content-Disposition", "attachment;filename=%s" % (filename,))
+        if return_representation:
+            request.setRequestHeader("Prefer", "return-representation")
+        dout = RequestDataString(data, contentType)
+        request.setData(dout, None)
+
+        # Process it
+        self.runSession(request)
+
+        # Check response status
+        if request.getStatusCode() not in (statuscodes.OK, statuscodes.Created, statuscodes.NoContent,):
+            self.handleHTTPError(request)
+
+
+    def removeAttachment(self, rurl, managed_id):
+
+        assert(isinstance(rurl, URL))
+
+        # Create WebDAV POST
+        rurl.extended = "?action=attachment-remove&managed-id=%s" % (managed_id,)
+        request = Post(self, rurl.relativeURL())
+
+        # Process it
+        self.runSession(request)
+
+        # Check response status
+        if request.getStatusCode() not in (statuscodes.OK, statuscodes.NoContent,):
+            self.handleHTTPError(request)
+
+
     def displayHTTPError(self, request):
         print request.status_code
 

Modified: CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/http/requestresponse.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/http/requestresponse.py	2012-10-31 16:05:59 UTC (rev 9995)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/http/requestresponse.py	2012-10-31 16:06:38 UTC (rev 9996)
@@ -38,6 +38,7 @@
     def _initResponse(self):
         self.session = None
         self.request_data = None
+        self.request_headers = {}
         self.response_data = None
         self.method = methods.GET
         self.url = None
@@ -81,6 +82,10 @@
         self.session = session
 
 
+    def setRequestHeader(self, name, value):
+        self.request_headers[name] = value
+
+
     def setData(self, request_data, response_data):
         self.request_data = request_data
         self.response_data = response_data
@@ -147,7 +152,11 @@
         # Check for content
         self.addContentHeaders(hdrs)
 
+        # Do custom headers
+        for name, value in self.request_headers.items():
+            hdrs.append((name, value))
 
+
     def addContentHeaders(self, hdrs):
         # Check for content
         if self.hasRequestData():

Modified: CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/url.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/url.py	2012-10-31 16:05:59 UTC (rev 9995)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/url.py	2012-10-31 16:06:38 UTC (rev 9996)
@@ -147,7 +147,7 @@
             result += (urllib.quote(path) if encode else path)
 
         if self.extended:
-            result += (urllib.quote_plus(self.extended, "?=") if encode else self.extended)
+            result += (urllib.quote_plus(self.extended, "?&=") if encode else self.extended)
 
         return result
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20121031/87d27dcd/attachment-0001.html>


More information about the calendarserver-changes mailing list