| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2022-10-02 00:49:27 +02:00
										 |  |  | Inkycal Agenda Module | 
					
						
							| 
									
										
										
										
											2023-06-03 16:16:07 +02:00
										 |  |  | Copyright by aceinnolab | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 06:35:08 +02:00
										 |  |  | import arrow | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | from inkycal.custom import * | 
					
						
							| 
									
										
										
										
											2020-05-26 19:10:20 +02:00
										 |  |  | from inkycal.modules.ical_parser import iCalendar | 
					
						
							| 
									
										
										
										
											2022-04-10 06:35:08 +02:00
										 |  |  | from inkycal.modules.template import inkycal_module | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 02:56:04 +02:00
										 |  |  | logger = logging.getLogger(__name__) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 19:10:20 +02:00
										 |  |  | class Agenda(inkycal_module): | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     """Agenda class
 | 
					
						
							|  |  |  |     Create agenda and show events from given icalendars | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     name = "Agenda - Display upcoming events from given iCalendars" | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     requires = { | 
					
						
							|  |  |  |         "ical_urls": { | 
					
						
							|  |  |  |             "label": "iCalendar URL/s, separate multiple ones with a comma", | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     optional = { | 
					
						
							|  |  |  |         "ical_files": { | 
					
						
							|  |  |  |             "label": "iCalendar filepaths, separated with a comma", | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         "date_format": { | 
					
						
							|  |  |  |             "label": "Use an arrow-supported token for custom date formatting " + | 
					
						
							|  |  |  |                      "see https://arrow.readthedocs.io/en/stable/#supported-tokens, e.g. ddd D MMM", | 
					
						
							|  |  |  |             "default": "ddd D MMM", | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         "time_format": { | 
					
						
							|  |  |  |             "label": "Use an arrow-supported token for custom time formatting " + | 
					
						
							|  |  |  |                      "see https://arrow.readthedocs.io/en/stable/#supported-tokens, e.g. HH:mm", | 
					
						
							|  |  |  |             "default": "HH:mm", | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     def __init__(self, config): | 
					
						
							|  |  |  |         """Initialize inkycal_agenda module""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         super().__init__(config) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         config = config['config'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Check if all required parameters are present | 
					
						
							|  |  |  |         for param in self.requires: | 
					
						
							| 
									
										
										
										
											2022-04-10 06:35:08 +02:00
										 |  |  |             if param not in config: | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                 raise Exception(f'config is missing {param}') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # module specific parameters | 
					
						
							|  |  |  |         self.date_format = config['date_format'] | 
					
						
							|  |  |  |         self.time_format = config['time_format'] | 
					
						
							|  |  |  |         self.language = config['language'] | 
					
						
							| 
									
										
										
										
											2020-11-10 11:53:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Check if ical_files is an empty string | 
					
						
							|  |  |  |         if config['ical_urls'] and isinstance(config['ical_urls'], str): | 
					
						
							|  |  |  |             self.ical_urls = config['ical_urls'].split(',') | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.ical_urls = config['ical_urls'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Check if ical_files is an empty string | 
					
						
							|  |  |  |         if config['ical_files'] and isinstance(config['ical_files'], str): | 
					
						
							|  |  |  |             self.ical_files = config['ical_files'].split(',') | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.ical_files = config['ical_files'] | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Additional config | 
					
						
							|  |  |  |         self.timezone = get_system_tz() | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # give an OK message | 
					
						
							| 
									
										
										
										
											2022-10-03 02:56:04 +02:00
										 |  |  |         print(f'{__name__} loaded') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     def generate_image(self): | 
					
						
							|  |  |  |         """Generate image for this module""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Define new image size with respect to padding | 
					
						
							|  |  |  |         im_width = int(self.width - (2 * self.padding_left)) | 
					
						
							|  |  |  |         im_height = int(self.height - (2 * self.padding_top)) | 
					
						
							|  |  |  |         im_size = im_width, im_height | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         logger.info(f'Image size: {im_size}') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Create an image for black pixels and one for coloured pixels | 
					
						
							|  |  |  |         im_black = Image.new('RGB', size=im_size, color='white') | 
					
						
							|  |  |  |         im_colour = Image.new('RGB', size=im_size, color='white') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Calculate the max number of lines that can fit on the image | 
					
						
							|  |  |  |         line_spacing = 1 | 
					
						
							| 
									
										
										
										
											2023-11-07 22:49:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         text_bbox_height = self.font.getbbox("hg") | 
					
						
							| 
									
										
										
										
											2023-11-21 16:03:09 +01:00
										 |  |  |         line_height = text_bbox_height[3] + line_spacing | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         line_width = im_width | 
					
						
							|  |  |  |         max_lines = im_height // line_height | 
					
						
							|  |  |  |         logger.debug(f'max lines: {max_lines}') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Create timeline for agenda | 
					
						
							|  |  |  |         now = arrow.now() | 
					
						
							|  |  |  |         today = now.floor('day') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Create a list of dates for the next days | 
					
						
							|  |  |  |         agenda_events = [ | 
					
						
							| 
									
										
										
										
											2023-11-21 16:03:09 +01:00
										 |  |  |             { | 
					
						
							|  |  |  |                 'begin': today.shift(days=+_), | 
					
						
							|  |  |  |                 'title': today.shift(days=+_).format( | 
					
						
							|  |  |  |                     self.date_format, locale=self.language) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             for _ in range(max_lines)] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Load icalendar from config | 
					
						
							|  |  |  |         self.ical = iCalendar() | 
					
						
							|  |  |  |         parser = self.ical | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if self.ical_urls: | 
					
						
							|  |  |  |             parser.load_url(self.ical_urls) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if self.ical_files: | 
					
						
							|  |  |  |             parser.load_from_file(self.ical_files) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Load events from all icalendar in timerange | 
					
						
							|  |  |  |         upcoming_events = parser.get_events(today, agenda_events[-1]['begin'], | 
					
						
							|  |  |  |                                             self.timezone) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Sort events by beginning time | 
					
						
							|  |  |  |         parser.sort() | 
					
						
							|  |  |  |         # parser.show_events() | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Set the width for date, time and event titles | 
					
						
							| 
									
										
										
										
											2023-11-07 22:49:48 +01:00
										 |  |  |         date_width = int(max([self.font.getlength( | 
					
						
							|  |  |  |             dates['begin'].format(self.date_format, locale=self.language)) | 
					
						
							| 
									
										
										
										
											2023-11-21 16:03:09 +01:00
										 |  |  |             for dates in agenda_events]) * 1.2) | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         logger.debug(f'date_width: {date_width}') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Calculate positions for each line | 
					
						
							|  |  |  |         line_pos = [(0, int(line * line_height)) for line in range(max_lines)] | 
					
						
							|  |  |  |         logger.debug(f'line_pos: {line_pos}') | 
					
						
							| 
									
										
										
										
											2020-11-10 11:53:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Check if any events were filtered | 
					
						
							|  |  |  |         if upcoming_events: | 
					
						
							|  |  |  |             logger.info('Managed to parse events from urls') | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             # Find out how much space the event times take | 
					
						
							| 
									
										
										
										
											2023-11-07 22:49:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             time_width = int(max([self.font.getlength( | 
					
						
							|  |  |  |                 events['begin'].format(self.time_format, locale=self.language)) | 
					
						
							| 
									
										
										
										
											2023-11-21 16:03:09 +01:00
										 |  |  |                 for events in upcoming_events]) * 1.2) | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             logger.debug(f'time_width: {time_width}') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             # Calculate x-pos for time | 
					
						
							|  |  |  |             x_time = date_width | 
					
						
							|  |  |  |             logger.debug(f'x-time: {x_time}') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             # Find out how much space is left for event titles | 
					
						
							|  |  |  |             event_width = im_width - time_width - date_width | 
					
						
							|  |  |  |             logger.debug(f'width for events: {event_width}') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             # Calculate x-pos for event titles | 
					
						
							|  |  |  |             x_event = date_width + time_width | 
					
						
							|  |  |  |             logger.debug(f'x-event: {x_event}') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             # Merge list of dates and list of events | 
					
						
							|  |  |  |             agenda_events += upcoming_events | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             # Sort the combined list in chronological order of dates | 
					
						
							|  |  |  |             by_date = lambda event: event['begin'] | 
					
						
							|  |  |  |             agenda_events.sort(key=by_date) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             # Delete more entries than can be displayed (max lines) | 
					
						
							|  |  |  |             del agenda_events[max_lines:] | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             self._agenda_events = agenda_events | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             cursor = 0 | 
					
						
							|  |  |  |             for _ in agenda_events: | 
					
						
							|  |  |  |                 title = _['title'] | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                 # Check if item is a date | 
					
						
							| 
									
										
										
										
											2022-04-10 06:35:08 +02:00
										 |  |  |                 if 'end' not in _: | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                     ImageDraw.Draw(im_colour).line( | 
					
						
							|  |  |  |                         (0, line_pos[cursor][1], im_width, line_pos[cursor][1]), | 
					
						
							|  |  |  |                         fill='black') | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                     write(im_black, line_pos[cursor], (date_width, line_height), | 
					
						
							|  |  |  |                           title, font=self.font, alignment='left') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                     cursor += 1 | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                 # Check if item is an event | 
					
						
							|  |  |  |                 if 'end' in _: | 
					
						
							| 
									
										
										
										
											2022-04-14 04:08:38 +02:00
										 |  |  |                     time = _['begin'].format(self.time_format, locale=self.language) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                     # Check if event is all day, if not, add the time | 
					
						
							| 
									
										
										
										
											2022-04-10 06:35:08 +02:00
										 |  |  |                     if not parser.all_day(_): | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                         write(im_black, (x_time, line_pos[cursor][1]), | 
					
						
							|  |  |  |                               (time_width, line_height), time, | 
					
						
							|  |  |  |                               font=self.font, alignment='left') | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                     write(im_black, (x_event, line_pos[cursor][1]), | 
					
						
							|  |  |  |                           (event_width, line_height), | 
					
						
							|  |  |  |                           '• ' + title, font=self.font, alignment='left') | 
					
						
							|  |  |  |                     cursor += 1 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # If no events were found, write only dates and lines | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             logger.info('no events found') | 
					
						
							| 
									
										
										
										
											2020-11-10 11:53:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             cursor = 0 | 
					
						
							|  |  |  |             for _ in agenda_events: | 
					
						
							|  |  |  |                 title = _['title'] | 
					
						
							|  |  |  |                 ImageDraw.Draw(im_colour).line( | 
					
						
							|  |  |  |                     (0, line_pos[cursor][1], im_width, line_pos[cursor][1]), | 
					
						
							|  |  |  |                     fill='black') | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                 write(im_black, line_pos[cursor], (date_width, line_height), | 
					
						
							|  |  |  |                       title, font=self.font, alignment='left') | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                 cursor += 1 | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # return the images ready for the display | 
					
						
							|  |  |  |         return im_black, im_colour |