Commit f81fe730 authored by erdnaxe's avatar erdnaxe 🎇

Second pass, saphsync is now able to download ical components from dav

parent a4c2bb20
from urllib.parse import urljoin
import keyring
import yaml
class Configuration:
"""Regroup app configuration"""
def __init__(self, yaml_file):
with open(yaml_file, 'r') as f:
config = yaml.load(f)
self.url = config['url']
self.username = config['username']
self.selected_groups = config['selected_groups']
# Load password
self.password = keyring.get_password('sogo', self.username)
......@@ -4,7 +4,7 @@ import re
logger = logging.getLogger('Filters')
def filter_group(event, selected_groups) -> bool:
def group(event, selected_groups) -> bool:
"""Filter group according to the group selected"""
summary = event.get('summary')
summary_search ='([0-9]{3}) - ([A-Z]{2}) \(grpe : ([^\)]*)\)', summary, re.IGNORECASE)
import logging
from datetime import datetime, timedelta
from icalendar import Calendar
import configuration
import filters
import keyring
import yaml
from caldav import DAVClient
from caldav.objects import dav
from icalendar import Calendar, Event
import filters
class Configuration:
"""Regroup app configuration"""
def __init__(self, yaml_file):
with open(yaml_file, 'r') as f:
config = yaml.load(f)
url = config['url']
username = config['username']
self.selected_groups = config['selected_groups']
# Load password
password = keyring.get_password('sogo', username)
# Load user calendar list
self.calendars = get_calendars(url, username, password)
from datetime import datetime, timedelta
from caldav import DAVClient
def get_calendars(url, username, password):
""" Get all calendars from DAV server """
......@@ -17,20 +34,32 @@ def get_calendars(url, username, password):
calendars = principal.calendars()
return calendars
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
config = configuration.Configuration('config.yml')
dav_calendars = get_calendars(config.url, config.username, config.password)
for calendar in dav_calendars:
properties = calendar.get_properties([dav.DisplayName(), ])
name = properties['{DAV:}displayname']
print("Traitement de " + name)
def get_calendar_name(cal):
properties = cal.get_properties([dav.DisplayName(), ])
return properties['{DAV:}displayname']
# Configure logging system
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
# Load user configuration
configuration = Configuration('config.yml')
# Regroup components from every calendars
events, todos, journals = [], [], []
for calendar in configuration.calendars:"Loading '{}'".format(get_calendar_name(calendar)))
# Get events that are in the future
from_date = - timedelta(weeks=1)
events = calendar.date_search(from_date)
todos = calendar.todos()
journals = calendar.journals()
print(events, todos, journals)
events += calendar.date_search(from_date)
# Get other components
todos += calendar.todos()
journals += calendar.journals()
# Create master calendar
calendar = Calendar()
......@@ -39,33 +68,22 @@ calendar.add('prodid', '-//SaphSync//EN')
calendar.add('method', 'PUBLISH')
calendar.add('x-wr-calname', 'SaphSync')
# Fetch online calendar and copy components
for url in config.calendars_url:
online_calendar = OnlineCalendar(config.url, config.login, config.password)
# Copy timezone definitions
tz = online_calendar.get_timezone()
# Copy events
for event_dav in events:
event_ical = Event.from_ical(
# Copy events
for event in online_calendar.get_events():
# If it is an event more recent than past week and in the correct group
if filter_date(event) and filter_group(event, config.selected_groups):
logging.debug('An event was added : {}'.format(event.get('summary')))
# Get the event
for component in event_ical.walk():
if == "VEVENT":
event = component
# Copy tasks
for todo in online_calendar.get_todos():
logging.debug('A task was added : {}'.format(todo.get('summary')))
# Copy other components
for component in online_calendar.get_other_components():'A unrecognised component ({}) was added : {}'.format(, component.to_ical()))
# If it is in the correct group
if, configuration.selected_groups):
logging.debug('An event was added : {}'.format(event.get('summary')))
# Write resulting calendar
with open('calendar.ics', 'wb') as f:
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