E-Paper-Calendar/Calendar/CalendarInterface.py

44 lines
1.8 KiB
Python
Raw Normal View History

2019-02-23 22:11:49 +01:00
from DataSourceInterface import DataSourceInterface
from datetime import datetime, timezone, timedelta
2019-02-23 22:11:49 +01:00
class CalendarInterface (DataSourceInterface):
"""Interface for fetching and processing calendar event information."""
def __init__(self):
self.loaded_events = self.__get_events__()
self.__sort_events__()
def __sort_events__(self):
self.loaded_events.sort(key=lambda x : x.begin_datetime)
def __get_events__(self):
2019-02-23 22:11:49 +01:00
raise NotImplementedError("Functions needs to be implemented")
def get_upcoming_events(self):
return self.__get_events_to_filter__(lambda x : (x.begin_datetime - datetime.now(timezone.utc)) > timedelta(days=-1))
2019-02-23 22:11:49 +01:00
def get_today_events(self):
return self.get_day_events(datetime.now(timezone.utc))
2019-02-23 22:11:49 +01:00
def get_day_events(self, date):
return self.__get_events_to_filter__(lambda x : x.begin_datetime.strftime('%d-%m-%y') == date.strftime('%d-%m-%y'))
2019-02-23 22:11:49 +01:00
def get_month_events(self, month = -1):
if month < 0:
month = datetime.now().month
return self.__get_events_to_filter__(lambda x : x.begin_datetime.month == month)
2019-02-23 22:11:49 +01:00
def get_week_events(self, week = -1):
if week < 0 and week_starts_on == "Monday":
week = int(datetime.now().strftime('%W')) + 1
elif week < 0:
week = int(datetime.now().strftime('%U')) + 1
if week_starts_on == "Monday":
return self.__get_events_to_filter__(lambda x : int(x.begin_datetime.strftime('%W')) + 1 == week)
else:
return self.__get_events_to_filter__(lambda x : int(x.begin_datetime.strftime('%U')) + 1 == week)
def __get_events_to_filter__(self, event_filter):
if self.loaded_events is None:
return []
return [event for event in self.loaded_events if event_filter(event)]