This repository has been archived by the owner on Jan 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalendar.py
330 lines (260 loc) · 9.91 KB
/
calendar.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
import cairocffi as cairo
import datetime
import locale
import csv
class CalendarError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class DataReader:
"""Reads calendar data from CSV file."""
def __init__(self, year):
self._year = year
self.data = {}
def read(self, filename):
try:
f = open(filename, 'r')
csv_reader = csv.reader(f, dialect="excel")
for row in csv_reader:
# check for valid data
if len(row) < 3 or len(row) > 4:
raise CalendarError("Error in file: " + filename +
" (wrong number of columns)")
# values
month = row[0]
day = row[1]
text = row[2]
# optional: highlight
highlight = False
if len(row) == 4 and row[3].lower() == "yes":
highlight = True
# add to list
key = datetime.date(self._year, int(month), int(day))
if not key in self.data:
self.data[key] = []
self.data[key].append([text, highlight])
except IOError as e:
raise CalendarError("Can't open file: " + filename + " (" +
str(e) + ")")
except TypeError as e:
raise CalendarError("Error in file: " + filename +
" (month must be a number)")
def get(self, date):
if date in self.data:
return self.data[date]
return []
def get_str(self, date):
text = map(lambda x: x[0], self.get(date))
return ", ".join(text)
def is_highlighted(self, date):
highlight = False
tmp = self.get(date)
for d in tmp:
highlight = highlight or d[1]
return highlight
class Calendar:
""" Creates a calendar as vector graphic """
# size of A0 in points (1 point = 1/72.0 inch)
WIDTH = 3370.39
HEIGHT = 2383.94
PADDING_TOP = 300
PADDING_RIGHT = 100
PADDING_LEFT = 100
PADDING_BOTTOM = 50
BOX_WIDTH = 240
BOX_HEIGHT = 50
SIZE_MONTH = 30
SIZE_DAY_NUMBER = 35
SIZE_DAY = 20
SIZE_DATA = 10
SIZE_DATA_MAIN = 20
DATA_MAIN_SHIFT = 13
DEFAULT_COLOR = {'red': 0, 'green': 0, 'blue': 0}
# colors
colors = []
def __init__(self, year, locale="de_DE.utf-8"):
# config
self._year = year
self._locale = locale
self._ctx = None
self._surface=None
self._data_top = DataReader(self._year)
self._data_bottom = DataReader(self._year)
self._data_main = DataReader(self._year)
@property
def year(self):
return self._year
def create(self, filename):
"""
Create the calendar and save to SVG file.
"""
# init cairo
self._surface = cairo.SVGSurface(filename, Calendar.WIDTH, Calendar.HEIGHT)
self._ctx = cairo.Context(self._surface)
# set locale for day and month names, if it doesn't work try (locale.LC_ALL, ''),(locale.LC_ALL, self._locale)
try:
locale.setlocale(locale.LC_ALL, self._locale)
except:
locale.setlocale(locale.LC_ALL, '')
# print month label
for m in range(12):
month = datetime.date(self._year, m+1, 1)
self.__print_month_label(month)
# print days
day = datetime.date(self._year, 1, 1)
delta = datetime.timedelta(1)
while day.year == self._year:
self.__print_day(day)
day += delta
# finish
self._ctx.stroke()
def save_as_pdf(self, filename):
"""
Save created calendar additionally as PDF. Must only be called after `create`.
"""
pdf = cairo.PDFSurface(filename, Calendar.WIDTH, Calendar.HEIGHT)
ctx = cairo.Context(pdf)
ctx.set_source_surface(self._surface)
ctx.paint()
def add_color(self, month, saturday, sunday, highlight):
self.colors.append([month, saturday, sunday, highlight])
def add_data(self, filename, position):
if position == "top":
self._data_top.read(filename)
elif position == "bottom":
self._data_bottom.read(filename)
elif position == "main":
self._data_main.read(filename)
else:
raise CalendarError("Unknown position for data: " + position)
def print_text(self, text, x, y, size, color=DEFAULT_COLOR, relative="tl"):
# font size
self._ctx.set_font_size(size)
# position
x_diff = 0
y_diff = 0
x_bearing, y_bearing, width, height, x_advance, \
y_advance = self._ctx.text_extents(text)
if relative == "c": # center
x_diff = -(width/2 + x_bearing)
y_diff = -(height/2 + y_bearing)
elif relative == "tl": # top left
y_diff = +height
elif relative == "tr": # top right
x_diff = -width - x_bearing
y_diff = +height
elif relative == "br": # bottom right
x_diff = -width - x_bearing
# set position for text
self._ctx.move_to(x + x_diff, y + y_diff)
# draw with color
self._ctx.set_source_rgb(**color)
self._ctx.show_text(text)
self._ctx.set_source_rgb(**Calendar.DEFAULT_COLOR)
# finish
self._ctx.stroke()
def print_png(self, filepath, x, y, w, h):
#create new area to paint
self._ctx.rectangle(x, y, w, h)
self._ctx.clip()
#reset path
self._ctx.new_path()
# load png
loaded_png = cairo.ImageSurface.create_from_png(filepath)
# paint png on clip
self._ctx.set_source_surface(loaded_png, x, y)
self._ctx.paint()
# reset clip and make painting with other functions possible
self._ctx.reset_clip()
self._surface.flush()
def __coords_space_boxes(self):
space_x = (Calendar.WIDTH - Calendar.PADDING_LEFT -
Calendar.PADDING_RIGHT - 12*Calendar.BOX_WIDTH) / 11
space_y = (Calendar.HEIGHT - Calendar.PADDING_TOP -
Calendar.PADDING_BOTTOM - 32*Calendar.BOX_HEIGHT -
space_x) / 30
return (space_x, space_y)
def __coords_month(self, date):
space_x, space_y = self.__coords_space_boxes()
x = Calendar.PADDING_LEFT + (date.month-1) * \
(space_x + Calendar.BOX_WIDTH)
y = Calendar.PADDING_TOP
return (x, y)
def __coords_day(self, date):
month_x, month_y = self.__coords_month(date)
space_x, space_y = self.__coords_space_boxes()
x = month_x
y = month_y + Calendar.BOX_HEIGHT + space_x + (date.day-1) * \
(space_y + Calendar.BOX_HEIGHT)
return (x, y)
def __print_month_label(self, date):
# positions
x, y = self.__coords_month(date)
x_center = x + Calendar.BOX_WIDTH / 2
y_center = y + Calendar.BOX_HEIGHT / 2
# rectangle
color = None
if len(self.colors) > 0:
color = self.colors[(date.month-1) % len(self.colors)][0]
self.__rectangle(x, y, Calendar.BOX_WIDTH, Calendar.BOX_HEIGHT, color)
# text
self.print_text(date.strftime("%B"), x_center, y_center,
Calendar.SIZE_MONTH, relative="c")
def __print_day(self, date):
# positions
x, y = self.__coords_day(date)
# check data for highlight
highlight = False
for d in [self._data_top, self._data_main, self._data_bottom]:
highlight = highlight or d.is_highlighted(date)
# rectangle
color = None
if len(self.colors) > 0:
tmp = self.colors[(date.month-1) % len(self.colors)]
if date.weekday() == 5: # saturday
color = tmp[1]
elif date.weekday() == 6: # sunday
color = tmp[2]
elif highlight: # highlight
color = tmp[3]
self.__rectangle(x, y, Calendar.BOX_WIDTH, Calendar.BOX_HEIGHT, color)
# day number
text_padding = max(0.1*Calendar.SIZE_DAY_NUMBER, 3)
self.print_text(date.strftime("%d"), x + text_padding,
y + text_padding, Calendar.SIZE_DAY_NUMBER,
relative="tl")
# weekday
text_padding = max(0.1*Calendar.SIZE_DAY, 3)
self.print_text(date.strftime("%a"),
x + Calendar.BOX_WIDTH - text_padding,
y + Calendar.BOX_HEIGHT - text_padding,
Calendar.SIZE_DAY, relative="br")
# data
text_padding = max(0.1*Calendar.SIZE_DATA, 3)
# top
self.print_text(self._data_top.get_str(date),
x + Calendar.BOX_WIDTH - text_padding,
y + text_padding, Calendar.SIZE_DATA, relative="tr")
# bottom
self.print_text(self._data_bottom.get_str(date),
x + text_padding,
y + Calendar.BOX_HEIGHT - text_padding,
Calendar.SIZE_DATA, relative="bl")
# main
self.print_text(self._data_main.get_str(date),
x + Calendar.BOX_WIDTH/2 + Calendar.DATA_MAIN_SHIFT,
y + Calendar.BOX_HEIGHT/2, Calendar.SIZE_DATA_MAIN,
relative="c")
def __rectangle(self, x, y, w, h, fill_color=None):
# print rectangle
self._ctx.rectangle(x, y, w, h)
# fill with color
if not fill_color is None:
self._ctx.set_source_rgb(**fill_color)
self._ctx.fill()
self._ctx.set_source_rgb(**Calendar.DEFAULT_COLOR)
# print border
self._ctx.rectangle(x, y, w, h) # lines
# finish
self._ctx.stroke()