-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminders.py
executable file
·102 lines (80 loc) · 2.6 KB
/
reminders.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
#!/usr/bin/env python
"""
Trigger notifications for reminders.
"""
__author__ = "Daniel Souza <[email protected]>"
import os
import yaml
import subprocess
import datetime
from pathlib import Path
from dateutil.relativedelta import *
CONFIG_FILE = Path(os.getenv("XDG_CONFIG_HOME"), "reminders/config.yaml")
TODAY = datetime.date.today()
def load_config() -> dict:
with open(CONFIG_FILE, 'r') as config:
return yaml.safe_load(config)
def create_config() -> None:
config_dir = os.path.dirname(CONFIG_FILE)
if not os.path.exists(config_dir):
os.makedirs(config_dir)
reminders_file = Path(config_dir, "reminders.yaml")
default_config = f'''
paths:
- \"{reminders_file}"
'''
with open(CONFIG_FILE, 'w') as f:
f.write(default_config)
default_reminders = f'''
- name: "Example Reminder"\n
desc: "This is an example. Edit configuration at {config_dir}."
next: {TODAY.strftime("%Y-%m-%d")}
last:
freq:
'''
with open(reminders_file, 'w') as f:
f.write(default_reminders)
def read_reminders(file_path: str) -> list:
with open(file_path, 'r') as file:
reminders = yaml.safe_load(file)
return reminders
def send_notification(summary: str, body: str) -> None:
subprocess.run(['notify-send', summary, body])
def check_reminders(reminders: list) -> None:
for reminder in reminders:
if reminder.get('next'):
next_date = reminder['next']
elif reminder.get('last'):
last_date = reminder['last']
next_date = calc_next_date(last_date, reminder['freq'])
else:
continue
if next_date <= TODAY:
summary = reminder['name']
if reminder.get('desc'):
body = f"{reminder['desc']} [{next_date}]"
else:
body = str(next_date)
send_notification(summary, body)
def calc_next_date(last_date: datetime, freq: str) -> datetime:
freq_unit = freq[-1]
freq_value = int(freq[0:-1])
if freq_unit == "d":
delta = relativedelta(days=+freq_value)
elif freq_unit == "w":
delta = relativedelta(weeks=+freq_value)
elif freq_unit == "m":
delta = relativedelta(months=+freq_value)
elif freq_unit == "y":
delta = relativedelta(years=+freq_value)
next_date = last_date + delta
return next_date
def main():
if not os.path.isfile(CONFIG_FILE):
create_config()
config = load_config()
for path in config['paths']:
reminders = read_reminders(os.path.expanduser(path))
check_reminders(reminders)
if __name__ == "__main__":
main()