Commit d74090db authored by erdnaxe's avatar erdnaxe 🎇

Final pass, document and refractor code

parent f81fe730
......@@ -7,19 +7,21 @@ logger = logging.getLogger('Filters')
def group(event, selected_groups) -> bool:
"""Filter group according to the group selected"""
summary = event.get('summary')
summary_search = re.search('([0-9]{3}) - ([A-Z]{2}) \(grpe : ([^\)]*)\)', summary, re.IGNORECASE)
summary_search = re.search('([0-9]{3}) - ([A-Z]{2}) \(grpe : ([^\)]*)\)',
summary, re.IGNORECASE)
if summary_search:
ue, groups = 'ue_' + summary_search.group(1), summary_search.group(3).split(' - ')
ue, groups = 'ue_' + summary_search.group(1), summary_search.group(
3).split(' - ')
if ue in selected_groups:
# UE is configured
for group in groups:
if group in selected_groups[ue]:
# Group was selected
return True
# Group is not the one selected
logger.info('Removed {} because it had not been chosen'.format(summary))
logger.info(f"Remove '{summary}' because it had not been chosen")
return False
# If there is not a group specified do not filter
......
import logging
from datetime import datetime, timedelta
import caldav
import icalendar
import keyring
import yaml
from caldav import DAVClient
from caldav.objects import dav
from icalendar import Calendar, Event
from caldav.objects import dav, Calendar
import filters
class Configuration:
"""Regroup app configuration"""
"""Regroup user configuration"""
def __init__(self, yaml_file):
with open(yaml_file, 'r') as f:
......@@ -23,46 +24,53 @@ class Configuration:
password = keyring.get_password('sogo', username)
# Load user calendar list
self.calendars = get_calendars(url, username, password)
self.calendars = get_dav_calendars(url, username, password)
def get_calendars(url, username, password):
""" Get all calendars from DAV server """
def get_dav_calendars(url: str, username: str, password: str):
"""Get all calendars from DAV server"""
logging.info("Downloading {}".format(url))
client = DAVClient(url, username=username, password=password)
client = caldav.DAVClient(url, username=username, password=password)
principal = client.principal()
calendars = principal.calendars()
return calendars
def get_calendar_name(cal):
def get_dav_calendar_name(cal: Calendar) -> str:
"""Return calendar name"""
properties = cal.get_properties([dav.DisplayName(), ])
return properties['{DAV:}displayname']
def get_all_dav_components(calendars: [Calendar]):
"""Return all components from the given calendars"""
events, todos, journals = [], [], []
for cal in calendars:
logging.info("Loading '{}'".format(get_dav_calendar_name(cal)))
# Get events that are in the future
from_date = datetime.today() - timedelta(weeks=1)
events += cal.date_search(from_date)
# Get other components
todos += cal.todos()
journals += cal.journals()
return events, todos, journals
# Configure logging system
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
# Load user configuration
# Load user configuration, then user components
configuration = Configuration('config.yml')
events, todos, journals = get_all_dav_components(configuration.calendars)
# Regroup components from every calendars
events, todos, journals = [], [], []
for calendar in configuration.calendars:
logging.info("Loading '{}'".format(get_calendar_name(calendar)))
# Get events that are in the future
from_date = datetime.today() - timedelta(weeks=1)
events += calendar.date_search(from_date)
# Get other components
todos += calendar.todos()
journals += calendar.journals()
# Create master calendar
calendar = Calendar()
# Create master calendar and populate with components
calendar = icalendar.Calendar()
calendar.add('version', '2.0')
calendar.add('prodid', '-//SaphSync//EN')
calendar.add('method', 'PUBLISH')
......@@ -71,9 +79,11 @@ calendar.add('x-wr-calname', 'SaphSync')
# Copy events
for event_dav in events:
event_dav.load()
event_ical = Event.from_ical(event_dav.data)
event_ical = icalendar.Event.from_ical(event_dav.data)
# Get the event
# TODO Fix this part because there is always only one event
event = None
for component in event_ical.walk():
if component.name == "VEVENT":
event = component
......@@ -86,4 +96,3 @@ for event_dav in events:
# Write resulting calendar
with open('calendar.ics', 'wb') as f:
f.write(calendar.to_ical())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment