| 62 | tzfile = _tzfile |
| 63 | |
| 64 | class ZoneInfoFile: |
| 65 | def __init__(self, zonefile_stream=None): |
| 66 | if zonefile_stream is not None: |
| 67 | self._load_legacy_zonefile_stream(zonefile_stream) |
| 68 | else: |
| 69 | self._eager_load_tzdata() |
| 70 | |
| 71 | def _eager_load_tzdata(self): |
| 72 | try: |
| 73 | import tzdata |
| 74 | except ImportError as e: |
| 75 | raise ImportError( |
| 76 | "The tzdata module is required to use ZoneInfoFile; either add a dependency on tzdata or migrate away from dateutil.zoneinfo." |
| 77 | ) from e |
| 78 | |
| 79 | zone_dict = {} |
| 80 | |
| 81 | for key in _tzdata_impl._load_tzdata_keys(): |
| 82 | with _tzdata_impl._load_tzdata(key) as f: |
| 83 | zone_dict[key] = tzfile(f, key=key) |
| 84 | |
| 85 | self.zones = zone_dict |
| 86 | self.metadata = { |
| 87 | "metadata_version": "3.0", |
| 88 | "tzversion": tzdata.IANA_VERSION, |
| 89 | } |
| 90 | |
| 91 | def _load_legacy_zonefile_stream(self, zonefile_stream): |
| 92 | with TarFile.open(fileobj=zonefile_stream) as tf: |
| 93 | self.zones = { |
| 94 | zf.name: tzfile(tf.extractfile(zf), filename=zf.name) |
| 95 | for zf in tf.getmembers() |
| 96 | if zf.isfile() and zf.name != METADATA_FN |
| 97 | } |
| 98 | # deal with links: They'll point to their parent object. Less |
| 99 | # waste of memory |
| 100 | links = { |
| 101 | zl.name: self.zones[zl.linkname] |
| 102 | for zl in tf.getmembers() |
| 103 | if zl.islnk() or zl.issym() |
| 104 | } |
| 105 | self.zones.update(links) |
| 106 | try: |
| 107 | metadata_json = tf.extractfile(tf.getmember(METADATA_FN)) |
| 108 | metadata_str = metadata_json.read().decode("UTF-8") |
| 109 | self.metadata = json.loads(metadata_str) |
| 110 | except KeyError: |
| 111 | # no metadata in tar file |
| 112 | self.metadata = None |
| 113 | |
| 114 | def get(self, name, default=None): |
| 115 | """ |
| 116 | Wrapper for :func:`ZoneInfoFile.zones.get`. This is a convenience method |
| 117 | for retrieving zones from the zone dictionary. |
| 118 | |
| 119 | :param name: |
| 120 | The name of the zone to retrieve. (Generally IANA zone names) |
| 121 |
no outgoing calls
no test coverage detected