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

source_changes at macosforge.org source_changes at macosforge.org
Wed Sep 25 01:08:46 PDT 2013


Revision: 11755
          http://trac.calendarserver.org//changeset/11755
Author:   cdaboo at apple.com
Date:     2013-09-25 01:08:46 -0700 (Wed, 25 Sep 2013)
Log Message:
-----------
Implement generic POST method.

Modified Paths:
--------------
    CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/__init__.py
    CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py

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

Modified: CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/__init__.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/__init__.py	2013-09-25 01:42:01 UTC (rev 11754)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/__init__.py	2013-09-25 08:08:46 UTC (rev 11755)
@@ -31,6 +31,7 @@
     "mkdir",
     "mv",
     "notifications",
+    "post",
     "principal",
     "props",
     "proxies",

Added: CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/post.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/post.py	                        (rev 0)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/browser/commands/post.py	2013-09-25 08:08:46 UTC (rev 11755)
@@ -0,0 +1,95 @@
+##
+# Copyright (c) 2007-2013 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 = ("post",)
+        self.do_wd_complete = True
+
+
+    def execute(self, cmdname, options):
+
+        fname = None
+        content_type = "text/plain"
+        path = None
+
+        opts, args = getopt.getopt(shlex.split(options), 'f:t:')
+
+        for name, value in opts:
+
+            if name == "-f":
+                fname = value
+            elif name == "-t":
+                content_type = value
+            else:
+                print "Unknown option: %s" % (name,)
+                print self.usage(cmdname)
+                raise WrongOptions
+
+        if not fname:
+            print "File name must 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)
+        else:
+            print "Path to POST must be provided"
+            print self.usage(cmdname)
+            raise WrongOptions
+
+        # Read in data
+        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)
+        self.shell.account.session.writeData(resource, data, content_type, method="POST")
+
+        return True
+
+
+    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 put [DEFAULT: text/plain]
+""" % (name,)
+
+
+    def helpDescription(self):
+        return "Post data to a resource on the server."

Modified: CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py	2013-09-25 01:42:01 UTC (rev 11754)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py	2013-09-25 08:08:46 UTC (rev 11755)
@@ -718,14 +718,20 @@
         return dout.getData(), etag
 
 
-    def writeData(self, rurl, data, contentType, etag=None):
+    def writeData(self, rurl, data, contentType, etag=None, method="PUT"):
 
         assert(isinstance(rurl, URL))
 
         # Create WebDAV PUT
-        request = Put(self, rurl.relativeURL())
+        if method == "PUT":
+            request = Put(self, rurl.relativeURL())
+        elif method == "POST":
+            request = Post(self, rurl.relativeURL())
         dout = RequestDataString(data, contentType)
-        request.setData(dout, None, etag=etag)
+        if method == "PUT":
+            request.setData(dout, None, etag=etag)
+        elif method == "POST":
+            request.setData(dout, None)
 
         # Process it
         self.runSession(request)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130925/96519ed0/attachment-0001.html>


More information about the calendarserver-changes mailing list