-
Notifications
You must be signed in to change notification settings - Fork 7
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
import dateutil | ||
import dateutil.rrule | ||
import ics | ||
import requests | ||
import yaml | ||
from dateutil.tz import gettz | ||
|
||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
|
||
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"] | ||
) | ||
|
@@ -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) | ||
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 | ||
|
||
|
||
|
@@ -199,7 +213,6 @@ | |
raise RuntimeError(f"Error: {f} is not a file") | ||
|
||
calendar = files_to_calendar(files) | ||
|
||
print(calendar.serialize()) | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.