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