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

Source Code for Module caldavclientlibrary.browser.utils

  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.protocol.url import URL 
 18  from caldavclientlibrary.protocol.webdav.definitions import davxml 
 19  import readline 
 20  import types 
 21   
 22   
23 -def printPrincipalPaths(account, principals, resolve, refresh):
24 25 result = "" 26 if resolve: 27 results = [account.getPrincipal(item, refresh=refresh) for item in principals] 28 results.sort(key=lambda x: x.getSmartDisplayName()) 29 strlen = reduce(lambda x,y: max(x, len(y.getSmartDisplayName()) + 1), results, 0) 30 results = ["%- *s (%s)" % (strlen, principal.getSmartDisplayName(), principal.principalURL) for principal in results] 31 else: 32 results = [item.relativeURL() for item in principals] 33 results.sort() 34 35 if len(results) == 1: 36 return results[0] 37 else: 38 for item in results: 39 result += "\n %s" % (item,) 40 41 return result
42
43 -def printPrincipals(account, principals, resolve, refresh):
44 45 result = "" 46 if resolve: 47 results = list(principals) 48 if refresh: 49 for principal in results: 50 principal.loadDetails(True) 51 results.sort(key=lambda x: x.getSmartDisplayName()) 52 strlen = reduce(lambda x,y: max(x, len(y.getSmartDisplayName()) + 1), results, 0) 53 results = ["%- *s (%s)" % (strlen, principal.getSmartDisplayName(), principal.principalURL) for principal in results] 54 else: 55 results = [item.principalURL for item in principals] 56 results.sort() 57 58 if len(results) == 1: 59 return results[0] 60 else: 61 for item in results: 62 result += "\n %s" % (item,) 63 64 return result
65
66 -def printProxyPrincipals(account, principal, read=True, write=True, resolve=True, refresh_main=False, refresh=False):
67 if read: 68 print " Read-Only Proxies: %s" % printPrincipals(account, principal.getReadProxies(refresh_main), resolve, refresh) 69 refresh_main = False 70 if write: 71 print " Read-Write Proxies: %s" % printPrincipals(account, principal.getWriteProxies(refresh_main), resolve, refresh)
72
73 -def printProperties(items):
74 sorted = items.keys() 75 sorted.sort() 76 for key in sorted: 77 value = items[key] 78 if type(value) in (types.StringType, types.UnicodeType, types.IntType): 79 print " %s: %s" % (key, value,) 80 elif type(value) in (types.ListType, types.TupleType,): 81 print " %s: %s" % (key, printList(value),) 82 else: 83 print " %s: %s" % (key, value,)
84
85 -def printList(items):
86 result = "" 87 if len(items) == 1: 88 return items[0] 89 else: 90 sorted = list(items) 91 sorted.sort() 92 for item in sorted: 93 result += "\n %s" % (item,) 94 return result
95
96 -def printTwoColumnList(items, indent=0):
97 98 strlen = reduce(lambda x,y: max(x, len(y[0]) + 1), items, 0) 99 sorted = list(items) 100 sorted.sort(key=lambda x: x[0]) 101 for col1, col2 in sorted: 102 print "%s%- *s - %s" % (" "*indent, strlen, col1, col2,)
103
104 -def printPrincipalList(principals):
105 106 result = "" 107 for ctr, principal in enumerate(principals): 108 result += "\n% 2d. %s (%s)" % (ctr+1, principal.getSmartDisplayName(), principal.principalURL) 109 return result
110
111 -def printACEList(aces, account):
112 113 result = "" 114 for ctr, ace in enumerate(aces): 115 result += "\n% 2d. %s" % (ctr+1, printACE(ace, account)) 116 return result
117
118 -def printACE(ace, account):
119 120 principalText = ace.principal 121 if principalText == str(davxml.href): 122 principal = account.getPrincipal(URL(url=ace.data)) 123 principalText = "%s (%s)" % (principal.getSmartDisplayName(), principal.principalURL) 124 elif principalText == str(davxml.all): 125 principalText = "ALL" 126 elif principalText == str(davxml.authenticated): 127 principalText = "AUTHENTICATED" 128 elif principalText == str(davxml.unauthenticated): 129 principalText = "UNAUTHENTICATED" 130 elif principalText == str(davxml.property): 131 principalText = "PROPERTY: %s" % (ace.data,) 132 result = "Principal: %s\n" % (principalText,) 133 if ace.invert or ace.protected or ace.inherited: 134 result += " Status:" 135 if ace.invert: 136 result += " INVERTED" 137 if ace.protected: 138 result += " PROTECTED" 139 if ace.inherited: 140 result += " INHERITED" 141 result += "\n" 142 result += " Grant: " if ace.grant else " Deny: " 143 result += ", ".join(ace.privs) 144 result += "\n" 145 return result
146
147 -def textInput(title, insert=None):
148 if insert: 149 title = "%s [%s]:" % (title, insert,) 150 result = raw_input(title) 151 if readline.get_current_history_length(): 152 readline.remove_history_item(readline.get_current_history_length() - 1) 153 if not result: 154 result = insert 155 return result
156
157 -def numericInput(title, low, high, allow_q = False, insert=None):
158 if allow_q: 159 result = choiceInput(title, [str(num) for num in range(low, high+1)] + ["q",], insert) 160 if result != "q": 161 result = int(result) 162 return result 163 else: 164 return int(choiceInput(title, [str(num) for num in range(low, high+1)], insert))
165
166 -def yesNoInput(title, insert=None):
167 return choiceInput(title, ("y", "n"), insert)
168
169 -def choiceInput(title, choices, insert=None):
170 while True: 171 result = textInput(title, insert) 172 if not result: 173 continue 174 if result in choices: 175 return result 176 print "Invalid input. Try again."
177
178 -def multiChoiceInput(title, choices, insert=None):
179 while True: 180 result = textInput(title, insert) 181 if not result: 182 continue 183 for char in result: 184 if char not in choices: 185 break 186 else: 187 return result 188 print "Invalid input. Try again."
189