-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathabif.py
More file actions
executable file
·351 lines (275 loc) · 10.5 KB
/
abif.py
File metadata and controls
executable file
·351 lines (275 loc) · 10.5 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/python3
"""ABIF - Aggregated Ballot Information Format
The Aggregated Ballot Information Format (ABIF) is a format for
concisely expressing the results of an ranked or rated election.
"""
import abif
import argparse
import json
import os
import re
from lark import Lark, Transformer, v_args
from lark.exceptions import UnexpectedToken, UnexpectedCharacters
ABIF_DIR = os.path.dirname(os.path.realpath(__file__, strict=True))
ABIF_GRAMMAR_FILE = os.path.join(ABIF_DIR, 'abif.ebnf')
with open(ABIF_GRAMMAR_FILE, 'r', encoding='utf-8') as f:
ABIF_GRAMMAR_STR = f.read()
ABIF_WORKING_TESTS = [
'testfiles/test001.abif',
'testfiles/test002.abif',
'testfiles/test003.abif',
'testfiles/test004.abif',
'testfiles/test010.abif',
'testfiles/test011.abif',
'testfiles/test012.abif',
'testfiles/test013.abif',
'testfiles/test015.abif',
'testfiles/test016.abif',
'testfiles/test017.abif',
'testfiles/test018.abif',
'testfiles/test019.abif'
]
FOOTER = """----------------
For more detailed analysis, try abiftool or awt (the "abif web tool")
which can be found at the following locations:
* abiftool: https://github.com/electorama/abiftool
* awt: https://abif.electorama.com/
"""
@v_args(inline=True)
class ABIFtoJabmodTransformer(Transformer):
def __init__(self):
super().__init__()
self.ballotcount = 0
self.votelines = []
self.candidates = {}
self.metadata = {}
def comment_string(self, token):
return str(token)
def comment(self, *args):
for arg in args:
if isinstance(arg, str) and arg != '#':
return ("comment", str(arg))
return ("comment", "")
def ballot_count(self, token):
count = int(token)
self.ballotcount += count
return count
def metadata_pair(self, key, colon, value):
retkey = str(key).strip('"')
retval = value
return (retkey, retval)
def metadata_key(self, key):
return key
def metadata_value(self, value):
if isinstance(value, str):
if value == "true":
retval = True
elif value == "false":
retval = False
elif value == "null":
retval = None
elif value.startswith('"') and value.endswith('"'):
# Strip quotes but keep as a string
retval = value[1:-1]
elif re.fullmatch(r'-?\d+', value):
return int(value)
# regex for simple decimal/floating-point number
elif re.fullmatch(r'-?\d+\.\d+', value):
retval = float(value)
else:
retval = value
return retval
def metadata_line(self, *items):
for item in items:
if isinstance(item, tuple) and len(item) == 2:
key, value = item
if key != "comment":
self.metadata[key] = value
return None
def cand_bare(self, token):
# Handle bare (unquoted) candidate keys
return str(token)
def cand_doublequote_quoted(self, token):
# Handle double-quoted candidate keys
return str(token).strip('"') # Remove quotes
def cand_square_quoted(self, open_bracket, content, close_bracket):
return str(content)
def cand_tok(self, item):
return item
def cand_id_sep(self, colon):
return colon
def cand_line(self, *items):
# Extract candidate ID and name
if len(items) >= 3 and str(items[0]) == '=':
id_token = items[1]
content_index = 3
else:
id_token = items[0]
content_index = 2
if content_index < len(items):
name_token = items[content_index]
cand_id = str(id_token)
# Process name from the token
# If already processed by cand_square_quoted, it will be a string
name = str(name_token)
self.candidates[cand_id] = name
return None
def cand_id(self, token):
# Always return a dictionary to be consistent with cand_tok_rating
return {"candidate": str(token), "rating": None}
def cand_tok_rating(self, cand_id, _, rating):
# Make sure cand_id is properly handled whether it's a string or dict
candidate = cand_id["candidate"] if isinstance(cand_id, dict) else str(cand_id)
return {"candidate": candidate, "rating": int(rating)}
# This method ensures preference items are always dictionaries
def pref_item(self, item):
if isinstance(item, dict) and "candidate" in item:
return item
elif isinstance(item, str):
return {"candidate": item, "rating": None}
return item
def pref_sep(self, sep):
return str(sep)
def eq(self, *args):
return "="
def gt(self, *args):
return ">"
def comma(self, *args):
return ","
def count_sep(self, *args):
return ":"
def prefs(self, first_item, *args):
prefs = [first_item]
separators = []
for i, arg in enumerate(args):
if isinstance(arg, str) and arg in ['>', '=', ',']:
separators.append(arg)
elif isinstance(arg, dict) and "candidate" in arg:
prefs.append(arg)
return {"prefs": prefs, "separators": separators}
def voteline(self, count, _, elements=None, *comment_parts):
# Handle case of blank ballots
preferences = []
separators = []
if elements:
preferences = elements.get("prefs", [])
separators = elements.get("separators", [])
# Build the preference string
prefstr_parts = []
# Extract the comment if present
comment = None
for part in comment_parts:
if isinstance(part, tuple) and part[0] == "comment":
comment = part[1]
# Build prefs structure for jabmod
prefs = {}
current_rank = 1 # Track the current rank
for i, pref in enumerate(preferences):
candidate = pref["candidate"]
rating = pref["rating"]
pref_data = {}
pref_data["rank"] = current_rank # Use tracked rank instead of index
if rating:
pref_data["rating"] = rating
# Add delimiter if not the last preference
if i < len(separators):
delimiter = separators[i]
pref_data["nextdelim"] = delimiter
# Only increment rank on ">" delimiter, not on "=" delimiter
if delimiter == ">":
current_rank += 1
prefstr_parts.append(
f"{candidate}/{rating}{delimiter}" if rating is not None else f"{candidate}{delimiter}")
else:
prefstr_parts.append(
f"{candidate}/{rating}" if rating is not None else candidate)
prefs[candidate] = pref_data
# Build the prefstr
prefstr = "".join(prefstr_parts)
voteline = {
"qty": count,
"comment": comment,
"prefs": prefs,
"prefstr": prefstr
}
self.votelines.append(voteline)
return None
def get_jabmod(self):
"""Return the complete jabmod structure."""
# Find all unique candidate IDs used in votelines
voteline_candidates = set()
for voteline in self.votelines:
for candidate in voteline.get('prefs', {}).keys():
voteline_candidates.add(candidate)
# Add any candidates found in votelines but not explicitly defined
for candidate_id in voteline_candidates:
if candidate_id not in self.candidates:
# Use the ID as both the key and the name when no explicit definition exists
self.candidates[candidate_id] = candidate_id
return {
"candidates": self.candidates,
"metadata": {"ballotcount": self.ballotcount, **self.metadata},
"votelines": sorted(self.votelines, key=lambda x: x["qty"], reverse=True)
}
def get_test_filenames(allfiles=False):
from os import listdir
from os.path import join
fnarray = []
if allfiles:
for f in listdir('testfiles'):
if f.endswith('abif'):
fnarray.append(join('testfiles', f))
fnarray.sort()
else:
fnarray = ABIF_WORKING_TESTS
return fnarray
def convert_abif_file_to_jabmod(abif_filename):
with open(abif_filename, 'r', encoding='utf-8') as f:
abif_str = f.read()
transformer = ABIFtoJabmodTransformer()
parser = Lark(ABIF_GRAMMAR_STR, parser="lalr", transformer=transformer)
parser.parse(abif_str)
return transformer.get_jabmod()
def analyze_file(abif_filename, verbose):
jabmod = convert_abif_file_to_jabmod(abif_filename)
outstr = ""
if verbose:
outstr += f"====================================================================\n"
outstr += f"Analysis for {abif_filename}:\n"
outstr += f" Ballot count: {jabmod['metadata']['ballotcount']}\n"
outstr += f" Candidates:\n"
for candkey, candval in jabmod['candidates'].items():
outstr += f" * {candkey}: {candval}\n"
else:
outstr += f"{abif_filename} ballot count: {jabmod['metadata']['ballotcount']}\n"
return outstr
def main():
""" Test function for running abif.py """
parser = argparse.ArgumentParser(description=__doc__.splitlines()[1])
parser.add_argument('--all-tests', help='get all tests from testfiles dir',
action="store_true")
parser.add_argument('-j', '--jabmod',
help='print JSON ABIF model (jabmod) for given ABIF',
action="store_true")
parser.add_argument('-v', '--verbose',
help='print long output',
action="store_true")
parser.add_argument('files', help='optional list of files to test',
nargs='*', default=None)
args = parser.parse_args()
if (args.files):
fnarray = args.files
else:
parser.print_usage()
fnarray = get_test_filenames(args.all_tests)
for filename in fnarray:
if args.jabmod:
jabmod = convert_abif_file_to_jabmod(filename)
print(json.dumps(jabmod, indent=4))
else:
print(analyze_file(filename, args.verbose), end="")
if args.verbose:
print(FOOTER)
if __name__ == "__main__":
# execute only if run as a script
main()