-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiy-calendar
executable file
·393 lines (300 loc) · 16.1 KB
/
diy-calendar
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import locale
import logging
logging.basicConfig(format='%(levelname)s(%(funcName)s): %(message)s')
#logger = logging.getLogger('diy_calendar')
# DIY Calendar
#
# gen_Current_Macros.py -- generate specific TeX macros for given year and locale
#
# This script prints out the LaTeX support macros for the DIY Planner, for the given year
#
# There are two types of functions:
# - gen_macro_<...> -- generates a LaTeX macro, page and language independent
# - write_out_<...> -- write out LaTeX files
#
# Notes: It is assumed week starts on Sunday
# User configuration:
# Locale -- uncomment one only, use utf-8 encoding ONLY
#
locale.setlocale(locale.LC_ALL, 'en_US.utf-8')
#locale.setlocale(locale.LC_ALL, 'en_AU.utf-8') # Australia
#locale.setlocale(locale.LC_ALL, 'en_GB.utf-8') # Great Britain
#locale.setlocale(locale.LC_ALL, 'fr_FR.utf-8') # France
#locale.setlocale(locale.LC_ALL, 'de_DE.utf-8') # Deuchland
#locale.setlocale(locale.LC_ALL, 'es_ES.utf-8') # Espaniol
# Define "Week" and "Notes" words, being used in the Weekly Planner
Week_locale = 'Week'
Notes_locale = 'Notes'
# END user configuration -- do _NOT_ change anything below
#------------------------------------------------------------------------------
import sys
import math
import calendar
calendar.setfirstweekday(calendar.SUNDAY)
#------------------------------------------------------------------------------
# using abbrevs in LaTeX macro names (can't use the locales here, using generic POSIX)
# abbrev and name coincide for May, marking the long with 'L'
day_abbr_C = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
day_name_C = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
month_abbr_C = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
month_name_C = ['', 'January', 'February', 'March', 'April', 'MayL', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
#--------------------------------------------------------------------
# Generate & write out locale (language dependent) macros
#
# defines:
# \Mon ...,
# \MonA ... as 'M' ... (one letter)
# \January ...
# \Jan ...
def write_out_i18n_macros(file):
i18n_macros_FH = open(file, 'w')
i18n_macros_FH.writelines('% Internationalization macros\n\n')
i18n_macros_FH.writelines('\\newcommand{\\Week}{' + Week_locale + '}\n')
i18n_macros_FH.writelines('\\newcommand{\\Notes}{' + Notes_locale + '}\n')
i18n_macros_FH.writelines('\n')
# capitalize 1-letter day abbrevs, bot not the rest (?)
i18n_macros_FH.writelines('% Weekdays\n')
for d in range(0,7):
# i18n_macros_FH.writelines('\\newcommand{\\' + day_name_C[d] + '}{' + calendar.day_name[d].capitalize() + '}\n')
i18n_macros_FH.writelines('\\newcommand{\\' + day_name_C[d] + '}{' + calendar.day_name[d] + '}\n')
i18n_macros_FH.writelines('\n')
i18n_macros_FH.writelines('% Weekdays: abbreviated 2/3 letters\n')
for d in range(0,7):
# i18n_macros_FH.writelines('\\newcommand{\\' + day_abbr_C[d] + '}{' + calendar.day_abbr[d].capitalize() + '}\n')
i18n_macros_FH.writelines('\\newcommand{\\' + day_abbr_C[d] + '}{' + calendar.day_abbr[d] + '}\n')
i18n_macros_FH.writelines('\n')
i18n_macros_FH.writelines('% Weekdays: abbreviated 1 letter\n')
for d in range(0,7):
i18n_macros_FH.writelines('\\newcommand{\\' + day_abbr_C[d] + 'A' + '}{' + calendar.day_abbr[d][0].capitalize() + '}\n')
i18n_macros_FH.writelines('\n')
i18n_macros_FH.writelines('% Months: abbreviated 3 letters\n')
for m in range(1,13):
# i18n_macros_FH.writelines('\\newcommand{\\' + month_abbr_C[m] + '}{' + calendar.month_abbr[m].capitalize() + '}\n')
i18n_macros_FH.writelines('\\newcommand{\\' + month_abbr_C[m] + '}{' + calendar.month_abbr[m] + '}\n')
i18n_macros_FH.writelines('\n')
i18n_macros_FH.writelines('% Months\n')
for m in range(1,13):
# i18n_macros_FH.writelines('\\newcommand{\\' + month_name_C[m] + '}{' + calendar.month_name[m].capitalize() + '}\n')
i18n_macros_FH.writelines('\\newcommand{\\' + month_name_C[m] + '}{' + calendar.month_name[m] + '}\n')
i18n_macros_FH.writelines('\n')
#--------------------------------------------------------------------
# generate one \MonthTbl<month_abbr> macro for the given month
#
# also \MonthTbl<month_abbr>Prev and \MonthTbl<month_abbr>Next
# if required trough 'postfix' optional arg (for previous and next year calendars
def gen_macro_MonthTbl(month, year, *postfix):
month_length = calendar.monthrange(year, month)[1]
# Macro optional postfix: 'Prev' or 'Next' (for previous or next year monthly calendars)
if len(postfix) == 0:
postfix = ['']
if (postfix[0] != '') and (postfix[0] != 'Prev') and (postfix[0] != 'Next'):
sys.stdout.write('\n\nERROR in extra param in gen_macro_MonthTbl() call, postfix value: "' + postfix[0]+ '" as given is not allowed.\n\n')
return ''
# macro def start
macro_def = '\\newcommand{\MonthTbl' + month_abbr_C[ month ] + postfix[0] + '}[1][\hfill]{%\n'
cal = calendar.Calendar( calendar.SUNDAY )
weeks = cal.monthdatescalendar(year, month)
for week_num in range(len(weeks)):
week = weeks[week_num]
for day in week:
#print day
if (day.month == month): # human-readable alignment: insert spaces as required
if (day.day < 10): # - days 1-9
macro_def += ' '
macro_def += '#1{' + str(day.day)
else: # - no days
macro_def += ' #1{'
#print day.weekday
if day.weekday() == calendar.SATURDAY:
if day.day == month_length or day.month != month:
macro_def += '}' # at the end of last row
if week_num < 5:
macro_def += ' \\\\\n#1{} & #1{} & #1{} & #1{} & #1{} & #1{} & #1{}'
else:
macro_def += '} \\\\\n'
else:
macro_def += '} & '
# macro def end
macro_def += '}\n'
return macro_def
#--------------------------------------------------------------------
# Generate and write out table macros for the current, previous and next years
#
# defines:
# \MyYear{<year>}
# \MyYearPrev{<year - 1>}
# \MyYearNext{<year + 1>}
# \MyYear{<year>}
# \MonthTblJan ...
# \MonthTblJanPrev ...
# \MonthTblJanNext ...
def write_out_MonthTbl_macros(year, file):
MonthTbl_macros_FH = open(file, 'w')
MonthTbl_macros_FH.writelines('% Monthly tables macros for current, previous and next year\n\n\n')
MonthTbl_macros_FH.writelines('% Current year -------------------------------------------------------\n\n')
MonthTbl_macros_FH.writelines('\\newcommand{\MyYear}{' + str(year) + '}\n\n')
for m in range(1, 13):
MonthTbl_macros_FH.writelines(gen_macro_MonthTbl(m, year) + '\n')
MonthTbl_macros_FH.writelines('% Prev year ----------------------------------------------------------\n\n')
MonthTbl_macros_FH.writelines('\\newcommand{\MyYearPrev}{' + str(year - 1) + '}\n\n')
for m in range(1, 13):
MonthTbl_macros_FH.writelines(gen_macro_MonthTbl(m, year-1, 'Prev') + '\n')
MonthTbl_macros_FH.writelines('% Next year ----------------------------------------------------------\n\n')
MonthTbl_macros_FH.writelines('\\newcommand{\MyYearNext}{' + str(year + 1) + '}\n\n')
for m in range(1, 13):
MonthTbl_macros_FH.writelines(gen_macro_MonthTbl(m, year+1, 'Next') + '\n')
#--------------------------------------------------------------------
# generate the \MP<month>Left macro for given month and year
# - contains first 3 days of the week for current month
# - incomplete rows are filled from previous/next months and marked '#2'
# - row 6 days are supplied as optional args to '#1' on row 5
# (months starting Sat with 31 days or Sun with 30/31 days)
def gen_macro_MPMonthLeft(month, year):
month_length = calendar.monthrange(year, month)[1]
myCal= calendar.Calendar(calendar.SUNDAY)
weeks = myCal.monthdatescalendar(year, month)
# macro start
macro_def = '\\newcommand{\MP' + month_abbr_C[ month ] + 'Left}[2]{%\n'
for week_num in range(len(weeks)):
if week_num > 4:
logging.warning( '%i-%i has more than 5 rows. Calendar truncated.' % (year, month) )
break
week = weeks[week_num]
for col in range(3):
macro_def += '& '
if week[col].day < 10:
macro_def += ' '
if week[col].month != month:
macro_def += '#2{' + str(week[col].day) + '} ' # days, max range 25-31, prev month
else:
macro_def += '#1{' + str(week[col].day) + '} ' # days, max range 1-7, curr month
#if week[col].weekday() == calendar.TUESDAY:
# macro_def += '} '
#else:
# macro_def += '} & '
if week_num+1 < len(weeks):
macro_def += '\\\\\n'
macro_def += '}\n'
return macro_def
#--------------------------------------------------------------------
# generate the \MP<month>Right
# (very similar to the \MP<month>Right, there is no 6 row problem)
def gen_macro_MPMonthRight(month, year):
myCal= calendar.Calendar(calendar.SUNDAY)
weeks = myCal.monthdatescalendar(year, month)
# macro start
macro_def = '\\newcommand{\MP' + month_abbr_C[ month ] + 'Right}[2]{%\n'
for week_num in range(len(weeks)):
if week_num > 4:
logging.warning( '%i-%i has more than 5 rows. Calendar truncated.' % (year, month) )
break
week = weeks[week_num]
for col in range(3,7):
if col != 3:
macro_def += '& '
if week[col].day < 10:
macro_def += ' '
if week[col].month != month:
macro_def += '#2{' + str(week[col].day) + '} ' # days, max range 25-31, prev month
else:
macro_def += '#1{' + str(week[col].day) + '} ' # days, max range 1-7, curr month
if week_num+1 < len(weeks):
macro_def += '\\\\\n'
macro_def += '}\n'
return macro_def
#--------------------------------------------------------------------
# Generate and write out table macros for the Monthly Planner
def write_out_MP_macros(year, start_month, file):
MP_macros_FH = open(file, 'w')
MP_macros_FH.writelines('% Monthly Planner tables macros for ' + str(year) + '\n\n\n')
for m in range(start_month, 13):
MP_macros_FH.writelines(gen_macro_MPMonthLeft(m, year) + '\n')
MP_macros_FH.writelines(gen_macro_MPMonthRight(m, year) + '\n')
#--------------------------------------------------------------------
# generates the weeks list
def yeardatescalendar(year):
# Joins monthdatescalendar(year, month) intelligently (no duplicates) for the full year
# Return a list of the weeks in the year as full weeks. Weeks are lists of seven datetime.date objects.
# week 0 is []
weeks_list = [[]]
myCal= calendar.Calendar(calendar.SUNDAY)
weeks_list += myCal.monthdatescalendar(year, 1) # Jan goes in unconditionally
for m in range(2, 13):
curr_month = myCal.monthdatescalendar(year, m)
if curr_month[0][0].month == m: # m > 1 always
weeks_list += myCal.monthdatescalendar(year, m) # direct join: month starts Monday
else:
weeks_list += myCal.monthdatescalendar(year, m)[1:] # skip first week: avoid duplication
return weeks_list
#------------------------------------------------------------------------------
# Generate and write out table macros for the Weekly Planner
# special cases: first week may start Dec prev year, last week may end Jan next year
def write_out_WP_macros(year, start_week, file):
WP_macros_FH = open(file, 'w')
WP_macros_FH.writelines('% Weekly Planner macros for ' + str(year) + '\n\n\n')
weeks = yeardatescalendar(year)
for w in range(start_week, len(weeks)):
curr_m = weeks[w][6].month # current month: from last day-of-week
other_m = weeks[w][0].month # month of first day-of-week: current month or previous
curr_y = weeks[w][6].year # year of last day-of-week, same as 'year', except maybe last week
other_y = weeks[w][0].year
# Month macros for the mini cals
prev_month_table = '\MonthTbl'
curr_month_table = '\MonthTbl' + month_abbr_C[curr_m]
curr_month_name = calendar.month_name[curr_m]
next_month_table = '\MonthTbl'
if (curr_m == 1) and (curr_y == year): # first month correction: current month is Jan, prev is Dec last year
prev_month_table += 'DecPrev'
prev_month_name = calendar.month_name[12]
else:
prev_month_table += month_abbr_C[curr_m - 1]
prev_month_name = calendar.month_name[curr_m - 1]
if curr_m == 12: # last month correction: current month is Dec, next is Jan next year
next_month_table += 'JanNext'
next_month_name = calendar.month_name[1]
else:
next_month_table += month_abbr_C[curr_m + 1]
next_month_name = calendar.month_name[curr_m + 1]
if (curr_m == 1) and (curr_y == year + 1): # last week correction: current month is Dec, prev is Nov, next is Jan next year
prev_month_table = '\MonthTbl' + month_abbr_C[11]
prev_month_name = calendar.month_name[11]
curr_month_table = '\MonthTbl' + month_abbr_C[12]
curr_month_name = calendar.month_name[12]
next_month_table = '\MonthTbl' + 'JanNext'
next_month_name = calendar.month_name[1]
left_pg_macro = '\LeftPageWP{' + str(w) + '}'
for d in range(0,4):
left_pg_macro += '{' + str(weeks[w][d].day) + ' ' + calendar.month_abbr[weeks[w][d].month] + '}'
right_pg_header = '\RightPageHeaderWP{'
if other_m == curr_m: # week contained
right_pg_header = '\RightPageHeaderWP{' + calendar.month_abbr[curr_m] + ' ' + str(curr_y) +'}'
elif other_m < curr_m: # week spanning 2 months
right_pg_header = '\RightPageHeaderWP{' + calendar.month_abbr[other_m] + ' -- ' + calendar.month_abbr[curr_m] + ' ' + str(curr_y) +'}'
else: # first and last weeks correction
right_pg_header = '\RightPageHeaderWP{' + calendar.month_abbr[12] + ' ' + str(other_y) + ' -- ' + calendar.month_abbr[1] + ' ' + str(curr_y) +'}'
right_pg_macro = '\RightPageWP'
for d in range(4,7):
right_pg_macro += '{' + str(weeks[w][d].day) + ' ' + calendar.month_abbr[weeks[w][d].month] + '}'
right_pg_macro += '{' + prev_month_name + '}' + '{' + prev_month_table + '}'
right_pg_macro += '{' + curr_month_name + '}' + '{' + curr_month_table + '}'
right_pg_macro += '{' + next_month_name + '}' + '{' + next_month_table + '}'
WP_macros_FH.writelines(left_pg_macro + '\n')
WP_macros_FH.writelines(right_pg_header + '\n')
WP_macros_FH.writelines(right_pg_macro + '\n')
#====================================================================
# Main
import argparse
ap = argparse.ArgumentParser(
description='DIY LaTeX monthly and weekly calendar generator'
)
ap.add_argument('year', type=int)
#meg = ap.add_mutually_exclusive_group()
ap.add_argument('--start-week', type=int, default=1)
ap.add_argument('--start-month', type=int, default=1)
args = ap.parse_args()
write_out_i18n_macros('DYI_i18n.tex')
write_out_MonthTbl_macros(args.year, 'DYI_Month_Tables.tex')
write_out_MP_macros(args.year, args.start_month, 'DYI_Monthly_Planner_Tables.tex')
write_out_WP_macros(args.year, args.start_week, 'DYI_Weekly_Planner_Tables.tex')