forked from mblayman/mattlayman.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checklist.py
77 lines (57 loc) · 2.08 KB
/
checklist.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
import json
from pathlib import Path
import typer
from dotenv import load_dotenv
from tools import constants
from tools.dev_formatter_task import DEVFormatterTask
TASKS = {"dev": DEVFormatterTask}
def main(article_path: Path = typer.Argument(..., exists=True)):
"""Run an article through the checklist."""
if article_path.suffix != ".md":
typer.echo("The provided article path is not a Markdown file.")
raise typer.Exit(code=1)
load_dotenv()
checklist = Checklist(article_path)
checklist.load(TASKS.keys())
for item in checklist:
if checklist.is_complete(item):
typer.echo(f"Skipping complete item: {item}")
continue
task = TASKS[item]()
response = typer.confirm(task.prompt, default=True)
if response:
typer.echo(task.start)
status = task.handle(article_path=article_path)
if status:
typer.echo("Task completed.")
checklist.complete(item)
checklist.write()
class Checklist:
"""A container to track which checklist items are processed."""
checklists_dir = constants.root / "checklists"
def __init__(self, article_path):
self.article_path = article_path
self.items = {}
def __iter__(self):
return iter(self.items.keys())
@property
def path(self):
article_path = str(
self.article_path.relative_to("content").with_suffix(".json")
)
article_path = article_path.replace("/", "_")
return self.checklists_dir / article_path
def load(self, checklist_items):
try:
with self.path.open() as f:
self.items = json.load(f)
except FileNotFoundError:
self.items = {item: False for item in checklist_items}
def is_complete(self, item):
return self.items[item]
def complete(self, item):
self.items[item] = True
def write(self):
"""Store the checklist for any future use."""
with self.path.open("w") as f:
json.dump(self.items, f, sort_keys=True, indent=2)