print labels with counts

This commit is contained in:
Jay Lee
2021-04-20 15:37:21 -04:00
parent 478804bd5c
commit 6ddfdf2514

View File

@@ -5326,20 +5326,27 @@ def gmail_del_result(request_id, response, exception):
print(exception) print(exception)
def showLabels(users): def printShowLabels(users, show=True):
i = 5 i = 5
onlyUser = showCounts = False onlyUser = False
showCounts = False
todrive = False
while i < len(sys.argv): while i < len(sys.argv):
myarg = sys.argv[i].lower().replace('_', '') myarg = sys.argv[i].lower().replace('_', '')
if myarg == 'onlyuser': if myarg == 'onlyuser':
onlyUser = True onlyUser = True
i += 1 i += 1
elif myarg == 'todrive':
todrive = True
i += 1
elif myarg == 'showcounts': elif myarg == 'showcounts':
showCounts = True showCounts = True
i += 1 i += 1
else: else:
controlflow.invalid_argument_exit(sys.argv[i], controlflow.invalid_argument_exit(sys.argv[i],
'gam <users> show labels') 'gam <users> show labels')
if not show:
titles = ['email']
for user in users: for user in users:
user, gmail = buildGmailGAPIObject(user) user, gmail = buildGmailGAPIObject(user)
if not gmail: if not gmail:
@@ -5347,17 +5354,19 @@ def showLabels(users):
labels = gapi.call(gmail.users().labels(), labels = gapi.call(gmail.users().labels(),
'list', 'list',
userId=user, userId=user,
soft_errors=True) soft_errors=True).get('labels', [])
if labels: i = 0
for label in labels['labels']: for label in labels:
i += 1
if onlyUser and (label['type'] == 'system'): if onlyUser and (label['type'] == 'system'):
continue continue
print(label['name'])
for a_key in label:
if a_key == 'name':
continue
print(f' {a_key}: {label[a_key]}')
if showCounts: if showCounts:
if i >= 50 and not i % 50:
# show label get count for greater than 100 labels
# every 100 labels
sys.stderr.write('\r')
sys.stderr.flush()
sys.stderr.write(f'Getting counts for label {i} of {len(labels)}')
counts = gapi.call( counts = gapi.call(
gmail.users().labels(), gmail.users().labels(),
'get', 'get',
@@ -5366,9 +5375,24 @@ def showLabels(users):
fields= fields=
'messagesTotal,messagesUnread,threadsTotal,threadsUnread' 'messagesTotal,messagesUnread,threadsTotal,threadsUnread'
) )
for a_key in counts: label.update(counts)
print(f' {a_key}: {counts[a_key]}') if show:
print(label['name'])
for a_key in label:
if a_key == 'name':
continue
print(f' {a_key}: {label[a_key]}')
print('') print('')
else:
for key in label:
if key not in titles:
titles.append(key)
label['email'] = user
if not show:
display.write_csv_file(labels,
titles,
list_type='Gmail Labels',
todrive=False)
def showGmailProfile(users): def showGmailProfile(users):
@@ -11730,7 +11754,7 @@ def ProcessGAMCommand(args):
elif command == 'show': elif command == 'show':
showWhat = sys.argv[4].lower() showWhat = sys.argv[4].lower()
if showWhat in ['labels', 'label']: if showWhat in ['labels', 'label']:
showLabels(users) printShowLabels(users)
elif showWhat == 'profile': elif showWhat == 'profile':
showProfile(users) showProfile(users)
elif showWhat == 'calendars': elif showWhat == 'calendars':
@@ -11819,6 +11843,8 @@ def ProcessGAMCommand(args):
printShowTeamDrives(users, True) printShowTeamDrives(users, True)
elif printWhat in ['contactdelegate', 'contactdelegates']: elif printWhat in ['contactdelegate', 'contactdelegates']:
gapi_contactdelegation.print_(users, True) gapi_contactdelegation.print_(users, True)
elif printWhat in ['labels']:
printShowLabels(users, show=False)
else: else:
controlflow.invalid_argument_exit(printWhat, controlflow.invalid_argument_exit(printWhat,
'gam <users> print') 'gam <users> print')