Commit 2437b2da authored by erdnaxe's avatar erdnaxe 🎇

Merge branch 'no_icalendar' into 'master'

Massive cleanup

See merge request erdnaxe/saphsync!3
parents 3ab30c41 9f2218fb
......@@ -14,85 +14,60 @@ class Configuration:
"""Regroup user configuration"""
def __init__(self, yaml_file):
"""Load YAML configuration and password from keyring"""
with open(yaml_file, 'r') as f:
config = yaml.load(f)
url = config['url']
username = config['username']
self.url = config['url']
self.username = config['username']
self.password = keyring.get_password('sogo', self.username)
self.selected_groups = config['selected_groups']
self.calendars = self.get_dav_calendars()
# Load password
password = keyring.get_password('sogo', username)
# Load user calendar list
self.calendars = get_dav_calendars(url, username, password)
def get_dav_calendars(url: str, username: str, password: str):
"""Get all calendars from DAV server""""Downloading {}".format(url))
client = caldav.DAVClient(url, username=username, password=password)
principal = client.principal()
calendars = principal.calendars()
return calendars
def get_dav_calendar_name(cal: Calendar) -> str:
"""Return calendar name"""
properties = cal.get_properties([dav.DisplayName(), ])
return properties['{DAV:}displayname']
def get_dav_calendars(self) -> [Calendar]:
"""Get all calendars from DAV server""""Fetching {}".format(self.url))
client = caldav.DAVClient(self.url, username=self.username,
principal = client.principal()
calendars = principal.calendars()
return calendars
def get_all_dav_components(calendars: [Calendar]):
"""Return all components from the given calendars"""
events, todos, journals = [], [], []
components = []
for cal in calendars:"Loading '{}'".format(get_dav_calendar_name(cal)))
# Get events that are in the future
from_date = - timedelta(weeks=1)
events += cal.date_search(from_date)
# Get other components
todos += cal.todos()
journals += cal.journals()
return events, todos, journals
name = cal.get_properties([dav.DisplayName(), ])['{DAV:}displayname']"Loading '{}'".format(name))
from_date = - timedelta(weeks=1) # Only future events
components += cal.date_search(from_date) + cal.todos() + cal.journals()
return components
def write_calendar_from_dav(f, dav_components):
"""Write a master calendar in file f using components from CalDav"""
for dav_component in dav_components:
dav_component.load() # Download CalDav component
ical = icalendar.Event.from_ical( # Parse ical
ical_components = [c for c in ical.walk() if != 'VCALENDAR']
for ical_component in ical_components:
if == "VEVENT":
# If it is an event, then check it is in the correct group
if, configuration.selected_groups):
# Configure logging system
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
# Load user configuration, then user components
# Load user configuration, user components then write calendar
configuration = Configuration('config.yml')
events, todos, journals = get_all_dav_components(configuration.calendars)
# Create master calendar and populate with components
calendar = icalendar.Calendar()
calendar.add('version', '2.0')
calendar.add('prodid', '-//SaphSync//EN')
calendar.add('method', 'PUBLISH')
calendar.add('x-wr-calname', 'SaphSync')
# Copy events
for event_dav in events:
event_ical = icalendar.Event.from_ical(
# Get the event
# TODO Fix this part because there is always only one event
event = None
for component in event_ical.walk():
if == "VEVENT":
event = component
# If it is in the correct group
if, configuration.selected_groups):
logging.debug('An event was added : {}'.format(event.get('summary')))
# Write resulting calendar
dav_components = get_all_dav_components(configuration.calendars)
with open('calendar.ics', 'wb') as f:
write_calendar_from_dav(f, dav_components)
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