Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process existing ics files from URL #71

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies = [
"ics == 0.8.0.dev0",
"python-dateutil >= 2.8",
"pyyaml >= 6",
"requests",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not add an extra dependency to requests or another HTTP library as the needs are very limited. urllib.request should be more than enough for that.

]

[project.optional-dependencies]
Expand Down
31 changes: 22 additions & 9 deletions yaml2ics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import dateutil
import dateutil.rrule
import ics
import requests
import yaml
from dateutil.tz import gettz

Expand Down Expand Up @@ -146,7 +147,7 @@
return cal


def files_to_events(files: list) -> (ics.Calendar, str):
def files_to_events(files: list) -> (ics.Calendar, str, bool):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some doc about that somewhere? The other parameters/return values were sort of ok to guess but this is less so to me.

"""Process files to a list of events"""
all_events = []
name = None
Expand All @@ -156,11 +157,19 @@
calendar_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
else:
calendar_yaml = yaml.load(open(f), Loader=yaml.FullLoader)

external = calendar_yaml.get("ics", None)
if external is not None:
# This is an existing ics file that we have to download
name = calendar_yaml.get("name", name)
all_events = requests.get(external).text
return all_events, name, True

Check warning on line 166 in yaml2ics.py

View check run for this annotation

Codecov / codecov/patch

yaml2ics.py#L164-L166

Added lines #L164 - L166 were not covered by tests

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(
included_events, _name, _ = files_to_events(
os.path.join(os.path.dirname(f), newfile)
for newfile in calendar_yaml["include"]
)
Expand All @@ -172,17 +181,22 @@
# keep the last one we find
name = calendar_yaml.get("name", name)

return all_events, name
return all_events, name, False


def files_to_calendar(files: list) -> ics.Calendar:
"""'main function: list of files to our result"""
all_events, name = files_to_events(files)
all_events, name, is_external = files_to_events(files)

if is_external:
# Existing external ics file
calendar = ics.Calendar(all_events)

Check warning on line 193 in yaml2ics.py

View check run for this annotation

Codecov / codecov/patch

yaml2ics.py#L193

Added line #L193 was not covered by tests
else:
calendar = events_to_calendar(all_events)
if name is not None:
calendar.extra.append(ics.ContentLine(name="NAME", value=name))
calendar.extra.append(ics.ContentLine(name="X-WR-CALNAME", value=name))

calendar = events_to_calendar(all_events)
if name is not None:
calendar.extra.append(ics.ContentLine(name="NAME", value=name))
calendar.extra.append(ics.ContentLine(name="X-WR-CALNAME", value=name))
return calendar


Expand All @@ -199,7 +213,6 @@
raise RuntimeError(f"Error: {f} is not a file")

calendar = files_to_calendar(files)

print(calendar.serialize())


Expand Down