-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.py
197 lines (154 loc) · 4.92 KB
/
build.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
import re
import json
from pathlib import Path
from builder import *
# Functions
def persian (text):
text = str(text)
for x, y in zip("1234567890", "۱۲۳۴۵۶۷۸۹۰"):
text = text.replace(x, y)
return text
# Metadata
with open("metadata.json", encoding = "utf8") as handler:
metadata = json.load(handler)
series = {
item["id"]: item
for item in metadata["series"]
}
root = Path("problems")
root.mkdir(parents = True, exist_ok = True)
# Problems
class Problem:
def __init__ (self, path):
if isinstance(path, Path):
self.path = path
else:
self.path = Path(path)
if not self.path.exists():
raise FileNotFoundError(path)
self.entries = {}
def parse (self):
with path.open("r", encoding = "utf8") as handler:
content = handler.read()
self.entries = re.findall(r'\[\_metadata\_\:(.*?)\]\:\-\s+\"(.*?)\"', content)
self.entries = dict(self.entries)
return self.entries
def rename (self):
if not self.entries:
self.parse()
name = entries["id"] + ".md"
self.path.rename(path.parent / name)
self.path = path.parent / name
paths = sorted(root.glob("*.md"))
problems = {}
for path in paths:
if path.name == "README.md":
continue
problem = Problem(path)
entries = problem.parse()
if entries["series"] not in problems:
problems[entries["series"]] = []
problems[entries["series"]].append(entries)
problem.rename()
# README.md
LEVELS = {
"easy": "ساده",
"medium": "متوسط",
"hard": "سخت"
}
output = ""
output += Heading("بانک سؤالات").render() + "\n\n"
output += Heading("بررسی اجمالی", 2).render() + "\n\n"
table = Table()
table.add_row(
[
"شماره",
"نوع",
"عنوان",
"ساده",
"متوسط",
"سخت",
"مجموع",
""
],
header = True
)
for i, (id, data) in enumerate(series.items()):
table.add_row(
[
persian(i + 1),
"سری" if data["type"] == "series" else "بسته جبرانی",
data["title"]["fa"],
persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "easy"]))),
persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "medium"]))),
persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "hard"]))),
persian(str(len(problems.get(data["id"], [])))),
Link("مشاهده", "#" + data["id"]).render()
]
)
output += table.render()
output += "\n\n"
for id, data in series.items():
mode = "سری" if data["type"] == "series" else "بسته جبرانی"
problems_count = sum(
len(y) for x, y in problems.items()
)
output += Heading(
persian(
mode + " " + str(data["number"]) + ": " + data["title"]["fa"]
),
2,
{
"id": data["id"]
}
).render()
output += "\n\n"
prbs = problems.get(data["id"], [])
if prbs:
easy = persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "easy"])))
medium = persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "medium"])))
hard = persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "hard"])))
output += f"برای این {mode}، {easy} سؤال ساده، {medium} سؤال متوسط و {hard} سؤال سخت و مجموعاً " + persian(len(prbs)) + f" سؤال طرح شده است."
output += "\n\n"
table = Table()
table.add_row(
[
"شماره",
"عنوان",
"سطح",
"طراح",
""
],
header = True
)
def get_level(level):
if level == "easy":
return 0
if level == "medium":
return 1
return 2
prbs = sorted(
sorted(
sorted(
prbs,
key = lambda x: x["id"]
),
key = lambda x: x["author"]
),
key = lambda x: get_level(x["level"])
)
for i, entries in enumerate(prbs):
table.add_row(
[
persian(i + 1),
entries["title"],
LEVELS[entries["level"]],
entries["author"],
Link("مشاهده", entries["id"] + ".md").render()
]
)
output += table.render()
else:
output += f"تا حالا هیچ سؤالی برای این {mode} طرح نشده است."
with (root / "README.md").open("w", encoding = "utf-8") as handler:
handler.write(output)