Repository navigation
Expand file tree
/
Copy pathupdate_readme.py
More file actions
136 lines (110 loc) · 4.18 KB
/
Copy pathupdate_readme.py
File metadata and controls
136 lines (110 loc) · 4.18 KB
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
"""
Scans main/*.py, extracts problem metadata from docstrings,
and regenerates the tracker section in README.md.
"""
import glob
import os
import re
from datetime import date
MAIN_DIR = os.path.join(os.path.dirname(__file__), "main")
def get_github_base():
import subprocess
try:
remote = subprocess.check_output(
["git", "remote", "get-url", "origin"],
cwd=os.path.dirname(__file__),
stderr=subprocess.DEVNULL,
).decode().strip()
# Handle both HTTPS and SSH formats:
# https://github.com/user/repo.git
# git@github.com:user/repo.git
remote = remote.removesuffix(".git")
if remote.startswith("git@"):
remote = re.sub(r"git@(.+?):", r"https://\1/", remote)
return f"{remote}/blob/main/main"
except Exception:
return "https://github.com"
GITHUB_BASE = get_github_base()
README_PATH = os.path.join(os.path.dirname(__file__), "README.md")
START_MARKER = "<!-- TRACKER_START -->"
END_MARKER = "<!-- TRACKER_END -->"
DIFF_ORDER = ["Easy", "Medium", "Hard"]
DIFF_COLOR = {"Easy": "🟢", "Medium": "🟡", "Hard": "🔴"}
DIFF_BADGE = {"Easy": "4c8b5e", "Medium": "8b7c4c", "Hard": "8b4c4c"}
def parse_problem(path):
with open(path, encoding="utf-8") as f:
content = f.read()
m = re.search(r'Problem:\s*(\d+)\s*-\s*(.+)', content)
d = re.search(r'Difficulty:\s*(\w+)', content)
l = re.search(r'Link:\s*\n(https?://\S+)', content)
if not m:
return None
filename = os.path.basename(path)
return {
"number": int(m.group(1)),
"title": m.group(2).strip(),
"difficulty": d.group(1).strip() if d else "Unknown",
"link": l.group(1).strip() if l else "",
"impl": f"{GITHUB_BASE}/{filename}",
}
def badge(diff, count):
color = DIFF_BADGE[diff]
label = f"{diff}%20%E2%80%93%20{count}"
return f""
def build_tracker(problems):
problems.sort(key=lambda p: p["number"])
total = len(problems)
counts = {d: sum(1 for p in problems if p["difficulty"] == d) for d in DIFF_ORDER}
today = date.today().strftime("%B %d, %Y")
badges = " ".join(badge(d, counts[d]) for d in DIFF_ORDER)
total_badge = f""
lines = [
"## 📊 Progress tracker",
"",
f"{total_badge} {badges}",
"",
f"_last updated {today}_",
"",
]
# One collapsible section per difficulty
for diff in DIFF_ORDER:
group = [p for p in problems if p["difficulty"] == diff]
if not group:
continue
emoji = DIFF_COLOR[diff]
lines.append(f"<details>")
lines.append(f"<summary><b>{emoji} {diff} ({len(group)})</b></summary>")
lines.append("")
lines.append("| # | Title | Solution |")
lines.append("|--:|-------|:--------:|")
for p in group:
title_cell = f"[{p['title']}]({p['link']})" if p["link"] else p["title"]
impl_cell = f"[`code`]({p['impl']})"
lines.append(f"| {p['number']} | {title_cell} | {impl_cell} |")
lines.append("")
lines.append("</details>")
lines.append("")
return "\n".join(lines)
def update_readme(tracker_block):
with open(README_PATH, encoding="utf-8") as f:
content = f.read()
new_section = f"{START_MARKER}\n{tracker_block}{END_MARKER}"
if START_MARKER in content and END_MARKER in content:
content = re.sub(
re.escape(START_MARKER) + r".*?" + re.escape(END_MARKER),
new_section,
content,
flags=re.DOTALL,
)
else:
content = content.rstrip("\n") + "\n\n" + new_section + "\n"
with open(README_PATH, "w", encoding="utf-8") as f:
f.write(content)
def main():
paths = glob.glob(os.path.join(MAIN_DIR, "*.py"))
problems = [p for p in (parse_problem(path) for path in paths) if p]
tracker = build_tracker(problems)
update_readme(tracker)
print(f"README updated — {len(problems)} problem(s) tracked.")
if __name__ == "__main__":
main()