forked from taers232c/GAM-Scripts3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetLabelsCountSize.py
executable file
·69 lines (61 loc) · 2.46 KB
/
GetLabelsCountSize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
"""
# Purpose: Create a CSV file that totals message label data: count and size
# Note: This script requires Advanced GAM:
# https://github.com/taers232c/GAMADV-XTD3
# Customize: DELIMITER, SHOW_TOTALS
# Python: Use python or python3 below as appropriate to your system; verify that you have version 3
# $ python -V or python3 -V
# Python 3.x.y
# Usage:
# 1: Get the label data
# Single user
# $ gam redirect csv ./LabelData.csv user [email protected] print messages showlabels showsize headers "" delimiter '|'
# Multiple users; replace all users as desired
# $ gam config auto_batch_min 1 redirect csv ./LabelData.csv multiprocess all users print messages showlabels showsize headers "" delimiter '|'
# 2: python3 GetLabelsCountSize.py LabelData.csv LabelSummary.csv
"""
import csv
import sys
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
DELIMITER = '|' # Must match delimiter from command line
SHOW_TOTALS = False # False: Don't show total label counts/size for each user; True: Do show
if (len(sys.argv) > 2) and (sys.argv[2] != '-'):
outputFile = open(sys.argv[2], 'w', encoding='utf-8', newline='')
else:
outputFile = sys.stdout
outputCSV = csv.DictWriter(outputFile, ['User', 'Label', 'Count', 'SizeEstimate'], lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
if (len(sys.argv) > 1) and (sys.argv[1] != '-'):
inputFile = open(sys.argv[1], 'r', encoding='utf-8')
else:
inputFile = sys.stdin
Users = {}
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
user = row['User']
Users.setdefault(user, {})
size = int(row['SizeEstimate'])
for label in row['Labels'].split(DELIMITER):
Users[user].setdefault(label, {'Count': 0, 'SizeEstimate': 0})
Users[user][label]['Count'] +=1
Users[user][label]['SizeEstimate'] += size
for user in sorted(Users):
count = 0
size = 0
for label, data in sorted(iter(Users[user].items())):
count += data['Count']
size += data['SizeEstimate']
outputCSV.writerow({'User': user,
'Label': label,
'Count': data['Count'],
'SizeEstimate': data['SizeEstimate']})
if SHOW_TOTALS:
outputCSV.writerow({'User': user,
'Label': 'Total',
'Count': count,
'SizeEstimate': size})
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()