Commit 9f2218fb authored by erdnaxe's avatar erdnaxe 🎇

Massive cleanup

parent 3ab30c41
......@@ -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"""
logging.info("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"""
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"""
events, todos, journals = [], [], []
components = []
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
name = cal.get_properties([dav.DisplayName(), ])['{DAV:}displayname']
logging.info("Loading '{}'".format(name))
from_date = datetime.today() - 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"""
f.write(b'BEGIN:VCALENDAR\nPRODID:-//SaphSync//EN\nVERSION:2.0\n')
for dav_component in dav_components:
dav_component.load() # Download CalDav component
ical = icalendar.Event.from_ical(dav_component.data) # Parse ical
ical_components = [c for c in ical.walk() if c.name != 'VCALENDAR']
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):
f.write(ical_component.to_ical())
else:
f.write(ical_component.to_ical())
f.write(b'END:VCALENDAR')
# Configure logging system
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logging.basicConfig(level=logging.INFO)
# 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_dav.load()
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
# If it is in the correct group
if filters.group(event, configuration.selected_groups):
logging.debug('An event was added : {}'.format(event.get('summary')))
calendar.add_component(event)
# Write resulting calendar
dav_components = get_all_dav_components(configuration.calendars)
with open('calendar.ics', 'wb') as f:
f.write(calendar.to_ical())
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