Lines 1-6
Link Here
|
1 |
# -*- coding: utf-8 -*- |
1 |
# -*- coding: utf-8 -*- |
2 |
import warnings |
2 |
import warnings |
3 |
import json |
3 |
import json |
|
|
4 |
import os |
4 |
|
5 |
|
5 |
from tarfile import TarFile |
6 |
from tarfile import TarFile |
6 |
from pkgutil import get_data |
7 |
from pkgutil import get_data |
Lines 10-16
from dateutil.tz import tzfile as _tzfile
Link Here
|
10 |
|
11 |
|
11 |
__all__ = ["get_zonefile_instance", "gettz", "gettz_db_metadata"] |
12 |
__all__ = ["get_zonefile_instance", "gettz", "gettz_db_metadata"] |
12 |
|
13 |
|
13 |
ZONEFILENAME = "dateutil-zoneinfo.tar.gz" |
14 |
ZONEDIRECTORY = "/usr/share/zoneinfo" |
14 |
METADATA_FN = 'METADATA' |
15 |
METADATA_FN = 'METADATA' |
15 |
|
16 |
|
16 |
|
17 |
|
Lines 19-30
class tzfile(_tzfile):
Link Here
|
19 |
return (gettz, (self._filename,)) |
20 |
return (gettz, (self._filename,)) |
20 |
|
21 |
|
21 |
|
22 |
|
22 |
def getzoneinfofile_stream(): |
23 |
def iter_zones(topdir): |
23 |
try: |
24 |
for dirpath, dirnames, filenames in os.walk(topdir): |
24 |
return BytesIO(get_data(__name__, ZONEFILENAME)) |
25 |
for f in filenames: |
25 |
except IOError as e: # TODO switch to FileNotFoundError? |
26 |
if f.endswith('.tab'): |
26 |
warnings.warn("I/O error({0}): {1}".format(e.errno, e.strerror)) |
27 |
continue |
27 |
return None |
28 |
fpath = os.path.join(dirpath, f) |
|
|
29 |
relpath = os.path.relpath(fpath, topdir) |
30 |
yield (relpath, tzfile(fpath, filename=relpath)) |
28 |
|
31 |
|
29 |
|
32 |
|
30 |
class ZoneInfoFile(object): |
33 |
class ZoneInfoFile(object): |
Lines 48-54
class ZoneInfoFile(object):
Link Here
|
48 |
# no metadata in tar file |
51 |
# no metadata in tar file |
49 |
self.metadata = None |
52 |
self.metadata = None |
50 |
else: |
53 |
else: |
51 |
self.zones = {} |
54 |
self.zones = dict(iter_zones(ZONEDIRECTORY)) |
52 |
self.metadata = None |
55 |
self.metadata = None |
53 |
|
56 |
|
54 |
def get(self, name, default=None): |
57 |
def get(self, name, default=None): |
Lines 99-105
def get_zonefile_instance(new_instance=False):
Link Here
|
99 |
zif = getattr(get_zonefile_instance, '_cached_instance', None) |
102 |
zif = getattr(get_zonefile_instance, '_cached_instance', None) |
100 |
|
103 |
|
101 |
if zif is None: |
104 |
if zif is None: |
102 |
zif = ZoneInfoFile(getzoneinfofile_stream()) |
105 |
zif = ZoneInfoFile() |
103 |
|
106 |
|
104 |
get_zonefile_instance._cached_instance = zif |
107 |
get_zonefile_instance._cached_instance = zif |
105 |
|
108 |
|
Lines 140-146
def gettz(name):
Link Here
|
140 |
DeprecationWarning) |
143 |
DeprecationWarning) |
141 |
|
144 |
|
142 |
if len(_CLASS_ZONE_INSTANCE) == 0: |
145 |
if len(_CLASS_ZONE_INSTANCE) == 0: |
143 |
_CLASS_ZONE_INSTANCE.append(ZoneInfoFile(getzoneinfofile_stream())) |
146 |
_CLASS_ZONE_INSTANCE.append(ZoneInfoFile()) |
144 |
return _CLASS_ZONE_INSTANCE[0].zones.get(name) |
147 |
return _CLASS_ZONE_INSTANCE[0].zones.get(name) |
145 |
|
148 |
|
146 |
|
149 |
|
Lines 163-167
def gettz_db_metadata():
Link Here
|
163 |
DeprecationWarning) |
166 |
DeprecationWarning) |
164 |
|
167 |
|
165 |
if len(_CLASS_ZONE_INSTANCE) == 0: |
168 |
if len(_CLASS_ZONE_INSTANCE) == 0: |
166 |
_CLASS_ZONE_INSTANCE.append(ZoneInfoFile(getzoneinfofile_stream())) |
169 |
_CLASS_ZONE_INSTANCE.append(ZoneInfoFile()) |
167 |
return _CLASS_ZONE_INSTANCE[0].metadata |
170 |
return _CLASS_ZONE_INSTANCE[0].metadata |
168 |
- |
|
|