-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.py
208 lines (166 loc) · 5.8 KB
/
cli.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
obsi command line interface.
"""
import logging
from collections import defaultdict
from datetime import datetime, timedelta
from pathlib import Path
import click
from obsi.anki import generate_anki_deck
from obsi.markdown import (
render_day,
render_day_of_year,
render_index,
render_month,
render_note_list,
render_week,
render_year,
)
from obsi.ml import generate_tag_recommendations
from obsi.storage import (
Vault,
day_date_to_path,
day_date_to_week_path,
get_day_of_year_path,
get_month_path,
get_year_path,
)
DAY_GENERATION_PADDING = 10
WEEK_GENERATION_PADDING = 5
NOTES_PATH = "/notes/"
OUTPUT_PATH = "/output/"
STUB_LENGTH_THRESHOLD = 100
@click.group()
def cli():
"""
This is the obsi command line interface. Use it to work with your vault.
"""
print("obsi started")
@cli.command()
def run():
"""
Run all the functionality at once. Just do this if you're unsure, nothing can go wrong.
"""
update_calendar()
update_recommendations()
update_indexes()
update_stubs()
def update_calendar():
years = range(2000, 2030)
update_days()
update_days_of_year()
update_weeks()
update_years(years=years)
update_months(years=years)
def update_days_of_year():
# chose the last gap year (2020) to loop through all days in a gap year
first_day_in_gap_year = datetime(2020, 1, 1)
for i in range(366):
pseudo_date = first_day_in_gap_year + timedelta(days=i)
doy_path = Path(OUTPUT_PATH).joinpath(get_day_of_year_path(pseudo_date))
doy_path.parent.mkdir(exist_ok=True, parents=True)
with doy_path.open("w") as file:
file.write(render_day_of_year(pseudo_date.month, pseudo_date.day))
def update_years(years):
for year in years:
year_path = Path(OUTPUT_PATH).joinpath(get_year_path(year))
year_path.parent.mkdir(exist_ok=True, parents=True)
with year_path.open("w") as file:
file.write(render_year(year, get_year_uri=get_year_path))
def update_months(years):
for year in years:
for month in range(1, 13):
month_path = Path(OUTPUT_PATH).joinpath(get_month_path(year, month))
month_path.parent.mkdir(exist_ok=True, parents=True)
with month_path.open("w") as file:
file.write(render_month(year, month, get_month_path, get_year_path))
@cli.command()
def anki_deck():
"""
Generate an (updatable) anki deck for your notes.
"""
name = f"Obsi notes for {NOTES_PATH}"
vault = Vault(NOTES_PATH)
notes = list(vault.generate_notes())
generate_anki_deck(name, notes, out_file=Path(OUTPUT_PATH).joinpath("deck.apkg"))
def update_days(padding=DAY_GENERATION_PADDING):
"""
Generates all days for the given padding.
:param padding: generate n days behind and ahead of current date.
"""
for i in range(-padding, padding):
date = (datetime.now() + timedelta(days=i)).date()
content = render_day(date)
day_path = Path(OUTPUT_PATH).joinpath(day_date_to_path(date))
if not day_path.is_file():
day_path.parent.mkdir(parents=True, exist_ok=True)
with day_path.open("w") as file:
logging.info(f"generate {day_path}")
file.write(content)
else:
logging.warning(f"{day_path} already exists, skipping")
def update_weeks(padding=WEEK_GENERATION_PADDING):
"""
Generate weeks for the given padding.
:param padding: generate n weeks around current date.
"""
for i in range(-padding, padding):
date = (datetime.now() + timedelta(weeks=i)).date()
content = render_week(date)
week_path = Path(OUTPUT_PATH).joinpath(day_date_to_week_path(date))
if not week_path.is_file():
week_path.parent.mkdir(parents=True, exist_ok=True)
with week_path.open("w") as file:
logging.info(f"generate {week_path}")
file.write(content)
else:
logging.warning(f"{week_path} already exists, skipping")
def update_recommendations():
notes = list(get_vault().generate_notes())
for tag, notes_rec in generate_tag_recommendations(notes):
logging.info(f"generate recommendations for {tag}")
filename = f"recommendations-{tag_to_filepart(tag)}.md"
with Path(OUTPUT_PATH).joinpath(filename).open("w") as file:
file.write(render_note_list(tag, notes_rec))
def update_indexes():
for filename, content in generate_indexes():
path = Path(OUTPUT_PATH).joinpath(filename)
with path.open("w") as file:
logging.info(f"generate index {filename}")
file.write(content)
def generate_indexes(untagged_index=True):
notes_per_tag = defaultdict(set)
notes_untagged = []
for note in get_vault().generate_notes():
for tag in note.tags:
notes_per_tag[tag].add(note)
if not note.tags:
notes_untagged.append(note)
for tag, notes in notes_per_tag.items():
filename = f"index-{tag_to_filepart(tag)}.md"
yield filename, render_index(tag, notes)
if untagged_index:
yield "index-untagged.md", render_index("untagged notes", notes_untagged)
def update_stubs():
"""
This will generate a list of stubs (short notes).
"""
stubs = [
note
for note in get_vault().generate_notes()
if len(note.content) < STUB_LENGTH_THRESHOLD
]
with Path(OUTPUT_PATH).joinpath("stubs.md").open("w") as file:
file.write(render_note_list("Stubs", stubs))
def tag_to_filepart(tag):
"""
Use a tag as part of a filename.
:param tag:
:return:
"""
return tag.lower().replace("#", "")
def get_vault():
return Vault(NOTES_PATH)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
cli()