-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (34 loc) · 1.04 KB
/
main.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
from pathlib import Path
from urllib.parse import urlparse
import click
@click.command()
@click.argument("problem")
def cli(problem: str) -> None:
slug = parse_slug(problem)
create_templates(slug)
def exists(path: Path) -> None:
if path.exists():
print(f"{path} already exists")
exit(1)
def parse_slug(problem: str) -> str:
if problem.startswith("https"):
url = urlparse(problem)
segments = url.path.split("/")
assert not segments[0] and segments[1] == "problems", f"invalid {url=}"
return segments[2]
else:
return problem
def create_templates(name: str) -> None:
filename = name.replace("-", "_")
if filename[0].isnumeric():
filename = f"_{filename}"
src_file = Path("src", f"{filename}.py")
test_file = Path("tests", f"test_{filename}.py")
exists(src_file)
exists(test_file)
src_file.open("w").close()
test_file.open("w").close()
print(f"touch {src_file}")
print(f"touch {test_file}")
if __name__ == "__main__":
cli()