-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremind
executable file
·239 lines (208 loc) · 8.49 KB
/
remind
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python3
# Copyright 2006-2024 Michael Cuffaro
#
# This file is part of mccal.
#
# mccal is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# mccal is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with mccal. If not, see <http://www.gnu.org/licenses/>.
import argparse
import re
import subprocess
import sys
import time
from datetime import datetime, timedelta
global answer
def process_reminder_ui(raw_reminder_text, event_id, calfile):
# The import is local to the function here so that python will not complain that wx is not
# installed (in the case that it isn't, that is) when remind is running in text mode.
import wx
def on_dismiss(_event):
# Mark the event as processed in the calendar file:
with open(calfile, mode='a') as c:
proc_date = datetime.today()
print(f'PROCESSED ID:{event_id} {proc_date.year}-{proc_date.month}-{proc_date.day} '
f'{proc_date.hour}:{proc_date.minute} "{raw_reminder_text}"', file=c)
sys.exit(0)
def on_answer(_event):
on_snooze(_event)
def on_snooze(_event):
m = re.fullmatch(r"([\d]+)([mhd])?", answer.GetValue())
if not m:
wx.MessageDialog(
None,
f"Invalid snooze value: '{answer.GetValue()}'. Please use <num>[m|h|d] format.",
).ShowModal()
else:
snooze = int(m.groups()[0])
if snooze < 1:
wx.MessageDialog(
None,
"Sorry but you can't snooze for less than 1 minute. Please try again."
).ShowModal()
else:
time_modifier = m.groups()[1] if len(m.groups()) > 1 else "m"
if time_modifier == "h":
snooze *= 3600
elif time_modifier == "d":
snooze *= 86400
else:
snooze *= 60
# Determine the processed time for the event.
proc_time = datetime.today()
# The alarm time is the processed time moved forward by the amount indicated by `snooze`:
alarm_time = proc_time + timedelta(seconds=snooze)
# Generate a new event id for the snooze event that we will be recording:
new_event_id = time.time()
with open(calfile, mode='a') as c:
# Mark the original event as fully processed:
print(f'PROCESSED ID:{event_id} {proc_time.year}-{proc_time.month}-{proc_time.day} '
f'{proc_time.hour}:{proc_time.minute} "{raw_reminder_text}"', file=c)
# Add the new snooze event to the calendar file:
print(f'SNOOZE ID:{new_event_id} {alarm_time.year}-{alarm_time.month}-{alarm_time.day} '
f'{alarm_time.hour}:{alarm_time.minute} "{raw_reminder_text}"',
file=c)
sys.exit(0)
app = wx.App()
raw_reminder_text = raw_reminder_text.strip('\"')
reminder_time, reminder_message = split_reminder(raw_reminder_text)
frame = wx.Frame(None, title='mccal reminder')
main_panel = wx.Panel(frame)
content_panel = wx.Panel(main_panel)
timestring_panel = wx.Panel(content_panel)
message_panel = wx.Panel(content_panel)
question_panel = wx.Panel(content_panel)
answer_panel = wx.Panel(content_panel)
button_panel = wx.Panel(content_panel)
timestring = reminder_time.strftime('%A, %B %d %Y %I:%M%p')
timestring = wx.StaticText(timestring_panel, -1, label=f"{timestring}")
timestring_box = wx.BoxSizer(wx.HORIZONTAL)
timestring_box.Add(timestring)
timestring_panel.SetSizerAndFit(timestring_box)
message = wx.StaticText(message_panel, -1, label=f"{reminder_message}")
message_box = wx.BoxSizer(wx.HORIZONTAL)
message_box.Add(message)
message_panel.SetSizerAndFit(message_box)
question = wx.StaticText(
question_panel,
-1,
label="Either click Dismiss to confirm or enter the amount\n"
"of time you would like to snooze this alarm for\n"
"(<num>[m|h|d])."
)
question_box = wx.BoxSizer(wx.HORIZONTAL)
question_box.Add(question)
question_panel.SetSizerAndFit(question_box)
answer = wx.TextCtrl(
answer_panel,
value="",
style=wx.TE_PROCESS_ENTER,
size=(max(timestring.GetSize().GetWidth(),
message.GetSize().GetWidth(),
question.GetSize().GetWidth()),
30)
)
answer.Bind(wx.EVT_TEXT_ENTER, on_answer, answer)
answer_box = wx.BoxSizer(wx.HORIZONTAL)
answer_box.Add(answer)
answer_panel.SetSizerAndFit(answer_box)
snooze_btn = wx.Button(button_panel, label="Snooze")
dismiss_btn = wx.Button(button_panel, label="Dismiss")
snooze_btn.Bind(wx.EVT_BUTTON, on_snooze, snooze_btn)
dismiss_btn.Bind(wx.EVT_BUTTON, on_dismiss, dismiss_btn)
button_box = wx.BoxSizer(wx.HORIZONTAL)
button_box.Add(snooze_btn)
button_box.Add(dismiss_btn)
button_panel.SetSizerAndFit(button_box)
content_box = wx.BoxSizer(wx.VERTICAL)
content_box.AddSpacer(12)
content_box.Add(timestring_panel, 0, wx.ALL | wx.ALIGN_LEFT)
content_box.Add(message_panel, 0, wx.ALL | wx.ALIGN_LEFT)
content_box.AddSpacer(12)
content_box.Add(question_panel, 0, wx.ALL | wx.ALIGN_LEFT)
content_box.AddSpacer(12)
content_box.Add(answer_panel, 0, wx.ALL | wx.ALIGN_LEFT)
content_box.AddSpacer(12)
content_box.Add(button_panel, 0, wx.ALL | wx.ALIGN_RIGHT)
content_box.AddSpacer(8)
content_panel.SetSizerAndFit(content_box)
main_box = wx.BoxSizer(wx.HORIZONTAL)
main_box.AddSpacer(12)
main_box.Add(content_panel, 0, wx.ALL | wx.ALIGN_CENTER)
main_box.AddSpacer(12)
main_panel.SetSizerAndFit(main_box)
frame.Fit()
frame.SetMinSize(frame.GetBestSize())
frame.Center()
frame.Show()
app.MainLoop()
app.Destroy()
def split_reminder(raw_reminder_text):
matches = re.match(r'^(\d{1,4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}) (.*)', raw_reminder_text)
if not matches:
print(f"Could not parse reminder text: '{raw_reminder_text}'")
sys.exit(1)
matches = matches.groups()
reminder_time = datetime(
year=int(matches[0]),
month=int(matches[1]),
day=int(matches[2]),
hour=int(matches[3]),
minute=int(matches[4])
)
return reminder_time, matches[5]
def main(calfile, event_id, raw_reminder_text, text_mode, recipient):
# Mark the event as pending by writing it to calfile:
with open(calfile, mode='a') as c:
pend_time = datetime.today()
print(f'PENDING ID:{event_id} {pend_time.year}-{pend_time.month}-{pend_time.day} '
f'{pend_time.hour}:{pend_time.minute} "{raw_reminder_text}"', file=c)
if recipient:
reminder_time, reminder_message = split_reminder(raw_reminder_text)
reminder_time = reminder_time.strftime('%A, %B %d %Y %I:%M%p')
result = subprocess.run(
f'echo "{reminder_time}\n{reminder_message}" |'
f' mail -s "Reminder for {reminder_time}" {recipient}',
shell=True,
capture_output=True
)
if result.returncode != 0:
print(
"Unable to send an email to '{}': {}".format(recipient, result.stderr.decode().strip('\n')),
file=sys.stderr
)
if text_mode:
# Output the raw_reminder_text to STDOUT, and mark the event as processed in the caledar file:
print(raw_reminder_text)
with open(calfile, mode='a') as c:
proc_time = datetime.today()
print(f'PROCESSED ID:{event_id} {proc_time.year}-{proc_time.month}-{proc_time.day} '
f'{proc_time.hour}:{proc_time.minute} "{raw_reminder_text}"', file=c)
else:
process_reminder_ui(raw_reminder_text, event_id, calfile)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Simple reminder calendar -- remind")
parser.add_argument('CALENDAR', help='a calendar file')
parser.add_argument('ID', help='the event ID associated with this reminder')
parser.add_argument('REMINDER_TEXT', help='the text associated with this reminder')
parser.add_argument(
'-t', '--text_mode', action='store_true',
help='after processing the reminder, output the reminder text to STDOUT '
'and exit, without providing the user with an option to snooze'
)
parser.add_argument(
'-m', '--mail', metavar='ADDR',
help="use the program 'mail' to send the reminder text as an email to the given email "
"address in addition to notifying the user that the reminder is due"
)
args = parser.parse_args()
main(args.CALENDAR, args.ID, args.REMINDER_TEXT, args.text_mode, args.mail)