This repository has been archived by the owner on Oct 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate.py
116 lines (71 loc) · 2.69 KB
/
generate.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
import sys, os, json
name = sys.argv[1]
try:
overwrite = int(sys.argv[2])
except:
overwrite = 0
cfg_path = os.path.join(name, "config.json")
cfg = json.loads(open(cfg_path).read())
categories = cfg["categories"]
for c in categories:
if not os.path.exists(os.path.join(name, c)):
os.mkdir(os.path.join(name, c))
challenges = cfg["challenges"]
columns = ["Challenge Name"] + list(map(lambda x: x.capitalize(), cfg["columns"]))
notes = "\n\n".join([f"## {key.capitalize()}" for key in cfg["index"]["notes"]])
challenges_header = "| " + " | ".join(columns) + " |"
index_template = f"""
# {name.strip('/')}
{notes}
# Challenges
{challenges_header}
{"|:-:"*len(columns) + "|"}
"""
namesCategory = {i:[] for i in cfg["categories"]}
for key, val in challenges.items():
if not len(namesCategory[val["category"]]):
namesCategory[val["category"]].append(key)
continue
comparisonGroup = namesCategory[val["category"]]
for i in range(len(comparisonGroup)):
compareTo = comparisonGroup[i]
try:
if int(challenges[compareTo]["points"]) > int(val["points"]):
namesCategory[val["category"]].insert(i, key)
break
except KeyError:
namesCategory[val["category"]].insert(i, key)
break
else:
namesCategory[val["category"]].append(key)
categories = sorted(cfg["categories"])
for cat, challs in namesCategory.items():
toAdd = ""
for chall in challs:
for col in columns:
if col == "Challenge Name":
toAdd += f"|[{chall}]({cat+'/'+chall.replace(' ', '-').replace('/', '-')+'.md'})"
continue
toAdd += "|" + challenges[chall][col.lower()]
toAdd += "|\n"
index_template += toAdd
for chall, amt in challenges.items():
chall = chall.replace(" ", "-").replace("/", "-")
fname = os.path.join(name, amt['category'].lower(), chall+".md")
if overwrite == 1:
with open(fname, "w") as f:
f.write(f"# {chall}\n{amt['category'].capitalize()}, {amt['points']}")
if overwrite == 2:
with open(fname, "w") as f:
f.write(f"# {chall}\n{amt['category'].capitalize()}")
else:
if not os.path.exists(fname):
with open(fname, "w") as f:
f.write(f"# {chall}\n{amt['category'].capitalize()}, {amt['points']}")
if overwrite:
with open(os.path.join(name, "README.md"), "w") as f:
f.write(index_template)
else:
if not os.path.exists(os.path.join(name, "README.md")):
with open(os.path.join(name, "README.md"), "w") as f:
f.write(index_template)