-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsoner.py
executable file
·89 lines (64 loc) · 2.39 KB
/
jsoner.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
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
import yaml
import sys
import json
import os
import re
usage = """
Problem data json file generator from rime for WPCS2
usage:
$ ./jsoner.py [config yaml file]
Note that you need to run `rime test` before executing this script
"""
def exit_with_usage(error = None) :
print(usage)
if error is not None :
print('\nError: ' + error)
exit(-1)
def open_and_read_file(file_name) :
if not os.path.isfile(file_name) :
exit_with_usage("file " + file_name + " dose not exist!")
with open(file_name, "r", encoding="utf-8") as f:
return f.read()
def sanitize_title(title) :
pattern = r"^[a-zA-Z]:? ?"
matched = re.match(pattern, title)
if matched :
return title[matched.end():]
else :
return title
if __name__ == '__main__' :
argvs = sys.argv
if len(argvs) != 2 :
exit_with_usage()
infile_name = argvs[1]
data = yaml.load(open_and_read_file(infile_name))
for problem in data['problems'] :
dir_name = problem['dir_name']
# check rime-out exists
if not os.path.isdir(dir_name + "/rime-out") :
exit_with_usage(dir_name + "/rime-out directory does not exits ")
# read problem title
def _problem(title=None, **ignore) :
problem['title'] = sanitize_title(title)
def _atcoder_config(**ignore) :
return
exports = {'problem': _problem, 'atcoder_config': _atcoder_config}
exec(open_and_read_file(dir_name + "/PROBLEM"), exports, {})
# statement
problem['statement'] = open_and_read_file(dir_name + "/statement.md")
# dataset
for data_set in problem['data_sets'] :
test_dir = dir_name + "/rime-out/tests/" + data_set['label'].lower()
data_set['input'] = open_and_read_file(test_dir + ".in")
data_set['output'] = open_and_read_file(test_dir + ".diff")
del problem['dir_name']
# output to std
data_set_str = ["{} ({})".format(s['label'], s['score']) for s in problem['data_sets']]
print(problem['title'] + " : " + ", ".join(data_set_str))
# TODO not endwith .yml
outfile_name = infile_name.replace(".yml", ".json")
with open(outfile_name, "w", encoding="utf-8") as f :
json.dump(data, f, ensure_ascii=False, sort_keys=True)
print("\nSuccessfully output to " + outfile_name)