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

Source Code for Module caldavclientlibrary.browser.commands.put

 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 = ("put", "write",)
29
30 - def execute(self, name, options):
31 32 fname = None 33 content_type = "text/plain" 34 path = None 35 36 opts, args = getopt.getopt(shlex.split(options), 'f:t:') 37 38 for name, value in opts: 39 40 if name == "-f": 41 fname = value 42 elif name == "-t": 43 content_type = value 44 else: 45 print "Unknown option: %s" % (name,) 46 print self.usage(name) 47 raise WrongOptions 48 49 if not fname: 50 print "File name must be provided" 51 print self.usage(name) 52 raise WrongOptions 53 54 elif len(args) > 1: 55 print "Wrong number of arguments: %d" % (len(args),) 56 print self.usage(name) 57 raise WrongOptions 58 elif args: 59 path = args[0] 60 if not path.startswith("/"): 61 path = os.path.join(self.shell.wd, path) 62 if path.endswith("/"): 63 print "Cannot PUT to a directory: %s" % (path,) 64 print self.usage(name) 65 raise WrongOptions 66 else: 67 print "Path to PUT to must be provided" 68 print self.usage(name) 69 raise WrongOptions 70 71 # Read in data 72 try: 73 data = open(fname, "r").read() 74 except IOError: 75 print "Unable to read data from file: %s" % (fname,) 76 print self.usage(name) 77 raise WrongOptions 78 79 resource = URL(url=path) 80 self.shell.account.session.writeData(resource, data, content_type) 81 82 return True
83
84 - def complete(self, text):
86
87 - def usage(self, name):
88 return """Usage: %s OPTIONS PATH 89 PATH is a relative or absolute path. 90 91 Options: 92 -f file name of data to put [REQUIRED] 93 -t content-type of data being put [DEFAULT: text/plain] 94 """ % (name,)
95
96 - def helpDescription(self):
97 return "Write data to a file on the server."
98