-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmake_year_template.py
More file actions
70 lines (54 loc) · 1.85 KB
/
make_year_template.py
File metadata and controls
70 lines (54 loc) · 1.85 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
"""Make a diary template for a given year."""
import argparse
import textwrap
from datetime import datetime, timedelta
from typing import Any
class Diary:
TEMPLATE = textwrap.dedent(
"""\
# Year of {year}
{weeks}
<!-- Modeline --> <!-- {{{{{{1 -->
<!-- markdownlint-disable-file -->
<!-- vim: sw=2 ts=2 tw=80 expandtab spell nonumber fdm=marker ai sts=2
-->
"""
)
def __init__(self, year) -> None:
self.year = year
def iter_weeks(self):
start = first_monday_of_year = {
d.weekday(): d
for d in [datetime(self.year, 1, 1) + timedelta(days=x) for x in range(7)]
}[0]
while start.year == first_monday_of_year.year:
yield "\n".join(
[
f"## Week of {start.strftime('%Y-%m-%d')} <!-- {{{{{{1 -->\n",
*[
f"### {d.strftime('%Y-%m-%d')} <!-- {{{{{{2 -->\n"
for d in ((start + timedelta(days=t)) for t in range(5))
],
"\n<!-- 1}}} -->\n",
]
)
start += timedelta(days=7)
def __str__(self) -> str:
return self.TEMPLATE.format(year=self.year, weeks="\n".join(self.iter_weeks()))
def valid_year(val: Any) -> int:
if not (1970 <= (val := int(val)) < 10_000):
raise ValueError(f"Invalid year {val}")
return val
def main():
parser = argparse.ArgumentParser(
description=__doc__,
epilog="""\
Remember to call `python.exe` from Git Bash (instead of `python`).
Redirect stdout to the new file.
""",
)
parser.add_argument("year", type=valid_year, help="year to make template for")
args = parser.parse_args()
print(Diary(args.year))
if __name__ == "__main__":
main()