2020-05-18 03:46:49 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Agenda module for Inky-Calendar Project
|
|
|
|
Copyright by aceisace
|
|
|
|
"""
|
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
from inkycal.modules.template import inkycal_module
|
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
|
2020-05-23 01:45:40 +02:00
|
|
|
|
2020-05-18 03:46:49 +02:00
|
|
|
import calendar as cal
|
|
|
|
import arrow
|
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
filename = os.path.basename(__file__).split('.py')[0]
|
|
|
|
logger = logging.getLogger(filename)
|
2020-05-26 19:10:20 +02:00
|
|
|
logger.setLevel(level=logging.ERROR)
|
2020-05-18 03:46:49 +02:00
|
|
|
|
2020-05-26 19:10:20 +02:00
|
|
|
class Agenda(inkycal_module):
|
2020-05-18 03:46:49 +02:00
|
|
|
"""Agenda class
|
|
|
|
Create agenda and show events from given icalendars
|
|
|
|
"""
|
|
|
|
|
2020-11-09 17:51:15 +01:00
|
|
|
name = "Inkycal Agenda"
|
|
|
|
|
|
|
|
requires = {
|
|
|
|
"ical_urls" : {
|
|
|
|
"label":"iCalendar URL/s, separate multiple ones with a comma",
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
optional = {
|
|
|
|
"ical_files" : {
|
|
|
|
"label":"iCalendar filepaths, separated with a comma",
|
|
|
|
"default":[]
|
|
|
|
},
|
|
|
|
|
|
|
|
"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",
|
|
|
|
},
|
|
|
|
|
|
|
|
"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",
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-18 03:46:49 +02:00
|
|
|
def __init__(self, section_size, section_config):
|
|
|
|
"""Initialize inkycal_agenda module"""
|
2020-05-23 01:45:40 +02:00
|
|
|
|
|
|
|
super().__init__(section_size, section_config)
|
2020-11-09 17:51:15 +01:00
|
|
|
|
|
|
|
for param in self.equires:
|
2020-05-23 01:45:40 +02:00
|
|
|
if not param in section_config:
|
|
|
|
raise Exception('config is missing {}'.format(param))
|
|
|
|
|
|
|
|
# module specific parameters
|
2020-11-09 17:51:15 +01:00
|
|
|
self.date_format = self.config['date_format']
|
|
|
|
self.time_format = self.config['time_format']
|
2020-06-19 19:40:50 +02:00
|
|
|
self.language = self.config['language']
|
2020-06-03 15:48:30 +02:00
|
|
|
self.ical_urls = self.config['ical_urls']
|
2020-11-09 17:51:15 +01:00
|
|
|
self.ical_files = self.config['ical_files']
|
|
|
|
|
|
|
|
self.timezone = get_system_tz()
|
2020-05-18 03:46:49 +02:00
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
# give an OK message
|
2020-11-09 17:51:15 +01:00
|
|
|
print('{0} loaded'.format(filename))
|
2020-05-18 03:46:49 +02:00
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
def _validate(self):
|
|
|
|
"""Validate module-specific parameters"""
|
2020-06-19 19:40:50 +02:00
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
if not isinstance(self.date_format, str):
|
|
|
|
print('date_format has to be an arrow-compatible token')
|
2020-06-19 19:40:50 +02:00
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
if not isinstance(self.time_format, str):
|
|
|
|
print('time_format has to be an arrow-compatible token')
|
2020-06-19 19:40:50 +02:00
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
if not isinstance(self.language, str):
|
|
|
|
print('language has to be a string: "en" ')
|
2020-06-19 19:40:50 +02:00
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
if not isinstance(self.timezone, str):
|
|
|
|
print('The timezone has bo be a string.')
|
2020-06-19 19:40:50 +02:00
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
if not isinstance(self.ical_urls, list):
|
|
|
|
print('ical_urls has to be a list ["url1", "url2"] ')
|
2020-06-19 19:40:50 +02:00
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
if not isinstance(self.ical_files, list):
|
|
|
|
print('ical_files has to be a list ["path1", "path2"] ')
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
def generate_image(self):
|
|
|
|
"""Generate image for this module"""
|
|
|
|
|
|
|
|
# Define new image size with respect to padding
|
2020-05-23 01:45:40 +02:00
|
|
|
im_width = int(self.width - (self.width * 2 * self.margin_x))
|
|
|
|
im_height = int(self.height - (self.height * 2 * self.margin_y))
|
2020-05-18 03:46:49 +02:00
|
|
|
im_size = im_width, im_height
|
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
logger.info('Image size: {0}'.format(im_size))
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
# Create an image for black pixels and one for coloured pixels
|
2020-05-23 01:45:40 +02:00
|
|
|
im_black = Image.new('RGB', size = im_size, color = 'white')
|
2020-05-18 03:46:49 +02:00
|
|
|
im_colour = Image.new('RGB', size = im_size, color = 'white')
|
|
|
|
|
|
|
|
# Calculate the max number of lines that can fit on the image
|
|
|
|
line_spacing = 1
|
|
|
|
line_height = int(self.font.getsize('hg')[1]) + line_spacing
|
|
|
|
line_width = im_width
|
|
|
|
max_lines = im_height // line_height
|
2020-05-23 01:45:40 +02:00
|
|
|
logger.debug(('max lines:',max_lines))
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
# Create timeline for agenda
|
|
|
|
now = arrow.now()
|
|
|
|
today = now.floor('day')
|
|
|
|
|
|
|
|
# Create a list of dates for the next days
|
|
|
|
agenda_events = [
|
|
|
|
{'begin':today.shift(days=+_),
|
|
|
|
'title': today.shift(days=+_).format(
|
|
|
|
self.date_format,locale=self.language)}
|
|
|
|
for _ in range(max_lines)]
|
|
|
|
|
|
|
|
# Load icalendar from config
|
2020-05-26 19:10:20 +02:00
|
|
|
self.ical = iCalendar()
|
2020-06-04 00:55:44 +02:00
|
|
|
parser = self.ical
|
|
|
|
|
2020-05-18 03:46:49 +02:00
|
|
|
if self.ical_urls:
|
|
|
|
parser.load_url(self.ical_urls)
|
|
|
|
if self.ical_files:
|
|
|
|
parser.load_from_file(self.ical_files)
|
|
|
|
|
|
|
|
# Load events from all icalendar in timerange
|
2020-05-30 00:45:53 +02:00
|
|
|
upcoming_events = parser.get_events(today, agenda_events[-1]['begin'],
|
|
|
|
self.timezone)
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
# Sort events by beginning time
|
|
|
|
parser.sort()
|
|
|
|
# parser.show_events()
|
2020-05-23 01:45:40 +02:00
|
|
|
|
2020-05-18 03:46:49 +02:00
|
|
|
# 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]
|
2020-05-18 18:31:23 +02:00
|
|
|
for dates in agenda_events]) * 1.2)
|
2020-05-23 01:45:40 +02:00
|
|
|
logger.debug(('date_width:', date_width))
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
# Check if any events were filtered
|
|
|
|
if upcoming_events:
|
2020-05-23 01:45:40 +02:00
|
|
|
|
2020-05-18 03:46:49 +02:00
|
|
|
# Find out how much space the event times take
|
|
|
|
time_width = int(max([self.font.getsize(
|
2020-05-18 18:31:23 +02:00
|
|
|
events['begin'].format(self.time_format, locale=self.language))[0]
|
|
|
|
for events in upcoming_events]) * 1.2)
|
2020-05-23 01:45:40 +02:00
|
|
|
logger.debug(('time_width:', time_width))
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
# Calculate x-pos for time
|
|
|
|
x_time = date_width
|
2020-05-23 01:45:40 +02:00
|
|
|
logger.debug(('x-time:', x_time))
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
# Find out how much space is left for event titles
|
|
|
|
event_width = im_width - time_width - date_width
|
2020-05-23 01:45:40 +02:00
|
|
|
logger.debug(('width for events:', event_width))
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
# Calculate x-pos for event titles
|
|
|
|
x_event = date_width + time_width
|
2020-05-23 01:45:40 +02:00
|
|
|
logger.debug(('x-event:', x_event))
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
# Calculate positions for each line
|
|
|
|
line_pos = [(0, int(line * line_height)) for line in range(max_lines)]
|
2020-05-23 01:45:40 +02:00
|
|
|
logger.debug(('line_pos:', line_pos))
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
# Merge list of dates and list of events
|
|
|
|
agenda_events += upcoming_events
|
|
|
|
|
|
|
|
# Sort the combined list in chronological order of dates
|
|
|
|
by_date = lambda event: event['begin']
|
|
|
|
agenda_events.sort(key = by_date)
|
|
|
|
|
|
|
|
# Delete more entries than can be displayed (max lines)
|
|
|
|
del agenda_events[max_lines:]
|
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
self._agenda_events = agenda_events
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
cursor = 0
|
|
|
|
for _ in agenda_events:
|
|
|
|
title = _['title']
|
|
|
|
|
|
|
|
# Check if item is a date
|
|
|
|
if not 'end' in _:
|
|
|
|
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
|
|
|
|
2020-05-18 03:46:49 +02:00
|
|
|
write(im_black, line_pos[cursor], (date_width, line_height),
|
|
|
|
title, font = self.font, alignment='left')
|
|
|
|
|
|
|
|
cursor += 1
|
|
|
|
|
|
|
|
# Check if item is an event
|
|
|
|
if 'end' in _:
|
2020-05-18 18:31:23 +02:00
|
|
|
time = _['begin'].format(self.time_format)
|
2020-05-18 03:46:49 +02:00
|
|
|
|
2020-05-18 18:31:23 +02:00
|
|
|
# Check if event is all day, if not, add the time
|
|
|
|
if parser.all_day(_) == False:
|
2020-05-18 03:46:49 +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
|
|
|
|
2020-05-18 03:46:49 +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
|
|
|
|
2020-05-18 18:31:23 +02:00
|
|
|
# If no events were found, write only dates and lines
|
2020-05-18 03:46:49 +02:00
|
|
|
else:
|
|
|
|
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
|
|
|
|
2020-05-18 03:46:49 +02:00
|
|
|
write(im_black, line_pos[cursor], (date_width, line_height),
|
|
|
|
title, font = self.font, alignment='left')
|
|
|
|
|
|
|
|
cursor += 1
|
|
|
|
|
2020-05-23 01:45:40 +02:00
|
|
|
logger.info('no events found')
|
2020-05-18 03:46:49 +02:00
|
|
|
|
2020-11-09 17:51:15 +01:00
|
|
|
# return the images ready for the display
|
|
|
|
return im_black, im_colour
|
2020-05-18 03:46:49 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-05-23 01:45:40 +02:00
|
|
|
print('running {0} in standalone mode'.format(filename))
|