[CalendarServer-changes] [7416] CalDAVClientLibrary/trunk/src

source_changes at macosforge.org source_changes at macosforge.org
Thu May 5 19:58:04 PDT 2011


Revision: 7416
          http://trac.macosforge.org/projects/calendarserver/changeset/7416
Author:   cdaboo at apple.com
Date:     2011-05-05 19:58:02 -0700 (Thu, 05 May 2011)
Log Message:
-----------
Add an import command for simple bulk imports.

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

Added Paths:
-----------
    CalDAVClientLibrary/trunk/src/browser/commands/import.py
    CalDAVClientLibrary/trunk/src/protocol/webdav/post.py

Modified: CalDAVClientLibrary/trunk/src/browser/commands/__init__.py
===================================================================
--- CalDAVClientLibrary/trunk/src/browser/commands/__init__.py	2011-05-05 19:30:12 UTC (rev 7415)
+++ CalDAVClientLibrary/trunk/src/browser/commands/__init__.py	2011-05-06 02:58:02 UTC (rev 7416)
@@ -20,6 +20,7 @@
     "cd",
     "help",
     "history",
+    "import",
     "logging",
     "ls",
     "mkadbk",

Added: CalDAVClientLibrary/trunk/src/browser/commands/import.py
===================================================================
--- CalDAVClientLibrary/trunk/src/browser/commands/import.py	                        (rev 0)
+++ CalDAVClientLibrary/trunk/src/browser/commands/import.py	2011-05-06 02:58:02 UTC (rev 7416)
@@ -0,0 +1,106 @@
+##
+# Copyright (c) 2007-2011 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 browser.command import Command
+from browser.command import WrongOptions
+from protocol.url import URL
+import os
+import getopt
+import shlex
+
+class Cmd(Command):
+    
+    def __init__(self):
+        super(Command, self).__init__()
+        self.cmds = ("import",)
+        
+    def execute(self, name, options):
+        
+        fname = None
+        content_type = None
+        path = None
+
+        opts, args = getopt.getopt(shlex.split(options), 'acf:')
+
+        for name, value in opts:
+            
+            if name == "-f":
+                fname = value
+            elif name == "-a":
+                if content_type:
+                    raise WrongOptions
+                content_type = "text/vcard"
+            elif name == "-c":
+                content_type = "text/calendar"
+                if content_type:
+                    raise WrongOptions
+            else:
+                print "Unknown option: %s" % (name,)
+                print self.usage(name)
+                raise WrongOptions
+        
+        if not fname:
+            print "File name must be provided"
+            print self.usage(name)
+            raise WrongOptions
+            
+        elif len(args) > 1:
+            print "Wrong number of arguments: %d" % (len(args),)
+            print self.usage(name)
+            raise WrongOptions
+        elif args:
+            path = args[0]
+            if not path.startswith("/"):
+                path = os.path.join(self.shell.wd, path)
+            if not path.endswith("/"):
+                print "Can only POST to a directory: %s" % (path,)
+                print self.usage(name)
+                raise WrongOptions
+        else:
+            print "Path to POST to must be provided"
+            print self.usage(name)
+            raise WrongOptions
+
+        # Read in data
+        try:
+            data = open(os.path.expanduser(fname), "r").read()
+        except IOError:
+            print "Unable to read data from file: %s" % (fname,)
+            print self.usage(name)
+            raise WrongOptions
+
+        resource = URL(url=path)
+        self.shell.account.session.importData(resource, data, content_type)
+
+        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 post [REQUIRED]
+-c   import calendar data
+-a   import address book data
+
+One of -c or -a is REQUIRED.
+""" % (name,)
+
+    def helpDescription(self):
+        return "Import data to a collection on the server."

Modified: CalDAVClientLibrary/trunk/src/client/clientsession.py
===================================================================
--- CalDAVClientLibrary/trunk/src/client/clientsession.py	2011-05-05 19:30:12 UTC (rev 7415)
+++ CalDAVClientLibrary/trunk/src/client/clientsession.py	2011-05-06 02:58:02 UTC (rev 7416)
@@ -33,6 +33,7 @@
 from protocol.webdav.get import Get
 from protocol.webdav.makecollection import MakeCollection
 from protocol.webdav.move import Move
+from protocol.webdav.post import Post
 from protocol.webdav.principalmatch import PrincipalMatch
 from protocol.webdav.propall import PropAll
 from protocol.webdav.propfind import PropFind
@@ -550,6 +551,22 @@
         if request.getStatusCode() not in (statuscodes.OK, statuscodes.Created, statuscodes.NoContent,):
             self.handleHTTPError(request)
 
+    def importData(self, rurl, data, contentType):
+
+        assert(isinstance(rurl, URL))
+
+        # Create WebDAV POST
+        request = Post(self, rurl.relativeURL())
+        dout = RequestDataString(data, contentType)
+        request.setData(dout, None)
+    
+        # Process it
+        self.runSession(request)
+    
+        # Check response status
+        if request.getStatusCode() not in (statuscodes.OK, statuscodes.MultiStatus, statuscodes.NoContent,):
+            self.handleHTTPError(request)
+
     def displayHTTPError(self, request):
         print request.status_code
 
@@ -701,6 +718,7 @@
             ))
             for hdr in response.msg.headers:
                 self.log.write(hdr)
+            self.log.write("\n")
 
         # Now get the data
         self.readResponseData(request, response)

Added: CalDAVClientLibrary/trunk/src/protocol/webdav/post.py
===================================================================
--- CalDAVClientLibrary/trunk/src/protocol/webdav/post.py	                        (rev 0)
+++ CalDAVClientLibrary/trunk/src/protocol/webdav/post.py	2011-05-06 02:58:02 UTC (rev 7416)
@@ -0,0 +1,23 @@
+##
+# Copyright (c) 2007-2011 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 protocol.webdav.definitions import methods
+from protocol.webdav.requestresponse import RequestResponse
+
+class Post(RequestResponse):
+
+    def __init__(self, session, url):
+        super(Post, self).__init__(session, methods.POST, url)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110505/59dbcdb6/attachment.html>


More information about the calendarserver-changes mailing list