Skip to content

The client

When you are looking to integrate iCal-library with your project, the client is the first place to look. If you have a service that periodically restarts, you might want to take a look at the CacheClient.

client

parse_icalendar_file(file)

Parse an iCalendar file and return a parsed VCalendar instance.

Parameters:

Name Type Description Default
file Union[str, Path]

A file on the local filesystem that contains the icalendar definition.

required

Returns:

Type Description
VCalendar

a VCalendar instance with all it's iCalendar components like VEvents, VToDos, VTimeZones etc.

Source code in ical_library/client.py
27
28
29
30
31
32
33
34
def parse_icalendar_file(file: Union[str, Path]) -> VCalendar:
    """
    Parse an iCalendar file and return a parsed VCalendar instance.
    :param file: A file on the local filesystem that contains the icalendar definition.
    :return: a VCalendar instance with all it's iCalendar components like VEvents, VToDos, VTimeZones etc.
    """
    with open(file, "r") as ical_file:
        return parse_lines_into_calendar(ical_file.read())

parse_icalendar_url(url, **kwargs)

Given a URL to an iCalendar file, return a parsed VCalendar instance.

Parameters:

Name Type Description Default
url str

The URL to the iCalendar file.

required
kwargs Any

Any keyword arguments to pass onto the urllib.request.urlopen call.

{}

Returns:

Type Description
VCalendar

a VCalendar instance with all it's iCalendar components like VEvents, VToDos, VTimeZones etc.

Source code in ical_library/client.py
37
38
39
40
41
42
43
44
45
46
def parse_icalendar_url(url: str, **kwargs: Any) -> VCalendar:
    """
    Given a URL to an iCalendar file, return a parsed VCalendar instance.
    :param url: The URL to the iCalendar file.
    :param kwargs: Any keyword arguments to pass onto the `urllib.request.urlopen` call.
    :return: a VCalendar instance with all it's iCalendar components like VEvents, VToDos, VTimeZones etc.
    """
    response = request.urlopen(url, **kwargs)
    text = response.read().decode("utf-8")
    return parse_lines_into_calendar(text)

parse_lines_into_calendar(raw_text)

Given the lines of an iCalendar file, return a parsed VCalendar instance.

Parameters:

Name Type Description Default
raw_text str

The raw text of the iCalendar file/website.

required

Returns:

Type Description
VCalendar

a VCalendar with all it's iCalendar components like VEvents, VToDos, VTimeZones etc.

Source code in ical_library/client.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def parse_lines_into_calendar(raw_text: str) -> VCalendar:
    """
    Given the lines of an iCalendar file, return a parsed VCalendar instance.
    :param raw_text: The raw text of the iCalendar file/website.
    :return: a VCalendar with all it's iCalendar components like VEvents, VToDos, VTimeZones etc.
    """
    lines: List[str] = []
    for line in re.split(r"\n", raw_text):
        line_content = re.sub(r"^\n|^\r|\n$|\r$", "", line)
        if len(line_content) > 0:
            lines.append(line_content)
    new_instance = VCalendar()
    if lines[0] != "BEGIN:VCALENDAR":
        raise ValueError(f"This is not a ICalendar as it started with {lines[0]=}.")
    new_instance.parse_component(lines, line_number=1)
    return new_instance