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

Source Code for Module caldavclientlibrary.browser.commands.proxies

  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  from caldavclientlibrary.browser import utils 
 21  from caldavclientlibrary.browser.subshell import SubShell 
 22  from caldavclientlibrary.browser import commands 
 23  import getopt 
 24  import shlex 
 25   
26 -class Cmd(Command):
27
28 - def __init__(self):
29 super(Command, self).__init__() 30 self.cmds = ("proxies", ) 31 self.subshell = None
32
33 - def execute(self, name, options):
34 35 interactive = False 36 read = False 37 write = False 38 principal = self.shell.account.getPrincipal() 39 40 try: 41 opts, args = getopt.getopt(shlex.split(options), 'irwp:') 42 except getopt.GetoptError, e: 43 print str(e) 44 print self.usage(name) 45 raise WrongOptions 46 47 for name, value in opts: 48 49 if name == "-i": 50 interactive = True 51 elif name == "-r": 52 read = True 53 elif name == "-w": 54 write = True 55 elif name == "-p": 56 principal = self.shell.account.getPrincipal(URL(url=value)) 57 else: 58 print "Unknown option: %s" % (name,) 59 print self.usage(name) 60 raise WrongOptions 61 62 if len(args) > 0: 63 print "Wrong number of arguments: %d" % (len(args),) 64 print self.usage(name) 65 raise WrongOptions 66 67 if interactive: 68 self.doInteractiveMode(principal) 69 else: 70 utils.printProxyPrincipals(self.shell.account, principal, read or not write, write or not read, True) 71 72 return True
73
74 - def doInteractiveMode(self, principal):
75 76 print "Entering Proxy edit mode on principal: %s (%s)" % (principal.getSmartDisplayName(), principal.principalURL) 77 if not self.subshell: 78 self.subshell = SubShell(self.shell, "Proxy", ( 79 commands.help.Cmd(), 80 commands.logging.Cmd(), 81 commands.quit.Cmd(), 82 Add(), 83 Remove(), 84 List(), 85 )) 86 self.subshell.principal = principal 87 self.subshell.account = self.shell.account 88 self.subshell.run()
89
90 - def usage(self, name):
91 return """Usage: %s [OPTIONS] 92 PRINCIPAL - principal path to request proxies for. 93 94 Options: 95 -i interactive mode for adding, changing and deleting proxies. 96 -r read proxies [OPTIONAL] 97 -w write proxies [OPTIONAL] 98 if neither is present, both are displayed. 99 100 -p principal path to request proxies for [OPTIONAL] 101 if not present, the current user's principal is used. 102 """ % (name,)
103
104 - def helpDescription(self):
105 return "Displays the delegates for the chosen user."
106
107 -class CommonProxiesCommand(Command):
108
109 - def parseOptions(self, name, options):
110 read = False 111 write = False 112 113 try: 114 opts, args = getopt.getopt(options.split(), 'rw') 115 except getopt.GetoptError, e: 116 print str(e) 117 print self.usage(name) 118 raise WrongOptions 119 120 for name, _ignore_value in opts: 121 122 if name == "-r": 123 read = True 124 if write: 125 print "Only one of -r and -w may be specified." 126 print self.usage(name) 127 raise WrongOptions 128 elif name == "-w": 129 write = True 130 if read: 131 print "Only one of -r and -w may be specified." 132 print self.usage(name) 133 raise WrongOptions 134 else: 135 print "Unknown option: %s" % (name,) 136 print self.usage(name) 137 raise WrongOptions 138 139 if not read and not write: 140 print "One of -r and -w must be specified." 141 print self.usage(name) 142 raise WrongOptions 143 144 if len(args) > 0: 145 print "Wrong number of arguments: %d" % (len(args),) 146 print self.usage(name) 147 raise WrongOptions 148 149 return read
150
151 - def printProxyList(self, read):
152 153 if read: 154 principals = self.shell.principal.getReadProxies() 155 else: 156 principals = self.shell.principal.getWriteProxies() 157 if principals: 158 print utils.printPrincipalList(principals) 159 else: 160 print "There are no proxies of the specified type." 161 return principals
162
163 -class Add(CommonProxiesCommand):
164 - def __init__(self):
165 super(Command, self).__init__() 166 self.cmds = ("add",)
167
168 - def execute(self, name, options):
169 170 read = self.parseOptions(name, options) 171 172 principals = self.printProxyList(read) 173 174 choice = utils.textInput("New principal [q - quit]: ") 175 if choice == "q": 176 return None 177 principal = self.shell.account.getPrincipal(URL(url=choice)) 178 if principal: 179 principals.append(principal) 180 principals = [principal.principalURL for principal in principals] 181 if read: 182 self.shell.principal.setReadProxies(principals) 183 else: 184 self.shell.principal.setWriteProxies(principals)
185
186 - def usage(self, name):
187 return """Usage: %s [OPTIONS] 188 189 Options: 190 -r add to read-only proxy list 191 -w add to read-write proxy list 192 one of -r or -w must be present. 193 """ % (name,)
194
195 - def helpDescription(self):
196 return "Add proxies on principal."
197
198 -class Remove(CommonProxiesCommand):
199 - def __init__(self):
200 super(Command, self).__init__() 201 self.cmds = ("remove",)
202
203 - def execute(self, name, options):
204 205 read = self.parseOptions(name, options) 206 207 principals = self.printProxyList(read) 208 209 choice = utils.numericInput("Select principal: ", 1, len(principals), allow_q=True) 210 if choice == "q": 211 return None 212 del principals[choice-1] 213 principals = [principal.principalURL for principal in principals] 214 if read: 215 self.shell.principal.setReadProxies(principals) 216 else: 217 self.shell.principal.setWriteProxies(principals)
218
219 - def usage(self, name):
220 return """Usage: %s [OPTIONS] 221 222 Options: 223 -r remove from read-only proxy list 224 -w remove from read-write proxy list 225 one of -r or -w must be present. 226 """ % (name,)
227
228 - def helpDescription(self):
229 return "Remove proxies on principal."
230
231 -class List(CommonProxiesCommand):
232 - def __init__(self):
233 super(Command, self).__init__() 234 self.cmds = ("list",)
235
236 - def execute(self, name, options):
237 238 utils.printProxyPrincipals(self.shell.account, self.shell.principal, True, True, True, True) 239 return True
240
241 - def usage(self, name):
242 return """Usage: %s 243 """ % (name,)
244
245 - def helpDescription(self):
246 return "List current ACLs on existing resource."
247