1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from caldavclientlibrary.browser import utils
18 from caldavclientlibrary.browser.command import CommandError
19 from caldavclientlibrary.browser.command import UnknownCommand
20 from caldavclientlibrary.protocol.url import URL
21 from caldavclientlibrary.protocol.webdav.definitions import davxml
22 import os
23 import readline
24 import traceback
25
27
38
39 - def readHistory(self):
40 try:
41 readline.read_history_file(os.path.expanduser("~/.%s" % (self.history_name,)))
42 except IOError:
43 pass
44
45 - def saveHistory(self):
46 readline.write_history_file(os.path.expanduser("~/.%s" % (self.history_name,)))
47
49 raise NotImplementedError
50
55
57
58
59 if self.preserve_history:
60 old_history = [readline.get_history_item(index) for index in xrange(readline.get_current_history_length())]
61 readline.clear_history()
62 map(readline.add_history, self.history)
63
64 readline.set_completer(self.complete)
65 readline.parse_and_bind("bind ^I rl_complete")
66
67 while True:
68 cmdline = raw_input("%s > " % (self.prefix,))
69 self.last_wd_complete = ("", ())
70 if not cmdline:
71 continue
72
73
74 try:
75 self.execute(cmdline)
76 except SystemExit, e:
77 print "Exiting shell: %s" % (e.code,)
78 break
79 except UnknownCommand, e:
80 print "Command '%s' unknown." % (e,)
81 except Exception, e:
82 traceback.print_exc()
83
84
85 if self.preserve_history:
86 self.saveHistory()
87 readline.clear_history()
88 map(readline.add_history, old_history)
89
91
92
93 if cmdline == "!!" and self.history:
94 cmdline = self.history[-1]
95 print cmdline
96 if readline.get_current_history_length():
97 readline.replace_history_item(readline.get_current_history_length() - 1, cmdline)
98 elif cmdline.startswith("!"):
99 try:
100 index = int(cmdline[1:])
101 if index> 0 and index <= len(self.history):
102 cmdline = self.history[index-1]
103 print cmdline
104 if readline.get_current_history_length():
105 readline.replace_history_item(readline.get_current_history_length() - 1, cmdline)
106 else:
107 raise ValueError()
108 except ValueError:
109 print "%s: event not found" % (cmdline,)
110 return
111
112
113 splits = cmdline.split(" ", 1)
114 cmd = splits[0]
115 options = splits[1] if len(splits) == 2 else ""
116
117
118 try:
119 if cmd not in self.commands:
120 self.history.append(cmdline)
121 raise UnknownCommand(cmd)
122 else:
123 self.commands[cmd].execute(cmd, options)
124 finally:
125
126 self.history.append(cmdline)
127
128 - def help(self, cmd=None):
151
153
154
155
156 results = []
157 check = readline.get_line_buffer()[:readline.get_endidx()].lstrip()
158 checklen = len(check)
159 if " " not in check:
160 for cmd in self.commands:
161 if cmd[:checklen] == check:
162 results.append(cmd)
163 else:
164 cmd, rest = check.split(" ", 1)
165 if cmd in self.commands:
166 results = self.commands[cmd].complete(rest)
167
168 return results[state]
169
171
172
173
174
175 if self.last_wd_complete[0] == text:
176 return self.last_wd_complete[1]
177
178
179 if text[0] == "/":
180 dirname, _ignore_child = os.path.split(text)
181 path = dirname
182 if not path.endswith("/"):
183 path += "/"
184 pathlen = 0
185 else:
186 path = self.wd
187 pathlen = len(path) + (0 if path.endswith("/") else 1)
188 dirname, _ignore_child = os.path.split(text)
189 if dirname:
190 path = os.path.join(path, dirname)
191 if not path.endswith("/"):
192 path += "/"
193
194
195 resource = URL(url=path)
196
197 props = (davxml.resourcetype,)
198 results = self.account.session.getPropertiesOnHierarchy(resource, props)
199
200 results = [result[pathlen:] for result in results.iterkeys() if len(result) > pathlen]
201
202 if text:
203 textlen = len(text)
204 results = [result for result in results if result[:textlen] == text]
205
206
207 self.last_wd_complete = (text, results,)
208 return results
209