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

Source Code for Module caldavclientlibrary.browser.shell

  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.baseshell import BaseShell 
 18  from caldavclientlibrary.browser.command import Command 
 19  from caldavclientlibrary.client.account import CalDAVAccount 
 20  from getpass import getpass 
 21  from caldavclientlibrary.protocol.url import URL 
 22  import caldavclientlibrary.browser.commands 
 23  import atexit 
 24  import getopt 
 25  import sys 
 26  import urlparse 
 27   
28 -class Shell(BaseShell):
29
30 - def __init__(self, server, path, user, pswd, logging):
31 32 super(Shell, self).__init__("caldav_client") 33 self.prefix = self.wd = "/" 34 self.server = server 35 self.user = user 36 self.pswd = pswd 37 38 self.registerCommands() 39 40 # Create the account 41 ssl = server.startswith("https://") 42 server = server[8:] if ssl else server[7:] 43 paths = path 44 self.account = CalDAVAccount(server, ssl=ssl, user=self.user, pswd=self.pswd, root=paths, principal=paths, logging=logging) 45 46 atexit.register(self.saveHistory)
47
48 - def registerCommands(self):
49 module = caldavclientlibrary.browser.commands 50 for item in module.__all__: 51 mod = __import__("caldavclientlibrary.browser.commands." + item, globals(), locals(), ["Cmd",]) 52 cmd_class = mod.Cmd 53 if type(cmd_class) is type and issubclass(cmd_class, Command): 54 self.registerCommand(cmd_class())
55
56 - def setWD(self, newwd):
57 58 # Check that the new one exists 59 resource = (newwd if newwd.endswith("/") else newwd + "/") 60 if not self.account.session.testResource(URL(url=resource)): 61 return False 62 self.prefix = self.wd = newwd 63 return True
64
65 - def setUserPswd(self, user, pswd):
66 67 self.user = user 68 self.pswd = pswd 69 self.account.setUserPswd(user, pswd)
70
71 -def usage():
72 return """Usage: shell [OPTIONS] 73 74 Options: 75 -l start with HTTP logging on. 76 --server=HOST url of the server include http/https scheme and port [REQUIRED]. 77 --user=USER user name to login as - will be prompted if not prsent [OPTIONAL]. 78 --pswd=PSWD password for user - will be prompted if not prsent [OPTIONAL]. 79 """
80
81 -def runit():
82 logging = False 83 server = None 84 user = None 85 pswd = None 86 87 opts, _ignore_args = getopt.getopt(sys.argv[1:], 'lh', ["help", "server=", "user=", "pswd="]) 88 89 for name, value in opts: 90 91 if name == "-l": 92 logging = True 93 elif name == "--server": 94 server = value 95 elif name == "--user": 96 user = value 97 elif name == "--pswd": 98 pswd = value 99 else: 100 print usage() 101 raise SystemExit() 102 103 if not server or not (server.startswith("http://") or server.startswith("https://")): 104 print usage() 105 raise SystemExit() 106 splits = urlparse.urlsplit(server) 107 server = splits.scheme + "://" + splits.netloc 108 path = splits.path 109 if not path: 110 path = "/" 111 112 if not user: 113 user = raw_input("User: ") 114 if not pswd: 115 pswd = getpass("Password: ") 116 117 shell = Shell(server, path, user, pswd, logging) 118 shell.run()
119 120 if __name__ == '__main__': 121 122 runit() 123