-
Notifications
You must be signed in to change notification settings - Fork 4
/
build_decks.py
117 lines (101 loc) · 4.25 KB
/
build_decks.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
# Copyright: Daveight and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import errno
import os
from unidecode import unidecode
from argparse import ArgumentParser
def encode_csv(text: str):
"""
Encodes text for CSV format
:param text: target text
:return: encoded text
"""
text = text.replace('"', '""')
return text
def get_file_content(path: str):
"""
Reads file content by path
:param path: file location path
:return: file text content
"""
f = None
try:
with open(path, "r") as f:
return f.read()
except OSError:
return None
finally:
if f is not None:
f.close()
def replace_non_utf_chars(description: str) -> str:
# Replace non-UTF-8 characters with their closest UTF-8 equivalent
clean_description = unidecode(description)
return clean_description
SOLUTION_SECTIONS = [['header', ''], ['java', 'Java'], ['cpp', 'C++'], ['js', 'JavaScript'], ['python', 'Python']]
parser = ArgumentParser()
parser.add_argument('--debug', dest='debug', action='store_true')
args = parser.parse_args()
for deck_category in os.listdir('src'):
for deck_subcategory in os.listdir('src/' + deck_category):
out = ''
deck_name = deck_category + '/' + deck_subcategory
if deck_name.startswith('.') or deck_name in ['template']:
continue
for name in sorted(os.listdir(f'src/{deck_name}/test_cases')):
try:
if not name.endswith('.tsv'):
continue
file = open(f'src/{deck_name}/test_cases/' + name, 'r')
lines = file.readlines()
name = name.replace('.tsv', '')
description = get_file_content(f'src/{deck_name}/descriptions/' + name)
description = replace_non_utf_chars(description)
if description is None:
print('can\'t find description for ' + name)
continue
title = get_file_content(f'src/{deck_name}/titles/' + name)
if title is None:
print('can\'t find title for ' + name)
continue
func_name = get_file_content(f'src/{deck_name}/fn_names/' + name)
if not func_name:
print('can\'t find func name for ' + name)
continue
solution = ''
for section in SOLUTION_SECTIONS:
txt = get_file_content(f'src/{deck_name}/solutions/{section[0]}/{name}')
if txt is None:
# print(f'can\'t find {section[0]} solution for {name}')
continue
if section[1]:
solution += '### ' + section[1] + '\n'
solution += txt + '\n'
out += '"' + encode_csv(title.strip()) + '"' + '\t'
out += '"' + encode_csv(description.strip()) + '"' + '\t'
out += '"' + func_name.strip() + '"' + '\t'
out += '"' + encode_csv(solution.strip()) + '"' + '\t'
out += '"'
for i, line in enumerate(lines):
items = line.strip().split('\t')
out += encode_csv(';'.join(items))
out += '\n'
out += '"'
out += '\n'
if args.debug:
debug_csv_name = f'debug/{deck_category}-{deck_subcategory}/{name}.csv'
if not os.path.exists(os.path.dirname(debug_csv_name)):
try:
os.makedirs(os.path.dirname(debug_csv_name))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
result = open(debug_csv_name, "w+")
result.write(out)
result.close()
out = ''
if not args.debug:
result = open(f'dist/{deck_category}-{deck_subcategory}.csv', 'w+')
result.write(out)
result.close()
except FileNotFoundError:
pass