Package caldavclientlibrary :: Package browser :: Package commands :: Module import
[hide private]
[frames] | no frames]

Source Code for Module caldavclientlibrary.browser.commands.import

  1  ## 
  2  # Copyright (c) 2007-2016 Apple Inc. All rights reserved. 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  # http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  ## 
 16   
 17  from caldavclientlibrary.browser.command import Command 
 18  from caldavclientlibrary.browser.command import WrongOptions 
 19  from caldavclientlibrary.protocol.url import URL 
 20  import os 
 21  import getopt 
 22  import shlex 
 23   
24 -class Cmd(Command):
25
26 - def __init__(self):
27 super(Command, self).__init__() 28 self.cmds = ("import",)
29
30 - def execute(self, name, options):
31 32 fname = None 33 content_type = None 34 path = None 35 36 opts, args = getopt.getopt(shlex.split(options), 'acf:') 37 38 for name, value in opts: 39 40 if name == "-f": 41 fname = value 42 elif name == "-a": 43 if content_type: 44 raise WrongOptions 45 content_type = "text/vcard" 46 elif name == "-c": 47 content_type = "text/calendar" 48 if content_type: 49 raise WrongOptions 50 else: 51 print "Unknown option: %s" % (name,) 52 print self.usage(name) 53 raise WrongOptions 54 55 if not fname: 56 print "File name must be provided" 57 print self.usage(name) 58 raise WrongOptions 59 60 elif len(args) > 1: 61 print "Wrong number of arguments: %d" % (len(args),) 62 print self.usage(name) 63 raise WrongOptions 64 elif args: 65 path = args[0] 66 if not path.startswith("/"): 67 path = os.path.join(self.shell.wd, path) 68 if not path.endswith("/"): 69 print "Can only POST to a directory: %s" % (path,) 70 print self.usage(name) 71 raise WrongOptions 72 else: 73 print "Path to POST to must be provided" 74 print self.usage(name) 75 raise WrongOptions 76 77 # Read in data 78 try: 79 data = open(os.path.expanduser(fname), "r").read() 80 except IOError: 81 print "Unable to read data from file: %s" % (fname,) 82 print self.usage(name) 83 raise WrongOptions 84 85 resource = URL(url=path) 86 self.shell.account.session.importData(resource, data, content_type) 87 88 return True
89
90 - def complete(self, text):
91 return self.shell.wdcomplete(text)
92
93 - def usage(self, name):
94 return """Usage: %s OPTIONS PATH 95 PATH is a relative or absolute path. 96 97 Options: 98 -f file name of data to post [REQUIRED] 99 -c import calendar data 100 -a import address book data 101 102 One of -c or -a is REQUIRED. 103 """ % (name,)
104
105 - def helpDescription(self):
106 return "Import data to a collection on the server."
107