PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 0ms

/dateutil/zoneinfo/__init__.py

http://django-geo.googlecode.com/
Python | 87 lines | 71 code | 10 blank | 6 comment | 21 complexity | afb4c02dda896c31e32b6c61ae60d02c MD5 | raw file
 1"""
 2Copyright (c) 2003-2005  Gustavo Niemeyer <gustavo@niemeyer.net>
 3
 4This module offers extensions to the standard python 2.3+
 5datetime module.
 6"""
 7from dateutil.tz import tzfile
 8from tarfile import TarFile
 9import os
10
11__author__ = "Gustavo Niemeyer <gustavo@niemeyer.net>"
12__license__ = "PSF License"
13
14__all__ = ["setcachesize", "gettz", "rebuild"]
15
16CACHE = []
17CACHESIZE = 10
18
19class tzfile(tzfile):
20    def __reduce__(self):
21        return (gettz, (self._filename,))
22
23def 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    return None
31
32ZONEINFOFILE = getzoneinfofile()
33
34del getzoneinfofile
35
36def setcachesize(size):
37    global CACHESIZE, CACHE
38    CACHESIZE = size
39    del CACHE[size:]
40
41def gettz(name):
42    tzinfo = None
43    if ZONEINFOFILE:
44        for cachedname, tzinfo in CACHE:
45            if cachedname == name:
46                break
47        else:
48            tf = TarFile.open(ZONEINFOFILE)
49            try:
50                zonefile = tf.extractfile(name)
51            except KeyError:
52                tzinfo = None
53            else:
54                tzinfo = tzfile(zonefile)
55            tf.close()
56            CACHE.insert(0, (name, tzinfo))
57            del CACHE[CACHESIZE:]
58    return tzinfo
59
60def rebuild(filename, tag=None, format="gz"):
61    import tempfile, shutil
62    tmpdir = tempfile.mkdtemp()
63    zonedir = os.path.join(tmpdir, "zoneinfo")
64    moduledir = os.path.dirname(__file__)
65    if tag: tag = "-"+tag
66    targetname = "zoneinfo%s.tar.%s" % (tag, format)
67    try:
68        tf = TarFile.open(filename)
69        for name in tf.getnames():
70            if not (name.endswith(".sh") or
71                    name.endswith(".tab") or
72                    name == "leapseconds"):
73                tf.extract(name, tmpdir)
74                filepath = os.path.join(tmpdir, name)
75                os.system("zic -d %s %s" % (zonedir, filepath))
76        tf.close()
77        target = os.path.join(moduledir, targetname)
78        for entry in os.listdir(moduledir):
79            if entry.startswith("zoneinfo") and ".tar." in entry:
80                os.unlink(os.path.join(moduledir, entry))
81        tf = TarFile.open(target, "w:%s" % format)
82        for entry in os.listdir(zonedir):
83            entrypath = os.path.join(zonedir, entry)
84            tf.add(entrypath, entry)
85        tf.close()
86    finally:
87        shutil.rmtree(tmpdir)