forked from platypush/platypush
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:
parent
966a6ce29e
commit
5ca3757834
2 changed files with 24 additions and 14 deletions
|
@ -301,11 +301,11 @@ backend.http:
|
||||||
# # Installing the dependencies: pip install 'platypush[ical,google]'
|
# # Installing the dependencies: pip install 'platypush[ical,google]'
|
||||||
# calendar:
|
# calendar:
|
||||||
# calendars:
|
# calendars:
|
||||||
# - type: platypush.plugins.google.calendar.GoogleCalendarPlugin
|
# - type: google.calendar
|
||||||
# - type: platypush.plugins.calendar.ical.CalendarIcalPlugin
|
# - type: calendar.ical
|
||||||
# url: https://www.facebook.com/events/ical/upcoming/?uid=your_user_id&key=your_key
|
# url: https://www.facebook.com/events/ical/upcoming/?uid=your_user_id&key=your_key
|
||||||
# - type: platypush.plugins.calendar.ical.CalendarIcalPlugin
|
# - type: calendar.ical
|
||||||
# url: http://riemann/nextcloud/remote.php/dav/public-calendars/9JBWHR7iioM88Y4D?export
|
# url: https://my.nextcloud.org/remote.php/dav/public-calendars/id?export
|
||||||
###
|
###
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
|
@ -5,6 +5,7 @@ import importlib
|
||||||
|
|
||||||
from abc import ABCMeta, abstractmethod
|
from abc import ABCMeta, abstractmethod
|
||||||
|
|
||||||
|
from platypush.context import get_plugin
|
||||||
from platypush.plugins import Plugin, action
|
from platypush.plugins import Plugin, action
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,10 +33,10 @@ class CalendarPlugin(Plugin, CalendarInterface):
|
||||||
|
|
||||||
calendars:
|
calendars:
|
||||||
# Use the Google Calendar integration
|
# Use the Google Calendar integration
|
||||||
- type: platypush.plugins.google.calendar.GoogleCalendarPlugin
|
- type: google.calendar
|
||||||
|
|
||||||
# Import the Facebook events calendar via iCal URL
|
# 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
|
url: https://www.facebook.com/ical/u.php?uid=USER_ID&key=FB_KEY
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -44,17 +45,24 @@ class CalendarPlugin(Plugin, CalendarInterface):
|
||||||
self.calendars = []
|
self.calendars = []
|
||||||
|
|
||||||
for calendar in calendars:
|
for calendar in calendars:
|
||||||
if 'type' not in calendar:
|
cal_type = calendar.pop('type', None)
|
||||||
|
if cal_type is None:
|
||||||
self.logger.warning(
|
self.logger.warning(
|
||||||
"Invalid calendar with no type specified: {}".format(calendar)
|
"Invalid calendar with no type specified: %s", calendar
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
cal_type = calendar.pop('type')
|
try:
|
||||||
module_name = '.'.join(cal_type.split('.')[:-1])
|
# New `calendar.name` format
|
||||||
class_name = cal_type.split('.')[-1]
|
cal_plugin = get_plugin(cal_type).__class__
|
||||||
module = importlib.import_module(module_name)
|
except Exception:
|
||||||
self.calendars.append(getattr(module, class_name)(**calendar))
|
# 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
|
@action
|
||||||
def get_upcoming_events(self, max_results=10):
|
def get_upcoming_events(self, max_results=10):
|
||||||
|
@ -105,7 +113,9 @@ class CalendarPlugin(Plugin, CalendarInterface):
|
||||||
cal_events = calendar.get_upcoming_events().output or []
|
cal_events = calendar.get_upcoming_events().output or []
|
||||||
events.extend(cal_events)
|
events.extend(cal_events)
|
||||||
except Exception as e:
|
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 = sorted(
|
||||||
events,
|
events,
|
||||||
|
|
Loading…
Reference in a new issue