forked from goyaljiiiiii/100-python-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
89 lines (75 loc) · 2.48 KB
/
cli.py
File metadata and controls
89 lines (75 loc) · 2.48 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
#!/usr/bin/env python3
import os
import re
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent
PROJECTS_DIR = ROOT / "projects"
PROJECTS_INDEX = ROOT / "PROJECTS.md"
def _load_index_names():
names = {}
if not PROJECTS_INDEX.exists():
return names
for line in PROJECTS_INDEX.read_text(encoding="utf-8").splitlines():
if not line.strip().startswith("|"):
continue
parts = [p.strip() for p in line.strip("|").split("|")]
if len(parts) < 4 or parts[0].lower() == "#":
continue
num = parts[0]
name = parts[1]
path = parts[2].strip("`")
names[num] = {"name": name, "path": path}
return names
def _discover_projects():
projects = []
for p in sorted(PROJECTS_DIR.iterdir()):
if not p.is_dir() or p.name.startswith("_"):
continue
main_py = p / "main.py"
if not main_py.exists():
continue
# Numeric folder like 001 or 012
m = re.match(r"^(\d+)", p.name)
if not m:
continue
num = m.group(1)
projects.append({"num": num, "dir": p, "main": main_py})
return projects
def _run_project(main_path: Path):
try:
subprocess.run([sys.executable, str(main_path)], check=False)
except KeyboardInterrupt:
print("\nProject interrupted. Returning to launcher...")
finally:
input("\nPress Enter to return to the launcher...")
def main():
names = _load_index_names()
projects = _discover_projects()
if not projects:
print("No projects found in projects/.")
return
while True:
print("\n=== 100 Python Projects Launcher ===")
print("Type a project number (001-100) or q to quit.")
choice = input("Run project: ").strip().lower()
if choice in {"q", "quit", "exit"}:
print("Goodbye!")
return
# normalize choice (allow 1 -> 001 if exists)
match = None
normalized = choice.zfill(3) if choice.isdigit() else choice
for proj in projects:
if normalized == proj["num"]:
match = proj
break
if not match:
print("Project not found. Try another number.")
continue
num = match["num"]
name = names.get(num, {}).get("name", f"Project {num}")
print(f"\nRunning {num} - {name}...\n")
_run_project(match["main"])
if __name__ == "__main__":
main()