NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

__init__.py (3145B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\dateutil\zoneinfo\__init__.py
      2 import logging
      3 import os
      4 import warnings
      5 import tempfile
      6 import shutil
      7 import json
      8 from tarfile import TarFile
      9 from pkgutil import get_data
     10 from io import BytesIO
     11 from contextlib import closing
     12 from dateutil.tz import tzfile
     13 __all__ = ['get_zonefile_instance',
     14  'gettz',
     15  'gettz_db_metadata',
     16  'rebuild']
     17 ZONEFILENAME = 'dateutil-zoneinfo.tar.gz'
     18 METADATA_FN = 'METADATA'
     19 tar_open = TarFile.open
     20 if not hasattr(TarFile, '__exit__'):
     21 
     22     def tar_open(*args, **kwargs):
     23         return closing(TarFile.open(*args, **kwargs))
     24 
     25 
     26 class tzfile(tzfile):
     27 
     28     def __reduce__(self):
     29         return (gettz, (self._filename,))
     30 
     31 
     32 def getzoneinfofile_stream():
     33     try:
     34         return BytesIO(get_data(__name__, ZONEFILENAME))
     35     except IOError as e:
     36         warnings.warn('I/O error({0}): {1}'.format(e.errno, e.strerror))
     37         return None
     38 
     39 
     40 class ZoneInfoFile(object):
     41 
     42     def __init__(self, zonefile_stream = None):
     43         if zonefile_stream is not None:
     44             with tar_open(fileobj=zonefile_stream, mode='r') as tf:
     45                 self.zones = dict(((zf.name, tzfile(tf.extractfile(zf), filename=zf.name)) for zf in tf.getmembers() if zf.isfile() and zf.name != METADATA_FN))
     46                 links = dict(((zl.name, self.zones[zl.linkname]) for zl in tf.getmembers() if zl.islnk() or zl.issym()))
     47                 self.zones.update(links)
     48                 try:
     49                     metadata_json = tf.extractfile(tf.getmember(METADATA_FN))
     50                     metadata_str = metadata_json.read().decode('UTF-8')
     51                     self.metadata = json.loads(metadata_str)
     52                 except KeyError:
     53                     self.metadata = None
     54 
     55         else:
     56             self.zones = dict()
     57             self.metadata = None
     58 
     59     def get(self, name, default = None):
     60         return self.zones.get(name, default)
     61 
     62 
     63 _CLASS_ZONE_INSTANCE = list()
     64 
     65 def get_zonefile_instance(new_instance = False):
     66     if new_instance:
     67         zif = None
     68     else:
     69         zif = getattr(get_zonefile_instance, '_cached_instance', None)
     70     if zif is None:
     71         zif = ZoneInfoFile(getzoneinfofile_stream())
     72         get_zonefile_instance._cached_instance = zif
     73     return zif
     74 
     75 
     76 def gettz(name):
     77     warnings.warn('zoneinfo.gettz() will be removed in future versions, to use the dateutil-provided zoneinfo files, instantiate a ZoneInfoFile object and use ZoneInfoFile.zones.get() instead. See the documentation for details.', DeprecationWarning)
     78     if len(_CLASS_ZONE_INSTANCE) == 0:
     79         _CLASS_ZONE_INSTANCE.append(ZoneInfoFile(getzoneinfofile_stream()))
     80     return _CLASS_ZONE_INSTANCE[0].zones.get(name)
     81 
     82 
     83 def gettz_db_metadata():
     84     warnings.warn("zoneinfo.gettz_db_metadata() will be removed in future versions, to use the dateutil-provided zoneinfo files, ZoneInfoFile object and query the 'metadata' attribute instead. See the documentation for details.", DeprecationWarning)
     85     if len(_CLASS_ZONE_INSTANCE) == 0:
     86         _CLASS_ZONE_INSTANCE.append(ZoneInfoFile(getzoneinfofile_stream()))
     87     return _CLASS_ZONE_INSTANCE[0].metadata