-
Notifications
You must be signed in to change notification settings - Fork 0
/
etfparse.py
131 lines (110 loc) Β· 4.12 KB
/
etfparse.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
#!/usr/bin/env python3
import io
import json
import argparse
__version__ = '1.2.0'
# Error codes
EXIT_INVALID_FILENAME: int = 1
EXIT_INSUFFICIENT_PERMISSIONS: int = 2
EXIT_INVALID_JSON_FORMAT: int = 3
EXIT_INVALID_AUTOTEST_FORMAT: int = 4
def add_options(parser: argparse.ArgumentParser) -> None:
"""
Adds following options to the argument parser
"""
parser.add_argument('files', nargs='*', default=['autotest2'],
help='Input the name(s) of your autotest file(s)',
type=argparse.FileType('r', encoding='UTF-8'))
parser.add_argument('--version', action='version',
version=f'%(prog)s v{__version__}')
def get_json_raw(filename: str) -> dict:
"""
Returns a dictionary object representing parsed JSON
for autotest file `filename`
:return: Dictionary representing parsed autotest file
"""
try:
with open(filename, 'r', encoding='UTF-8') as input_file:
json_raw = json.load(input_file)
return json_raw
except FileNotFoundError:
print(f'Error: File "{filename}" not found!')
exit(EXIT_INVALID_FILENAME)
except PermissionError:
print('Error: Unable to read file (Insufficient permissions)')
exit(EXIT_INSUFFICIENT_PERMISSIONS)
except json.JSONDecodeError:
print('Error: Unable to read file (Not in JSON format)')
exit(EXIT_INVALID_JSON_FORMAT)
def get_autotests(json_raw) -> list[str]:
"""
Returns all the autotests from `json_raw`
:return: List of strings representing autotest codes
"""
autotests: list[str] = []
try:
total_tests = len(json_raw['tests'])
for test_number in range(1, total_tests):
autotests.append(json_raw['tests'][test_number]['patch'][0]['code'])
return autotests
except KeyError:
print('Error: File not in expected autotest format')
exit(EXIT_INVALID_AUTOTEST_FORMAT)
def get_expected_outputs(json_raw) -> list[str]:
"""
Returns expected outputs for all autotests from `json_raw`
:return: List of strings representing expected outputs
"""
expected_outputs: list[str] = []
try:
total_tests = len(json_raw['tests'])
for test_number in range(1, total_tests):
expected_outputs.append(json_raw['tests'][test_number]['execute']['expect'][0])
return expected_outputs
except KeyError:
print('Error: File not in expected autotest format')
exit(EXIT_INVALID_AUTOTEST_FORMAT)
def get_homework_name(json_raw) -> str:
"""
Returns homework (assignment) name from `json_raw`
:return: Homework name
"""
try:
return json_raw['name']
except KeyError:
print('Error: File not in expected autotest format')
exit(EXIT_INVALID_AUTOTEST_FORMAT)
def parse_file(filename: str) -> None:
"""
Parses file `filename` and creates `filename-output.txt` with
all the autotests and outputs.
"""
json_raw = get_json_raw(filename)
autotests = get_autotests(json_raw)
expected_outputs = get_expected_outputs(json_raw)
homework_name = get_homework_name(json_raw)
try:
with open(filename + '-output.txt', 'w', encoding='UTF-8') as output_file:
output_file.write(homework_name + '\n\n')
current_test_number = 1
while len(autotests) != 0:
output_file.write(f'// AT {current_test_number}' + '\n')
output_file.write(autotests.pop() + '\n\n')
output_file.write(f'Expected: {expected_outputs.pop()}' + '\n\n\n')
current_test_number += 1
print('Success: ' + f'"{homework_name}"')
except PermissionError:
print('Error: Unable to create file (Insufficient permissions)')
exit(EXIT_INSUFFICIENT_PERMISSIONS)
def main() -> None:
parser = argparse.ArgumentParser(prog='etfparse')
add_options(parser)
args = parser.parse_args()
files = args.files
for file in files:
if isinstance(file, io.TextIOWrapper):
parse_file(file.name)
else:
parse_file(file)
if __name__ == '__main__':
main()