-
Notifications
You must be signed in to change notification settings - Fork 50
/
donors.py
154 lines (131 loc) · 4.5 KB
/
donors.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python
'''Add and remove names in the supporters list.
Usage:
python3 donors.py -a "New Name"
python3 donors.py -d "Name To Remove"
python3 donors.py -i file_of_names_to_add.txt
The appendix-donor.tex file is updated in place. Use git revert to undo
changes.
'''
import argparse
import re
import sys
DEFAULT_DONOR_FILE = 'appendix-donors.tex'
PAGE_START = '''\\setlength{\\tabcolsep}{1mm}
\\begin{tabular}{p{4cm}p{4cm}p{4cm}}
'''
PAGE_NEWPAGE = '''\\newpage
'''
PAGE_END = '''\\end{tabular}
'''
PAGE_LINE_COUNT = 43
PAGE_LINE_COUNT_NEXT = 46
NAME_LINE_RE = re.compile(r'(.*) & (.*) & (.*)\s*\\\\')
def list_get(lst, idx):
try:
return lst[idx]
except IndexError:
return None
def norm_letters(name):
for a, b in (
('é', 'e'),
('ş', 's'),
('ü', 'u'),
('ö', 'o'),
('ä', 'a')):
name = name.replace(a, b)
return name
class Donors:
def __init__(self):
self.preamble = None
self.postamble = None
self.names = []
@classmethod
def from_donor_str(cls, txt):
donors = cls()
pos = 0
while True:
page_start_i = txt.find(PAGE_START, pos)
if page_start_i == -1:
donors.postamble = txt[pos:]
break
if donors.preamble is None:
donors.preamble = txt[:page_start_i]
page_end_i = txt.find(PAGE_END, page_start_i)
if page_end_i == -1:
raise ValueError(
'Found a page start without a page end, pos ' +
str(page_start_i))
pos = page_end_i + len(PAGE_END)
page_lines = txt[page_start_i:page_end_i].split('\n')
for line in page_lines:
m = NAME_LINE_RE.match(line)
if not m:
continue
donors.names.extend(
[n.strip() for n in m.groups() if n.strip()])
return donors
@classmethod
def from_donor_file(cls, path=DEFAULT_DONOR_FILE):
with open(path) as fh:
return cls.from_donor_str(fh.read())
def to_donor_str(self):
self.names.sort(key=lambda x: norm_letters(x.strip().lower()))
parts = []
parts.append(self.preamble or '')
pagenum = 1
name_i = 0
line_count = PAGE_LINE_COUNT
while name_i < len(self.names):
cols = [self.names[name_i:name_i + line_count],
self.names[name_i + line_count:name_i + line_count*2],
self.names[name_i + line_count*2:name_i + line_count*3]]
if pagenum > 1:
parts.append('% page ' + str(pagenum) + '\n')
parts.append(PAGE_NEWPAGE)
parts.append(PAGE_START)
for n in range(line_count):
parts.append(list_get(cols[0], n) or '')
parts.append(' & ')
parts.append(list_get(cols[1], n) or '')
parts.append(' & ')
parts.append(list_get(cols[2], n) or '')
parts.append(' \\\\\n')
parts.append(PAGE_END)
name_i += line_count * 3
pagenum += 1
line_count = PAGE_LINE_COUNT_NEXT
parts.append(self.postamble or '')
return ''.join(parts)
def to_donor_file(self, path=DEFAULT_DONOR_FILE):
with open(path, 'w') as fh:
fh.write(self.to_donor_str())
def add_name(self, name):
self.names.append(name.strip())
def delete_name(self, name):
name = name.strip()
name_i = self.names.find(name)
if name_i != -1:
del self.names[name_i]
def main(raw_args):
arg_parser = argparse.ArgumentParser(
prog='donors',
description='Add and delete names from the donors list appendix')
arg_parser.add_argument('-a', '--add', action='append')
arg_parser.add_argument('-d', '--delete', action='append')
arg_parser.add_argument('-i', '--input-file')
arg_parser.add_argument('--donor-file', default=DEFAULT_DONOR_FILE)
args = arg_parser.parse_args(raw_args)
dnrs = Donors.from_donor_file(args.donor_file)
if args.add is not None:
for name in args.add:
print(f'Added "{name}"')
dnrs.add_name(name)
if args.delete is not None:
for name in args.delete:
print(f'Deleted "{name}"')
dnrs.delete_name(name)
dnrs.to_donor_file()
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))