| 2 | |
| 3 | |
| 4 | def load_tzdata(key): |
| 5 | from importlib import resources |
| 6 | |
| 7 | components = key.split("/") |
| 8 | package_name = ".".join(["tzdata.zoneinfo"] + components[:-1]) |
| 9 | resource_name = components[-1] |
| 10 | |
| 11 | try: |
| 12 | return resources.files(package_name).joinpath(resource_name).open("rb") |
| 13 | except (ImportError, FileNotFoundError, UnicodeEncodeError): |
| 14 | # There are three types of exception that can be raised that all amount |
| 15 | # to "we cannot find this key": |
| 16 | # |
| 17 | # ImportError: If package_name doesn't exist (e.g. if tzdata is not |
| 18 | # installed, or if there's an error in the folder name like |
| 19 | # Amrica/New_York) |
| 20 | # FileNotFoundError: If resource_name doesn't exist in the package |
| 21 | # (e.g. Europe/Krasnoy) |
| 22 | # UnicodeEncodeError: If package_name or resource_name are not UTF-8, |
| 23 | # such as keys containing a surrogate character. |
| 24 | raise ZoneInfoNotFoundError(f"No time zone found with key {key}") |
| 25 | |
| 26 | |
| 27 | def load_data(fobj): |