-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·114 lines (95 loc) · 4.78 KB
/
main.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
#!/usr/bin/env python3
import sys
import colorama
from modules.case.saver import case_file_name
from modules.command.commands.gen import gen_with_progress_bar, gen_without_progress_bar
from modules.command.commands.test_double import test_double_with_progress_bar, test_double_without_progress_bar
from modules.command.commands.test_single import test_single_with_progress_bar, test_single_without_progress_bar
from modules.command.parser import parse_command_line_argument
from modules.format.parser import parse_format
from modules.utility.colorizer import code
from modules.utility.exit_failure import exit_failure
from modules.utility.printer import error, hint, info, progress
from modules.variable.parser import parse_variable
def main() -> None:
colorama.init()
# check version
if (sys.version_info[0] < 3) or ((sys.version_info[0] == 3) and sys.version_info[1] < 9):
error('This program requires Python 3.9.0 (or higher) but you are using Python {}.{}.{}.'.format(
sys.version_info[0],
sys.version_info[1],
sys.version_info[2]
))
exit_failure()
args = parse_command_line_argument()
if args.verify:
info('You are in the verification mode.')
# parse variables & output format
using_tty = False
if args.input == '':
info('The input will be read from standard input.')
if sys.stdin.isatty():
using_tty = True
hint(f'In terminal, enter EOF manually (typically {code("Ctrl + D")}) '
'when the input is finished.')
hint('Use {} or {} argument to use file input.'.format(
code('--input'),
code('-i')
))
if (args.prefix == '') and (args.suffix == ''):
if args.cases == 1:
info(f'The output will be named {code(case_file_name(1, 1, "", ""))}.')
else:
info('The output will be named {}, {}, ....'.format(
code(case_file_name(1, args.cases, '', '')),
code(case_file_name(2, args.cases, '', '')),
))
hint('Specify prefix ({} or {}) and/or suffix ({} or {}) if this is not what you want.'.format(
code('--prefix'),
code('-p'),
code('--suffix'),
code('-s')
))
source = sys.stdin if args.input == '' else open(args.input, encoding='utf-8')
progress('Start parsing variables and output format.')
variables, override_statements, variable_inputs = parse_variable(using_tty, source, args.verify)
format, format_inputs = parse_format(using_tty, source, variables)
if using_tty:
print(flush=True)
if input('Do you want to save your input as a text file? [y/N] ').lower() in ['y', 'yes']:
with open(input('Enter file name: '), 'w') as f:
f.write(''.join(variable_inputs))
f.write(''.join(format_inputs))
progress('Variables and output format have been parsed successfully.')
# execute command
if args.subcommand == 'gen':
if args.no_progress_bar:
gen_without_progress_bar(args.cases, args.prefix, args.suffix,
args.verify, variables, override_statements, format)
else:
gen_with_progress_bar(args.cases, args.prefix, args.suffix,
args.verify, variables, override_statements, format)
elif args.subcommand == 'test' and len(args.programs) == 1:
if args.no_progress_bar:
test_single_without_progress_bar(args.cases, args.programs[0], args.time_limit,
args.prefix, args.suffix, args.verify,
variables, override_statements, format)
else:
test_single_with_progress_bar(args.cases, args.programs[0], args.time_limit,
args.prefix, args.suffix, args.verify,
variables, override_statements, format)
elif args.subcommand == 'test' and len(args.programs) == 2:
if args.no_progress_bar:
test_double_without_progress_bar(args.cases, args.programs[0], args.programs[1], args.time_limit,
args.prefix, args.suffix, args.verify,
variables, override_statements, format)
else:
test_double_with_progress_bar(args.cases, args.programs[0], args.programs[1], args.time_limit,
args.prefix, args.suffix, args.verify,
variables, override_statements, format)
else:
error(f'Too many (= {len(args.programs)}) programs are provided.')
exit_failure()
colorama.deinit()
if __name__ == '__main__':
main()