Minor improvements to event handling
This commit is contained in:
parent
620211b0fb
commit
48510763a6
@ -154,6 +154,22 @@ class icalendar:
|
||||
|
||||
self.parsed_events = []
|
||||
|
||||
@staticmethod
|
||||
def all_day(event):
|
||||
"""Check if an event is an all day event.
|
||||
Returns True if event is all day, else False
|
||||
"""
|
||||
if not ('end' and 'begin') in event:
|
||||
print('Events must have a starting and ending time')
|
||||
raise Exception('This event is not valid!')
|
||||
else:
|
||||
begin, end = event['begin'], event['end']
|
||||
duration = end - begin
|
||||
if (begin.format('HH:mm') == '00:00' and end.format('HH:mm') == '00:00'
|
||||
and duration.days >= 1):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def show_events(self, fmt='DD MMM YY HH:mm'):
|
||||
"""print all parsed events in a more readable way
|
||||
|
@ -36,9 +36,9 @@ class agenda:
|
||||
|
||||
# Section specific config
|
||||
# Format for formatting dates
|
||||
self.date_format = 'D MMM'
|
||||
self.date_format = 'ddd D MMM'
|
||||
# Fromat for formatting event timings
|
||||
self.event_format = "HH:mm" #use auto for 24/12 hour format?
|
||||
self.time_format = "HH:mm" #use auto for 24/12 hour format?
|
||||
self.language = 'en' # Grab from settings file?
|
||||
self.timezone = get_system_tz()
|
||||
# urls of icalendars
|
||||
@ -121,7 +121,7 @@ class agenda:
|
||||
# Set the width for date, time and event titles
|
||||
date_width = int(max([self.font.getsize(
|
||||
dates['begin'].format(self.date_format, locale=self.language))[0]
|
||||
for dates in agenda_events]) * 1.05)
|
||||
for dates in agenda_events]) * 1.2)
|
||||
logging.debug(('date_width:', date_width))
|
||||
|
||||
# Check if any events were filtered
|
||||
@ -129,8 +129,8 @@ class agenda:
|
||||
|
||||
# Find out how much space the event times take
|
||||
time_width = int(max([self.font.getsize(
|
||||
events['begin'].format(self.event_format, locale=self.language))[0]
|
||||
for events in upcoming_events]) * 1.05)
|
||||
events['begin'].format(self.time_format, locale=self.language))[0]
|
||||
for events in upcoming_events]) * 1.2)
|
||||
logging.debug(('time_width:', time_width))
|
||||
|
||||
# Calculate x-pos for time
|
||||
@ -178,12 +178,10 @@ class agenda:
|
||||
|
||||
# Check if item is an event
|
||||
if 'end' in _:
|
||||
time = _['begin'].format(self.event_format)
|
||||
time = _['begin'].format(self.time_format)
|
||||
|
||||
# ad-hoc! Don't display event begin time if all day
|
||||
# TODO: modifiy ical-parser to somehow tell if event is all day
|
||||
# Maybe event.duration = arrow(end-start).days?
|
||||
if time != '00:00':
|
||||
# Check if event is all day, if not, add the time
|
||||
if parser.all_day(_) == False:
|
||||
write(im_black, (x_time, line_pos[cursor][1]),
|
||||
(time_width, line_height), time,
|
||||
font = self.font, alignment='left')
|
||||
@ -193,10 +191,7 @@ class agenda:
|
||||
'• '+title, font = self.font, alignment='left')
|
||||
cursor += 1
|
||||
|
||||
############################################################################
|
||||
# Exception handling
|
||||
############################################################################
|
||||
|
||||
# If no events were found, write only dates and lines
|
||||
else:
|
||||
cursor = 0
|
||||
for _ in agenda_events:
|
||||
@ -212,6 +207,9 @@ class agenda:
|
||||
|
||||
logging.info('no events found')
|
||||
|
||||
############################################################################
|
||||
# Exception handling
|
||||
############################################################################
|
||||
|
||||
# Save image of black and colour channel in image-folder
|
||||
im_black.save(images+self.name+'.png')
|
||||
|
@ -37,7 +37,8 @@ class calendar:
|
||||
|
||||
self.weekstart = 'Monday'
|
||||
self.show_events = True
|
||||
self.event_format = "D MMM HH:mm"
|
||||
self.date_format = 'D MMM' # used for dates
|
||||
self.time_format = "HH:mm" # used for timings
|
||||
self.language = 'en' # Grab from settings file?
|
||||
|
||||
self.timezone = get_system_tz()
|
||||
@ -108,8 +109,8 @@ class calendar:
|
||||
icon_height = calendar_height // calendar_rows
|
||||
|
||||
# Calculate spacings for calendar area
|
||||
x_spacing_calendar = int((self.width % icon_width) / 2)
|
||||
y_spacing_calendar = int((self.height % calendar_rows) / 2)
|
||||
x_spacing_calendar = int((im_width % icon_width) / 2)
|
||||
y_spacing_calendar = int((im_height % calendar_rows) / 2)
|
||||
|
||||
# Calculate positions for days of month
|
||||
grid_start_y = (month_name_height + weekdays_height + y_spacing_calendar)
|
||||
@ -261,37 +262,56 @@ class calendar:
|
||||
if upcoming_events:
|
||||
|
||||
# Find out how much space (width) the date format requires
|
||||
fmt = self.event_format
|
||||
lang = self.language
|
||||
|
||||
date_width = int(max([self.font.getsize(
|
||||
events['begin'].format(fmt,locale=lang))[0]
|
||||
for events in upcoming_events]) * 1.05)
|
||||
events['begin'].format(self.date_format,locale=lang))[0]
|
||||
for events in upcoming_events]) * 1.1)
|
||||
|
||||
time_width = int(max([self.font.getsize(
|
||||
events['begin'].format(self.time_format, locale=lang))[0]
|
||||
for events in upcoming_events]) * 1.1)
|
||||
|
||||
line_height = self.font.getsize('hg')[1] + line_spacing
|
||||
event_width = im_width - date_width
|
||||
|
||||
event_width_s = im_width - date_width - time_width
|
||||
event_width_l = im_width - date_width
|
||||
|
||||
# Display upcoming events below calendar
|
||||
tomorrow = now.shift(days=1).floor('day')
|
||||
in_two_days = now.shift(days=2).floor('day')
|
||||
|
||||
# Write events and dates below calendar
|
||||
# TODO: check if events all-day and then don't display time
|
||||
|
||||
cursor = 0
|
||||
for event in upcoming_events:
|
||||
name, date = event['title'], event['begin'].format(fmt, locale=lang)
|
||||
print(date)
|
||||
name = event['title']
|
||||
date = event['begin'].format(self.date_format, locale=lang)
|
||||
time = event['begin'].format(self.time_format, locale=lang)
|
||||
|
||||
if now < event['end']:
|
||||
write(im_colour, event_lines[cursor], (date_width, line_height),
|
||||
date, font=self.font, alignment = 'left')
|
||||
write(im_black, (date_width,event_lines[cursor][1]),
|
||||
(event_width, line_height), name, font=self.font,
|
||||
|
||||
# Check if event is all day
|
||||
if parser.all_day(event) == True:
|
||||
write(im_black, (date_width, event_lines[cursor][1]),
|
||||
(event_width_l, line_height), name, font=self.font,
|
||||
alignment = 'left')
|
||||
else:
|
||||
write(im_black, (time_width, event_lines[cursor][1]),
|
||||
(event_width, line_height), time, font=self.font,
|
||||
alignment = 'left')
|
||||
|
||||
write(im_black, (date_width+time_width,event_lines[cursor][1]),
|
||||
(event_width_s, line_height), name, font=self.font,
|
||||
alignment = 'left')
|
||||
cursor += 1
|
||||
else:
|
||||
#leave section empty? or display ----- (dotted line)
|
||||
pass
|
||||
symbol = '- '
|
||||
while self.font.getsize(symbol)[0] < im_width*0.9:
|
||||
symbol += ' -'
|
||||
write(im_black, event_lines[0],
|
||||
(im_width, self.font.getsize(symbol)[1]), symbol,
|
||||
font = self.font)
|
||||
|
||||
|
||||
###################################################################
|
||||
|
Loading…
Reference in New Issue
Block a user