Skip to content

Commit

Permalink
git_calendar/yaml2ics: Allow include to include from a url ICS
Browse files Browse the repository at this point in the history
- Allows to include from a remote ICS file
- Remote YAML is harder since it requires being able to include other
  files (which I am not doing yet)
  • Loading branch information
rkdarst committed Jun 3, 2024
1 parent 71507d3 commit 7374376
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions git_calendar/yaml2ics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import datetime
import os
import sys
import urllib.request

import dateutil
import dateutil.rrule
Expand Down Expand Up @@ -182,23 +183,30 @@ def events_to_calendar(events: list) -> str:
return cal


def files_to_events(files: list) -> (ics.Calendar, str):
def files_to_events(files: list, dirname:str='') -> (ics.Calendar, str):
"""Process files to a list of events"""
all_events = []
name = None

for f in files:
# If it is a raw ICS file
if isinstance(f, str) and f.endswith('.ics'):
calendar = ics.Calendar(urllib.request.urlopen(f).read().decode())
all_events.extend(calendar.events)
continue

# If it is a YAML file:
if hasattr(f, "read"):
calendar_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
else:
calendar_yaml = yaml.load(open(f), Loader=yaml.FullLoader)
calendar_yaml = yaml.load(open(os.path.join(dirname, f)), Loader=yaml.FullLoader)
tz = calendar_yaml.get("timezone", None)
if tz is not None:
tz = gettz(tz)
if "include" in calendar_yaml:
included_events, _name = files_to_events(
os.path.join(os.path.dirname(f), newfile)
for newfile in calendar_yaml["include"]
(newfile for newfile in calendar_yaml["include"]),
dirname=os.path.join(os.path.dirname(f))
)
all_events.extend(included_events)
for event in calendar_yaml.get("events", []):
Expand Down

0 comments on commit 7374376

Please sign in to comment.