-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddappointments
executable file
·145 lines (129 loc) · 5.31 KB
/
addappointments
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
#!/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 sys
import time
from datetime import datetime, timedelta
from pathlib import Path
def save_appointment(calendar, appdate, remind, text, appt_id):
# Assigning a date implies a copy operation:
alarm = appdate
alarm = alarm - timedelta(minutes=int(remind))
if appt_id is None:
appt_id = time.time()
print(
f'ID:{appt_id} {alarm.year}-{alarm.month}-{alarm.day} {alarm.hour}:{alarm.minute} '
f'"{appdate.year}-{appdate.month}-{appdate.day} {appdate.hour}:{appdate.minute} {text}"',
file=calendar
)
def main(appointments, calendar, appt_id):
for appointment in appointments:
# Parse the next line:
appointment = appointment.strip('\n')
matches = re.fullmatch(r"((weekly|biweekly|monthly|yearly)\s+(\d+)\s+){0,1}"
r"(remind\s+(\d+)\s+){0,1}"
r"((\d{1,4})-(\d{1,2})-(\d{1,2})\s+){0,1}"
r"(\d{1,2}):(\d{1,2})"
r"(\s+(([\S\s])*)){0,1}",
appointment)
if not matches:
print(f"Ignoring appointment with invalid format: '{appointment}'. ", file=sys.stderr)
continue
matches = matches.groups()
repeat, rep_val, remind, year, month, day, hour, minute, text = (
matches[1], matches[2], matches[4], matches[6], matches[7],
matches[8], matches[9], matches[10], matches[12]
)
# Determine the appointment date:
if year is None:
appt_date = datetime.today()
else:
try:
appt_date = datetime(year=int(year), month=int(month), day=int(day))
except ValueError:
print(f"Ignoring appointment with invalid date: {year}-{month}-{day}", file=sys.stderr)
continue
try:
appt_date = appt_date.replace(hour=int(hour), minute=int(minute))
except ValueError:
print(f"Ignoring appointment with invalid time: {hour}:{minute}", file=sys.stderr)
continue
# If no reminder has been provided it defaults to 0:
remind = remind or 0
# Save the appointment and then save it again as many times as needed at the proper times:
this_appt_id = f"{appt_id}0" if appt_id else None
save_appointment(calendar, appt_date, remind, text, this_appt_id)
rep_val = None if not rep_val else int(rep_val)
if repeat == "weekly":
for i in range(1, rep_val):
appt_date = appt_date + timedelta(days=7)
this_appt_id = f"{appt_id}{i}" if appt_id else None
save_appointment(calendar, appt_date, remind, text, this_appt_id)
elif repeat == "biweekly":
for i in range(1, rep_val):
appt_date = appt_date + timedelta(days=14)
this_appt_id = f"{appt_id}{i}" if appt_id else None
save_appointment(calendar, appt_date, remind, text, this_appt_id)
elif repeat == "monthly":
for i in range(1, rep_val):
appt_date = appt_date.replace(
month=appt_date.month+1 if appt_date.month != 12 else 1,
year=appt_date.year+1 if appt_date.month == 12 else appt_date.year
)
this_appt_id = f"{appt_id}{i}" if appt_id else None
save_appointment(calendar, appt_date, remind, text, this_appt_id)
elif repeat == "yearly":
for i in range(1, rep_val):
appt_date = appt_date.replace(year=appt_date.year+1)
this_appt_id = f"{appt_id}{i}" if appt_id else None
save_appointment(calendar, appt_date, remind, text, this_appt_id)
print("OK")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description=(
"""Simple reminder calendar -- addappointments
Adds the contents of APPOINTMENTS, which contains events specified in the format:
<weekly|biweekly|monthly|yearly> <N>] [remind <M>] [yyyy-mm-dd] HH:mm [<text>]
where N indicates how often to repeat the reminder, and M is the number of
minutes before the reminder is due to notify the user of it,
to the given CALENDAR.
"""
)
)
default_cal = f"{str(Path.home())}/.mycalendar.txt"
parser.add_argument(
'--calendar', metavar='FILE',
type=argparse.FileType('a'),
help=f"use the given calendar file instead of '{default_cal}'"
)
parser.add_argument(
'--id',
help='use the given ID to identify new events instead of generating IDs randomly'
)
parser.add_argument(
'APPOINTMENTS',
nargs='?',
type=argparse.FileType('r'),
help=('the file from which the appointments are read, or STDIN if ommitted')
)
args = parser.parse_args()
appointments = args.APPOINTMENTS or sys.stdin
calendar = args.calendar or open(default_cal, mode='a')
main(appointments, calendar, args.id)