Commit 784093d9 authored by Alexandre (erdnaxe)'s avatar Alexandre (erdnaxe) 🎇

Less code, better life

parent e8051996
#!/usr/bin/env python3
import logging
from datetime import datetime, timedelta
......@@ -11,42 +13,32 @@ import filters
class Configuration:
"""Regroup user configuration"""
"""Regroup user DavCal configuration"""
def __init__(self, yaml_file):
"""Load YAML configuration and password from keyring"""
"""Fetch DavCal from YAML configuration and keyring"""
with open(yaml_file, 'r') as f:
config = yaml.load(f)
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()
def get_dav_calendars(self) -> [Calendar]:
"""Get all calendars from DAV server"""
logging.info("Fetching {}".format(self.url))
client = caldav.DAVClient(self.url, username=self.username,
password=self.password)
principal = client.principal()
calendars = principal.calendars()
return calendars
def get_all_dav_components(calendars: [Calendar]):
"""Return all components from the given calendars"""
components = []
for cal in calendars:
name = cal.get_properties([dav.DisplayName(), ])['{DAV:}displayname']
logging.info("Loading '\033[1;1m{}\033[1;0m'".format(name))
from_date = datetime.today() - timedelta(weeks=1) # Only future events
components += cal.date_search(from_date) + cal.todos() + cal.journals()
c = yaml.load(f)
logging.info("Fetching {}".format(c['url']))
password = keyring.get_password('sogo', c['username'])
client = caldav.DAVClient(c['url'], username=c['username'],
password=password)
self.calendars = client.principal().calendars()
self.selected_groups = c['selected_groups']
return components
def get_all_dav_components(self):
"""Return all components from the given calendars"""
components = []
for cal in self.calendars:
n = cal.get_properties([dav.DisplayName(), ])['{DAV:}displayname']
logging.info("Loading '\033[1;1m{}\033[1;0m'".format(n))
from_date = datetime.today() - timedelta(weeks=1) # Future events
components += cal.date_search(from_date)
components += cal.todos() + cal.journals()
return components
def write_calendar_from_dav(f, dav_components):
def write_calendar_from_dav(f, dav_components, selected_groups):
"""Write a master calendar in file f using components from CalDav"""
f.write(b'BEGIN:VCALENDAR\nPRODID:-//SaphSync//EN\nVERSION:2.0\n')
for dav_component in dav_components:
......@@ -56,26 +48,23 @@ def write_calendar_from_dav(f, dav_components):
for ical_component in ical_components:
if ical_component.name == "VEVENT":
# If it is an event, then check it is in the correct group
if filters.group(ical_component, configuration.selected_groups):
if filters.group(ical_component, selected_groups):
f.write(ical_component.to_ical())
else:
f.write(ical_component.to_ical())
f.write(b'END:VCALENDAR')
# Configure logging system
logging.addLevelName(logging.INFO,
"\033[1;36m%s\033[1;0m" % logging.getLevelName(logging.INFO))
logging.addLevelName(logging.WARNING,
"\033[1;103m%s\033[1;0m" % logging.getLevelName(logging.WARNING))
logging.addLevelName(logging.ERROR,
"\033[1;41m%s\033[1;0m" % logging.getLevelName(logging.ERROR))
logging.basicConfig(
level=logging.INFO,
format='\033[1;90m%(asctime)s\033[1;0m %(levelname)s %(message)s')
if __name__ == '__main__':
# Configure logging system
logging.addLevelName(logging.INFO, "\033[1;36mINFO\033[1;0m")
logging.addLevelName(logging.WARNING, "\033[1;103mWARNING\033[1;0m")
logging.addLevelName(logging.ERROR, "\033[1;41mERROR\033[1;0m")
logging.basicConfig(
level=logging.INFO,
format='\033[1;90m%(asctime)s\033[1;0m %(levelname)s %(message)s')
# Load user configuration, user components then write calendar
configuration = Configuration('config.yml')
dav_components = get_all_dav_components(configuration.calendars)
with open('calendar.ics', 'wb') as f:
write_calendar_from_dav(f, dav_components)
conf = Configuration('config.yml') # Load user configuration and calendars
dav_components = conf.get_all_dav_components() # Load components
with open('calendar.ics', 'wb') as f: # Write master calendar
write_calendar_from_dav(f, dav_components, conf.selected_groups)
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