-
Notifications
You must be signed in to change notification settings - Fork 1
/
abif.py
executable file
·212 lines (162 loc) · 5.52 KB
/
abif.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
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
#!/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 os
import re
from lark import Lark, Transformer, v_args
from lark.exceptions import UnexpectedToken, UnexpectedCharacters
ABIF_DIR = os.path.dirname(__file__)
ABIF_GRAMMAR_FILE = os.path.join(ABIF_DIR, 'abif.ebnf')
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/test014.abif',
'testfiles/test015.abif',
'testfiles/test016.abif',
'testfiles/test017.abif',
'testfiles/test018.abif'
]
class ABIF_Parser:
"""Class for parsing Aggregated Ballot Information Format (ABIF) files
"""
def __init__(self):
self.parser = self.get_parser()
return None
def get_parser(self):
with open(ABIF_GRAMMAR_FILE, "r") as f:
self.abif_grammar_bnf = f.read()
self.abif_parser = Lark(self.abif_grammar_bnf,
parser="earley",
ambiguity="resolve").parse
return self.abif_parser
class ABIF_File(Transformer):
"""File with ABIF-formatted ballots
"""
def __init__(self, filename=None, verbose=False):
""" Initialize ABIF class from file """
self.ballots = []
self.filename = filename
self.err = None
self.verbose = verbose
if self.filename == None:
raise(BaseException())
else:
self._load()
return None
def _load(self):
""" Load ABIF file into memory """
i = 0
with open(self.filename) as f:
for (i, line) in enumerate(f):
self.ballots.append(line)
return self.filename
def transform(self):
pobj = self.parseobj
return super().transform(pobj)
def count(self):
""" Return the number of ballots represented by abif file """
# As of 2021-07-19, this ensures that the file parses, and
# then does a very crude regexp-based count of the ballot
# bundles.
# Try running lark-based parser
self.parse()
count_retval = 0
for (i, line) in enumerate(self.ballots):
match = re.search(r'^(\d+)\s*[:\*]', line)
if match:
thiscount = int(match.group(1))
count_retval += thiscount
self.ballotqty = count_retval
return self.ballotqty
def _read_file(self):
afilehandle = open(self.filename, 'r')
self.file_as_string = afilehandle.read()
afilehandle.close()
def _get_error_string(self):
return self.err
def parse(self):
""" Parse file using Lark parser, returning output as text blob
"""
self._read_file()
lark_parser = abif.ABIF_Parser().get_parser()
outstr = ""
try:
self.parseobj = lark_parser(self.file_as_string)
except UnexpectedCharacters as err:
self.err = str(err)
print("ERROR (UnexpectedCharacters): " + self.filename)
print(self.err)
if(self.verbose):
print("================")
print("FILE: " + self.filename)
print("---------------")
print(self.file_as_string)
raise
except:
print("ERROR-GACK: " + self.filename)
if(self.verbose):
print(self.file_as_string)
raise
outstr += "================\n"
outstr += "FILE: " + self.filename + "\n"
outstr += "---------------\n"
outstr += self.file_as_string
if(self.parseobj):
outstr += "================\n"
outstr += "PARSEOUT:\n"
outstr += "---------------\n"
outstr += str(self.parseobj.pretty()) + "\n"
outstr += "---------------\n"
return outstr
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 analyze_file(afilename, verbose):
obj = abif.ABIF_File(afilename, verbose=verbose)
if(verbose):
outstr = obj.parse()
else:
outstr = ""
outstr += obj.filename + " -- Count: "
outstr += str(obj.count())
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('-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:
print(analyze_file(filename, args.verbose))
if __name__ == "__main__":
# execute only if run as a script
main()