A more readable configuration for the `calendar` plugin.

The old type configuration
(`platypush.plugins.calendar.name.CalendarNamePlugin`) is a bit clunky.

Instead, since the type will always be a plugin, we should encourage
the use of `calendar.name` directly to identify the type.
This commit is contained in:
Fabio Manganiello 2023-10-01 01:09:15 +02:00
parent 966a6ce29e
commit 5ca3757834
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
2 changed files with 24 additions and 14 deletions

View File

@ -301,11 +301,11 @@ backend.http:
# # Installing the dependencies: pip install 'platypush[ical,google]'
# calendar:
# calendars:
# - type: platypush.plugins.google.calendar.GoogleCalendarPlugin
# - type: platypush.plugins.calendar.ical.CalendarIcalPlugin
# - type: google.calendar
# - type: calendar.ical
# url: https://www.facebook.com/events/ical/upcoming/?uid=your_user_id&key=your_key
# - type: platypush.plugins.calendar.ical.CalendarIcalPlugin
# url: http://riemann/nextcloud/remote.php/dav/public-calendars/9JBWHR7iioM88Y4D?export
# - type: calendar.ical
# url: https://my.nextcloud.org/remote.php/dav/public-calendars/id?export
###
###

View File

@ -5,6 +5,7 @@ import importlib
from abc import ABCMeta, abstractmethod
from platypush.context import get_plugin
from platypush.plugins import Plugin, action
@ -32,10 +33,10 @@ class CalendarPlugin(Plugin, CalendarInterface):
calendars:
# Use the Google Calendar integration
- type: platypush.plugins.google.calendar.GoogleCalendarPlugin
- type: google.calendar
# Import the Facebook events calendar via iCal URL
- type: platypush.plugins.calendar.ical.IcalCalendarPlugin
- type: calendar.ical
url: https://www.facebook.com/ical/u.php?uid=USER_ID&key=FB_KEY
"""
@ -44,17 +45,24 @@ class CalendarPlugin(Plugin, CalendarInterface):
self.calendars = []
for calendar in calendars:
if 'type' not in calendar:
cal_type = calendar.pop('type', None)
if cal_type is None:
self.logger.warning(
"Invalid calendar with no type specified: {}".format(calendar)
"Invalid calendar with no type specified: %s", calendar
)
continue
cal_type = calendar.pop('type')
module_name = '.'.join(cal_type.split('.')[:-1])
class_name = cal_type.split('.')[-1]
module = importlib.import_module(module_name)
self.calendars.append(getattr(module, class_name)(**calendar))
try:
# New `calendar.name` format
cal_plugin = get_plugin(cal_type).__class__
except Exception:
# Legacy `platypush.plugins.calendar.name.CalendarNamePlugin` format
module_name = '.'.join(cal_type.split('.')[:-1])
class_name = cal_type.split('.')[-1]
module = importlib.import_module(module_name)
cal_plugin = getattr(module, class_name)
self.calendars.append(cal_plugin(**calendar))
@action
def get_upcoming_events(self, max_results=10):
@ -105,7 +113,9 @@ class CalendarPlugin(Plugin, CalendarInterface):
cal_events = calendar.get_upcoming_events().output or []
events.extend(cal_events)
except Exception as e:
self.logger.warning('Could not retrieve events: {}'.format(str(e)))
self.logger.warning(
'Could not retrieve events from calendar %s: %s', calendar, e
)
events = sorted(
events,