-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvcalendar.py
510 lines (419 loc) · 14.9 KB
/
csvcalendar.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# A class that contains the data to generate a csv file with one line per week
# with birthdays, events, moon phases among others.
#
# Birthday file: one birthday per line.
# line = "yyyy-mm-dd,person", where yyyy is the birth year.
# If the birthday year is unknow, write 0000 as year.
# Nameday file: one nameday (or other event) per line.
# line = "i,mm-dd,person", where i is either 0 to avoid inclusion or 1.
# Moon file: one moon phase per line.
# line = "yyyy-mm-dd,moon_phase".
# The text `moon_phase` will be copied as is.
# You can use unicode characters for the moon phases:
# - ●
# - ◐
# - ○
# - ◑
# or
# - u+e38d, Moon new,
# - u+e394, Moon First quarter,
# - u+e39b, Moon full,
# - u+e3a2, Moon last quarter,
# Event file: one event per line.
# line = "yyyy-mm-dd,event".
# Holiday file: one holiday per line.
# line = "yyyy-mm-dd,holiday".
# with 0000 for unknown birthday year.
import copy
import datetime
from typing import Any,Dict,List,Union
import warnings
__all__ = ['Calendar']
g_months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
g_weekdays = [
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday'
]
def str_for_csv(l: Any, join: str = ', '):
"""Add double quotes (and join if list).
Parameters
----------
- l: Object with member __str__.
- join: joining string.
"""
if l is None:
return '""'
elif isinstance(l, list):
return '"' + join.join([str(v) for v in l]) + '"'
else:
return '"' + str(l) + '"'
class Event:
def __init__(self, datestr: str, name: str):
"""
Parameters
----------
- datestr: date in format yyyy-mm-dd.
- name: name of the birthday person.
"""
self.year = int(datestr[:4])
self.month = int(datestr[5:7])
self.day = int(datestr[8:10])
self.name = name
@property
def celebration_date(self):
return datetime.date(self.year, self.month, self.day)
def __str__(self):
return self.name
# TODO: handle 02-29
class Birthday:
def __init__(self, datestr: str, name: str):
"""
Parameters
----------
- datestr: date in format yyyy-mm-dd.
- name: name of the birthday person.
"""
self.year = int(datestr[:4])
if not self.year:
# Minimum year for datetime.date is 1.
self.year = 1
self.month = int(datestr[5:7])
self.day = int(datestr[8:10])
self.date = datetime.date(self.year, self.month, self.day)
self.name = name
# True whether the celebration must be on the next day (for birthdays
# on Feb. 29th).
self.on_next_day = False
self.celebration_year = datetime.date.today().year
@property
def celebration_date(self):
try:
date = datetime.date(self.celebration_year, self.month, self.day)
except ValueError:
self.on_next_day = True
return datetime.date(self.celebration_year, 3, 1)
return date
def __str__(self):
comment = ''
join_str = ''
if self.year != 1:
comment += str(self.celebration_year - self.year)
join_str = ', '
if self.on_next_day:
comment += join_str + '02-29'
if comment:
return self.name + ' (' + comment + ')'
else:
return self.name
class Nameday:
def __init__(self, datestr: str, name: str):
"""
Parameters
----------
- datestr: date in format mm-dd.
- name: name of the birthday person.
"""
self.month = int(datestr[:2])
self.day = int(datestr[3:5])
self.name = name
self.celebration_year = datetime.date.today().year
@property
def celebration_date(self):
return datetime.date(self.celebration_year, self.month, self.day)
def __str__(self):
return self.name
class Day:
def __init__(self, date: datetime.date):
self.date = date
self.year = date.year
self.month = date.month
self.day = date.day
self.birthdays: List[Birthday] = []
self.namedays: List[Nameday] = []
self.events: List[Event] = []
self.moon = None
self.holiday = None
@property
def holiday(self):
return self.__dict__['holiday']
@holiday.setter
def holiday(self, value: Event):
if (value is not None) and not self.valid_date(value):
warnings.warn('Holiday not at the correct date, ignoring')
self.__dict__['holiday'] = value
@property
def moon(self):
return self.__dict__['moon']
@moon.setter
def moon(self, value: Event):
if (value is not None) and (not self.valid_date(value)):
warnings.warn('Moon not at the correct date, ignoring')
self.__dict__['moon'] = value
def valid_date(self, event: Union[Birthday,Event,Nameday]):
if (hasattr(event, 'year') and (event.year != 1) and
(event.celebration_date.year != self.date.year)):
return False
if event.month != self.date.month:
return False
if event.day != self.date.day:
return False
return True
def add_birthday(self, birthday: Birthday):
if self.valid_date(birthday):
self.birthdays.append(birthday)
else:
warnings.warn('Birthday not at the correct date, ignoring')
def add_nameday(self, nameday: Nameday):
if self.valid_date(nameday):
self.namedays.append(nameday)
else:
warnings.warn('Nameday not at the correct date, ignoring')
def add_event(self, event: Event):
if self.valid_date(event):
self.events.append(event)
else:
warnings.warn('Event not at the correct date, ignoring')
class Week:
def __init__(self,
monday: datetime.date,
left_page: int,
months: List[str]):
"""
Parameters
----------
- monday: a datetime.date object.
"""
if monday.isoweekday() != 1:
raise ValueError('First argument must be on Monday')
self._monday = monday
self._months = months
self._left_page = left_page
self._right_page = left_page + 1
self.days: List[datetime.date] = []
for delta in range(7):
self.days.append(Day(monday + datetime.timedelta(delta)))
def __str__(self):
# This must correspond to the content of self.header.
days_data: List[str] = []
for day in self.days:
days_data.append(day.day)
days_data.append(str_for_csv(day.birthdays))
days_data.append(str_for_csv(day.namedays))
days_data.append(str_for_csv(day.events))
days_data.append(str_for_csv(day.moon))
days_data.append(str_for_csv(day.holiday))
data = [self.code, self.number,
'{:03}'.format(self._left_page),
'{:03}'.format(self._right_page),
self.month_str]
data += days_data
return ','.join([str(d) for d in data])
@property
def header(self):
"""Return the csv header line"""
# This must correspond to the content of self.__str___.
header = ['"code"', '"number"', '"page_left"', '"page_right"',
'"month"']
for d in range(7):
header.append(str_for_csv(g_weekdays[d]))
header.append(str_for_csv('birthdays_' + g_weekdays[d]))
header.append(str_for_csv('namedays_' + g_weekdays[d]))
header.append(str_for_csv('events_' + g_weekdays[d]))
header.append(str_for_csv('moon_' + g_weekdays[d]))
header.append(str_for_csv('holiday_' + g_weekdays[d]))
header_str = ','.join(header)
return header_str
@property
def month_str(self) -> str:
"""Return e.g. 'Jan' or 'Jan - Feb'.
'Jan' would be replaced by the translated month.
"""
month_monday = self.monday.month - 1
month_sunday = self.sunday.month - 1
if month_monday == month_sunday:
return self._months[month_monday]
else:
return (self._months[month_monday] + ' - ' +
self._months[month_sunday])
@property
def monday(self) -> datetime.date:
return self.days[0]
@property
def tuesday(self) -> datetime.date:
return self.days[1]
@property
def wednesday(self) -> datetime.date:
return self.days[2]
@property
def thursday(self) -> datetime.date:
return self.days[3]
@property
def friday(self) -> datetime.date:
return self.days[4]
@property
def saturday(self) -> datetime.date:
return self.days[5]
@property
def sunday(self) -> datetime.date:
return self.days[6]
@property
def number(self) -> int:
"""The number of the calendar week, according to ISO 8601."""
return self._monday.isocalendar()[1]
@property
def code(self) -> str:
"""Return e.g. 2022-04 for the 4th week in 2022."""
return '{}-{:02}'.format(self.monday.year, self._monday.isocalendar()[1])
def add_birthday(self, birthday: Birthday):
delta = (birthday.celebration_date - self._monday).days
self.days[delta].add_birthday(birthday)
def add_nameday(self, nameday: Nameday):
delta = (nameday.celebration_date - self._monday).days
self.days[delta].add_nameday(nameday)
def add_event(self, event: Event):
delta = (event.celebration_date - self._monday).days
self.days[delta].add_event(event)
def add_moon(self, moon: Event):
delta = (moon.celebration_date - self._monday).days
self.days[delta].moon = moon
def add_holiday(self, holiday: Event):
delta = (holiday.celebration_date - self._monday).days
self.days[delta].holiday = holiday
class Calendar:
def __init__(self,
year: int,
extra_weeks: int,
start_page: int = 2,
months: List[str] = g_months):
"""
Parameters
----------
- year: year for which to generate data.
- extra_weeks: number of weeks of the following year.
- start_page: page where the generated data will start.
- months: list of months.
"""
self.year = year
if months is None:
self.months = g_months
self.months = months
self.init_weeks(year, extra_weeks, start_page)
def __str__(self):
if not self.weeks:
return ''
header_str = self.weeks[0].header + '\n'
return header_str + '\n'.join([str(w) for w in self.weeks])
def init_weeks(self, year: int, extra_weeks: int, start_page: int):
first_january = datetime.date(year, 1, 1)
# `first_day` is the Monday in the same week as `first_january`..
self.first_day = first_january - datetime.timedelta(
first_january.weekday())
last_december = datetime.date(year, 12, 31)
# `last_day` is the Sunday in the same week as `last_december`.
self.last_day = last_december + datetime.timedelta(
-last_december.weekday() + 7 * (extra_weeks + 1) - 1)
self.weeks: List[Week] = []
self.week_of_day: Dict[datetime.date,Week] = {}
day = copy.copy(self.first_day)
left_page = copy.copy(start_page)
while day < self.last_day:
week = Week(day, left_page, self.months)
self.weeks.append(week)
for delta in range(7):
self.week_of_day[day + datetime.timedelta(delta)] = week
day += datetime.timedelta(7)
left_page += 2
def add_birthday(self, datestr: str, name: str):
"""
Parameters
----------
- datestr: date in format yyyy-mm-dd, with 0000 for unknown birthday
year.
- name: name of the birthday person.
"""
birthday = Birthday(datestr, name)
birthday.celebration_year = self.year - 1
if birthday.celebration_date in self.week_of_day:
self.week_of_day[birthday.celebration_date].add_birthday(birthday)
birthday = copy.deepcopy(birthday)
birthday.celebration_year = self.year
self.week_of_day[birthday.celebration_date].add_birthday(birthday)
birthday = copy.deepcopy(birthday)
birthday.celebration_year = self.year + 1
if birthday.celebration_date in self.week_of_day:
self.week_of_day[birthday.celebration_date].add_birthday(birthday)
def add_nameday(self, datestr: str, name: str):
"""
Parameters
----------
- datestr: date in format mm-dd.
- name: name of the celebrated saint.
"""
nameday = Nameday(datestr, name)
nameday.celebration_year = self.year - 1
try:
if nameday.celebration_date in self.week_of_day:
self.week_of_day[nameday.celebration_date].add_nameday(nameday)
except ValueError:
pass
nameday = copy.deepcopy(nameday)
nameday.celebration_year = self.year
try:
self.week_of_day[nameday.celebration_date].add_nameday(nameday)
except ValueError:
pass
nameday = copy.deepcopy(nameday)
nameday.celebration_year = self.year + 1
try:
if nameday.celebration_date in self.week_of_day:
self.week_of_day[nameday.celebration_date].add_nameday(nameday)
except ValueError:
pass
def add_event(self, datestr: str, name: str):
"""
Parameters
----------
- datestr: date in format yyyy-mm-dd.
- name: name of the event.
"""
event = Event(datestr, name)
if event.celebration_date in self.week_of_day:
self.week_of_day[event.celebration_date].add_event(event)
def set_moon(self, datestr: str, phase: str):
"""
Parameters
----------
- datestr: date in format yyyy-mm-dd.
- phase: moon phase
"""
moon = Event(datestr, phase)
if moon.celebration_date in self.week_of_day:
self.week_of_day[moon.celebration_date].add_moon(moon)
def set_holiday(self, datestr: str, name: str):
"""
Parameters
----------
- datestr: date in format yyyy-mm-dd.
- name: name of the holiday.
"""
holiday = Event(datestr, name)
if holiday.celebration_date in self.week_of_day:
self.week_of_day[holiday.celebration_date].add_holiday(holiday)