home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2376 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.2 KB  |  100 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from dateutil.tz import tzfile
  5. from tarfile import TarFile
  6. import os
  7. __author__ = 'Gustavo Niemeyer <gustavo@niemeyer.net>'
  8. __license__ = 'PSF License'
  9. __all__ = [
  10.     'setcachesize',
  11.     'gettz',
  12.     'rebuild']
  13. CACHE = []
  14. CACHESIZE = 10
  15.  
  16. class tzfile(tzfile):
  17.     
  18.     def __reduce__(self):
  19.         return (gettz, (self._filename,))
  20.  
  21.  
  22.  
  23. def getzoneinfofile():
  24.     filenames = os.listdir(os.path.join(os.path.dirname(__file__)))
  25.     filenames.sort()
  26.     filenames.reverse()
  27.     for entry in filenames:
  28.         if entry.startswith('zoneinfo') and '.tar.' in entry:
  29.             return os.path.join(os.path.dirname(__file__), entry)
  30.     
  31.  
  32. ZONEINFOFILE = getzoneinfofile()
  33. del getzoneinfofile
  34.  
  35. def setcachesize(size):
  36.     global CACHESIZE
  37.     CACHESIZE = size
  38.     del CACHE[size:]
  39.  
  40.  
  41. def gettz(name):
  42.     tzinfo = None
  43.     if ZONEINFOFILE:
  44.         for cachedname, tzinfo in CACHE:
  45.             if cachedname == name:
  46.                 break
  47.                 continue
  48.         else:
  49.             tf = TarFile.open(ZONEINFOFILE)
  50.             
  51.             try:
  52.                 zonefile = tf.extractfile(name)
  53.             except KeyError:
  54.                 tzinfo = None
  55.  
  56.             tzinfo = tzfile(zonefile)
  57.             CACHE.insert(0, (name, tzinfo))
  58.             del CACHE[CACHESIZE:]
  59.     
  60.     return tzinfo
  61.  
  62.  
  63. def rebuild(filename, tag = None, format = 'gz'):
  64.     import tempfile
  65.     import shutil
  66.     tmpdir = tempfile.mkdtemp()
  67.     zonedir = os.path.join(tmpdir, 'zoneinfo')
  68.     moduledir = os.path.dirname(__file__)
  69.     if tag:
  70.         tag = '-' + tag
  71.     
  72.     targetname = 'zoneinfo%s.tar.%s' % (tag, format)
  73.     
  74.     try:
  75.         tf = TarFile.open(filename)
  76.         for name in tf.getnames():
  77.             if not name.endswith('.sh') and name.endswith('.tab') or name == 'leapseconds':
  78.                 tf.extract(name, tmpdir)
  79.                 filepath = os.path.join(tmpdir, name)
  80.                 os.system('zic -d %s %s' % (zonedir, filepath))
  81.                 continue
  82.         
  83.         tf.close()
  84.         target = os.path.join(moduledir, targetname)
  85.         for entry in os.listdir(moduledir):
  86.             if entry.startswith('zoneinfo') and '.tar.' in entry:
  87.                 os.unlink(os.path.join(moduledir, entry))
  88.                 continue
  89.         
  90.         tf = TarFile.open(target, 'w:%s' % format)
  91.         for entry in os.listdir(zonedir):
  92.             entrypath = os.path.join(zonedir, entry)
  93.             tf.add(entrypath, entry)
  94.         
  95.         tf.close()
  96.     finally:
  97.         shutil.rmtree(tmpdir)
  98.  
  99.  
  100.