From 62d4824c68821c879785bd3bd231a80483ae34a3 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Sun, 30 Jun 2024 08:49:28 -0700 Subject: [PATCH 1/2] Pre-load all timezone disk reads on import --- ical/tzif/timezoneinfo.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ical/tzif/timezoneinfo.py b/ical/tzif/timezoneinfo.py index 93e8d2c..c15eb1b 100644 --- a/ical/tzif/timezoneinfo.py +++ b/ical/tzif/timezoneinfo.py @@ -22,6 +22,11 @@ _LOGGER = logging.getLogger(__name__) +_IGNORED_TIMEZONES = { + "Asia/Hanoi", # Not in tzdata +} + + class TimezoneInfoError(Exception): """Raised on error working with timezone information.""" @@ -32,6 +37,7 @@ def _read_system_timezones() -> set[str]: return zoneinfo.available_timezones() +@cache def _find_tzfile(key: str) -> str | None: """Retrieve the path to a TZif file from a key.""" for search_path in zoneinfo.TZPATH: @@ -63,6 +69,7 @@ def _iana_key_to_resource(key: str) -> tuple[str, str]: return package, resource +@cache def read(key: str) -> TimezoneInfo: """Read the TZif file from the tzdata package and return timezone records.""" if key not in _read_system_timezones() and key not in _read_tzdata_timezones(): @@ -167,3 +174,12 @@ def read_tzinfo(key: str) -> TzInfo: return TzInfo.from_timezoneinfo(timezoneinfo) except ValueError as err: raise TimezoneInfoError(f"Unable create TzInfo: {key}") from err + + + +# Avoid blocking disk reads in async function by pre-loading all timezone reads +for key in _read_system_timezones(): + try: + read_tzinfo(key) + except TimezoneInfoError: + pass From f9b828fe9f2de9d04860e2478ff6c3ac71807805 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Sun, 30 Jun 2024 08:50:42 -0700 Subject: [PATCH 2/2] Remove dead code --- ical/tzif/timezoneinfo.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ical/tzif/timezoneinfo.py b/ical/tzif/timezoneinfo.py index c15eb1b..0d8577b 100644 --- a/ical/tzif/timezoneinfo.py +++ b/ical/tzif/timezoneinfo.py @@ -22,11 +22,6 @@ _LOGGER = logging.getLogger(__name__) -_IGNORED_TIMEZONES = { - "Asia/Hanoi", # Not in tzdata -} - - class TimezoneInfoError(Exception): """Raised on error working with timezone information.""" @@ -176,7 +171,6 @@ def read_tzinfo(key: str) -> TzInfo: raise TimezoneInfoError(f"Unable create TzInfo: {key}") from err - # Avoid blocking disk reads in async function by pre-loading all timezone reads for key in _read_system_timezones(): try: