Package caldavclientlibrary :: Package admin :: Package xmlaccounts :: Package commands :: Module listrecords
[hide private]
[frames] | no frames]

Source Code for Module caldavclientlibrary.admin.xmlaccounts.commands.listrecords

  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.admin.xmlaccounts.commands.command import Command 
 18  from caldavclientlibrary.admin.xmlaccounts import recordtypes 
 19  import itertools 
 20   
21 -class ListRecords(Command):
22 23 CMDNAME = "list" 24
25 - def __init__(self):
26 super(ListRecords, self).__init__(self.CMDNAME, "List all records of the specified type.")
27
28 - def allRecordsAllowed(self):
29 """ 30 Indicate that this command can list all records in one go as well as each 31 individual record type. 32 """ 33 return True
34
35 - def doCommand(self):
36 """ 37 Run the command. 38 """ 39 self.listRecords(self.recordType)
40
41 - def listRecords(self, recordType):
42 """ 43 Lists records of the specified record type from the directory. 44 """ 45 46 if recordType == recordtypes.recordType_all: 47 users = [l for l in self.directory.records.itervalues()] 48 print "Full List\n" 49 else: 50 users = (self.directory.records[recordType],) 51 print "%s List\n" % (recordType.capitalize(),) 52 53 54 table = [ 55 ["UID", "GUID", "Name", "CUADDR",] 56 ] 57 if recordType == recordtypes.recordType_all: 58 table[0].insert(0, "TYPE") 59 table[0].append("MEMBERS") 60 table[0].append("PROXIES") 61 elif recordType in (recordtypes.recordType_groups,): 62 table[0].append("MEMBERS") 63 elif recordType in (recordtypes.recordType_locations, recordtypes.recordType_resources,): 64 table[0].append("PROXIES") 65 for user in itertools.chain(*users): 66 if len(table) > 1: 67 table.append(None) 68 cuaddrs = user.calendarUserAddresses if user.calendarUserAddresses else ("",) 69 if user.recordType in (recordtypes.recordType_groups,): 70 members = user.members if user.members else (None,) 71 elif user.recordType in (recordtypes.recordType_locations, recordtypes.recordType_resources,): 72 members = user.proxies if user.proxies else (None,) 73 else: 74 members = (None,) 75 for ctr, items in enumerate(map(None, cuaddrs, members,)): 76 cuaddr, member = items 77 if cuaddr is None: 78 cuaddr = "" 79 if member is None: 80 member = "" 81 else: 82 member = "(%s) %s" % (member[0], member[1],) 83 if recordType == recordtypes.recordType_all: 84 row = (user.recordType,) 85 else: 86 row = () 87 row += ( 88 user.uid if not ctr else "", 89 user.guid if not ctr else "", 90 user.name if not ctr else "", 91 cuaddr, 92 ) 93 if recordType == recordtypes.recordType_all: 94 if user.recordType in (recordtypes.recordType_groups,): 95 row += (member, "") 96 elif user.recordType in (recordtypes.recordType_locations, recordtypes.recordType_resources,): 97 row += ("", member,) 98 else: 99 row += ("", "") 100 elif user.recordType in (recordtypes.recordType_groups, recordtypes.recordType_locations, recordtypes.recordType_resources,): 101 row += (member,) 102 table.append(row) 103 104 self.printTable(table) 105 return 1
106
107 - def printTable(self, table):
108 109 maxWidths = [0 for _ignore in table[0]] 110 for row in table: 111 if row is not None: 112 for ctr, col in enumerate(row): 113 maxWidths[ctr] = max(maxWidths[ctr], len(col) if col else 0) 114 115 self.printDivider(maxWidths, False) 116 for rowctr, row in enumerate(table): 117 if row is None: 118 self.printDivider(maxWidths) 119 else: 120 print "|", 121 for colctr, col in enumerate(row): 122 print "%- *s" % (maxWidths[colctr], col if col else ""), 123 print "|", 124 print "" 125 if not rowctr: 126 self.printDivider(maxWidths) 127 self.printDivider(maxWidths, False)
128
129 - def printDivider(self, maxWidths, intermediate=True):
130 t = "|" if intermediate else "+" 131 for widthctr, width in enumerate(maxWidths): 132 t += "-" 133 t += "-" * width 134 t += "-+" if widthctr < len(maxWidths) - 1 else ("-|" if intermediate else "-+") 135 print t
136