-
-
Notifications
You must be signed in to change notification settings - Fork 175
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
Add from_file and to_file method #757
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
from datetime import date, datetime, timedelta, tzinfo | ||
from typing import List, Optional, Tuple | ||
from datetime import date, datetime, timedelta | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING, List, NamedTuple, Optional, Tuple, Union | ||
|
||
import dateutil.rrule | ||
|
@@ -587,6 +588,74 @@ def is_thunderbird(self) -> bool: | |
return any(attr.startswith("X-MOZ-") for attr in self.keys()) | ||
|
||
|
||
@classmethod | ||
def from_file(cls, file: Union[str, Path], multiple: bool = False): | ||
"""Create a Component from a file. | ||
|
||
This class method can be used by any Component subclass (Calendar, Event, etc.) | ||
to read their data from a file. | ||
|
||
Args: | ||
file: The file to read from. Can be: | ||
- A string path to a file | ||
- A Path object | ||
multiple: If True, allows parsing multiple components from the file. | ||
|
||
Returns: | ||
If multiple=False (default): | ||
A single Component instance of the appropriate type | ||
If multiple=True: | ||
A list of Component instances | ||
|
||
Raises: | ||
FileNotFoundError: If the file path doesn't exist | ||
ValueError: If the file contents are not valid iCalendar format | ||
|
||
Example: | ||
>>> from icalendar import Calendar | ||
>>> # Read a calendar file | ||
>>> cal = Calendar.from_file("src/icalendar/tests/calendars/example.ics") | ||
>>> # Read multiple calendars | ||
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. You can remove the >>> here 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. Are you suggestion to remove the 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. 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. if I remove the
This does work:
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. It needs a new line:
Otherwise the comment is considered output. |
||
>>> cals = Calendar.from_file("src/icalendar/tests/calendars/multiple_calendar_components.ics", multiple=True) | ||
""" | ||
# Handle string path by converting to Path | ||
if isinstance(file, str): | ||
file = Path(file) | ||
|
||
return cls.from_ical(file.read_bytes(), multiple=multiple) | ||
|
||
def to_file(self, file: Union[str, Path], sorted: bool = True) -> None: | ||
"""Write the component to a file. | ||
|
||
This method can be used by any Component subclass (Calendar, Event, etc.) | ||
to write their data to a file. | ||
|
||
Args: | ||
file: Where to write the component. Can be: | ||
- A string path to a file | ||
- A Path object | ||
sorted: Whether parameters and properties should be lexicographically sorted. | ||
|
||
Example: | ||
>>> from icalendar import Calendar | ||
>>> from pathlib import Path | ||
>>> # Read a calendar file | ||
>>> cal = Calendar.from_file("src/icalendar/tests/calendars/example.ics") | ||
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. The examples here seem to be the same as above. You can use a temporary file for this. 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. I changed the examples to use the |
||
>>> # or pass a Path object | ||
>>> path = Path("src/icalendar/tests/calendars/example.ics") | ||
>>> cal = Calendar.from_file(path) | ||
>>> # Read multiple calendars | ||
>>> cals = Calendar.from_file("src/icalendar/tests/calendars/multiple_calendar_components.ics", multiple=True) | ||
|
||
""" | ||
|
||
# Handle string path | ||
if isinstance(file, str): | ||
file = Path(file) | ||
|
||
file.write_bytes(self.to_ical(sorted=sorted)) | ||
|
||
|
||
|
||
####################################### | ||
# components defined in RFC 5545 | ||
|
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.