From 5ca37578348c82d07e898f9fd7a44dab048d0986 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 1 Oct 2023 01:09:15 +0200 Subject: [PATCH] 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. --- platypush/config/config.yaml | 8 +++---- platypush/plugins/calendar/__init__.py | 30 +++++++++++++++++--------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/platypush/config/config.yaml b/platypush/config/config.yaml index a3906127f7..8cd6508462 100644 --- a/platypush/config/config.yaml +++ b/platypush/config/config.yaml @@ -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 ### ### diff --git a/platypush/plugins/calendar/__init__.py b/platypush/plugins/calendar/__init__.py index 9128ef4157..24d6d0fb6a 100644 --- a/platypush/plugins/calendar/__init__.py +++ b/platypush/plugins/calendar/__init__.py @@ -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,