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

Source Code for Module caldavclientlibrary.browser.baseshell

  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 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   
26 -class BaseShell(object):
27
28 - def __init__(self, history_name):
29 30 self.prefix = "" 31 self.commands = {} 32 self.history = [] 33 self.history_name = history_name 34 self.preserve_history = False 35 self.last_wd_complete = ("", ()) 36 37 self.readHistory()
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
48 - def registerCommands(self, cmds):
49 raise NotImplementedError
50
51 - def registerCommand(self, command):
52 for cmd in command.getCmds(): 53 self.commands[cmd] = command 54 command.setShell(self)
55
56 - def run(self):
57 58 # Preserve existing history 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 # Try to dispatch command 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 # Restore previous history 85 if self.preserve_history: 86 self.saveHistory() 87 readline.clear_history() 88 map(readline.add_history, old_history)
89
90 - def execute(self, cmdline):
91 92 # Check for history recall 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 # split the command line into command and options 113 splits = cmdline.split(" ", 1) 114 cmd = splits[0] 115 options = splits[1] if len(splits) == 2 else "" 116 117 # Find matching command 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 # Store in history 126 self.history.append(cmdline)
127
128 - def help(self, cmd=None):
129 130 if cmd: 131 if cmd in self.commands: 132 cmds = ((cmd, self.commands[cmd]),) 133 full_help = True 134 else: 135 raise CommandError("Command could not be found: %s" % (cmd,)) 136 else: 137 cmds = self.commands.keys() 138 cmds.sort() 139 cmds = [(cmd, self.commands[cmd]) for cmd in cmds] 140 full_help = False 141 142 if full_help: 143 if self.commands[cmd].hasHelp(cmd): 144 print self.commands[cmd].help(cmd) 145 else: 146 results = [] 147 for name, cmd in cmds: 148 if cmd.hasHelp(name): 149 results.append(cmd.helpListing(name)) 150 utils.printTwoColumnList(results)
151
152 - def complete(self, text, state):
153 154 # If there is no space in the text we complete a command 155 #print "complete: %s %d" % (text, state) 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
170 - def wdcomplete(self, text):
171 172 #print "\nwdcomplete: %s" % (text,) 173 174 # Look at cache and return that 175 if self.last_wd_complete[0] == text: 176 return self.last_wd_complete[1] 177 178 # Look for relative vs absolute 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 #print "pdc: %s, %s, %s, %s" % (self.wd, path, dirname, child) 195 resource = URL(url=path) 196 197 props = (davxml.resourcetype,) 198 results = self.account.session.getPropertiesOnHierarchy(resource, props) 199 #print results.keys() 200 results = [result[pathlen:] for result in results.iterkeys() if len(result) > pathlen] 201 #print results 202 if text: 203 textlen = len(text) 204 results = [result for result in results if result[:textlen] == text] 205 #print results 206 207 self.last_wd_complete = (text, results,) 208 return results
209