forked from taers232c/GAM-Scripts3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShowDelegators.py
executable file
·48 lines (40 loc) · 1.76 KB
/
ShowDelegators.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
#!/usr/bin/env python3
"""
# Purpose: Make a CSV file showing delegators for delegates
# Note: This script can use Basic or Advanced GAM:
# https://github.com/jay0lee/GAM
# https://github.com/taers232c/GAMADV-X, https://github.com/taers232c/GAMADV-XTD, https://github.com/taers232c/GAMADV-XTD3
# Usage:
# 1: Get delegates
# $ Basic: gam all users print delegates > ./AllDelegates.csv
# $ Advanced: gam all users print delegates shownames > ./AllDelegates.csv
# 2: From that list of delegates, output a CSV file with headers "Delegate,Delegate Email,Delegators
# $ python ShowDelegators.py ./AllDelegates.csv ./AllDelegators.csv
"""
import csv
import sys
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
delegates = {}
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, ['Delegate', 'Delegate Email', 'Delegators'], 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
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
delegate = row['delegateAddress']
delegates.setdefault(delegate, {'Delegate': row.get('delegateName', delegate), 'Delegators': []})
delegates[delegate]['Delegators'].append(row['User'])
for delegate in sorted(delegates):
outputCSV.writerow({'Delegate': delegates[delegate]['Delegate'],
'Delegate Email': delegate,
'Delegators': ' '.join(delegates[delegate]['Delegators'])})
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()