1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
25
29
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
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
92
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
106 return "Import data to a collection on the server."
107