-
Notifications
You must be signed in to change notification settings - Fork 3
/
tasks.py
74 lines (50 loc) · 1.85 KB
/
tasks.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
import frontmatter
import jinja2
import maya
import typer
import yaml
from datetime import date, datetime
from math import ceil
from pathlib import Path
ACCOMPLISHMENT_TEMPLATE = Path("templates", "weekly.md")
OUTPUT_FOLDER = Path("accomplishments", "{year}")
README_TEMPLATE = Path("templates", "README.md")
TASKS_DATA = Path("data", "tasks.yml")
app = typer.Typer()
@app.command()
def accomplishment(date: str = "now", overwrite: bool = False):
parsed_date = maya.when(date, timezone="US/Central").datetime(naive=True)
day_of_month = parsed_date.day
week_number = (day_of_month - 1) // 7 + 1
output_filename = f"{parsed_date.year}-{parsed_date.month:02}-week{week_number}.md"
output_filename = Path(
str(OUTPUT_FOLDER.joinpath(output_filename)).format(year=parsed_date.year)
)
typer.echo(str(output_filename))
if not output_filename.parent.exists():
output_filename.parent.mkdir()
if TASKS_DATA.exists():
data = yaml.load(TASKS_DATA.read_text(), Loader=yaml.FullLoader)
else:
data = dict()
context_data = data.copy()
context_data["date"] = parsed_date
# Handle Accomplishments
post = frontmatter.loads(ACCOMPLISHMENT_TEMPLATE.read_text())
t = jinja2.Template(post.content)
contents = t.render(context_data)
post["date"] = parsed_date
post.content = contents
if not output_filename.exists() or overwrite:
output_filename.write_text(frontmatter.dumps(post))
# Handle README
output_filename = Path("README.md")
post = frontmatter.loads(README_TEMPLATE.read_text())
t = jinja2.Template(post.content)
contents = t.render(context_data)
post["date"] = parsed_date
post.content = contents
# output_filename.write_text(frontmatter.dumps(post))
output_filename.write_text(post.content)
if __name__ == "__main__":
app()