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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __author__ = 'Gustavo Niemeyer <gustavo@niemeyer.net>'
  5. __license__ = 'PSF License'
  6. import datetime
  7. import struct
  8. import time
  9. import sys
  10. import os
  11. relativedelta = None
  12. parser = None
  13. rrule = None
  14. __all__ = [
  15.     'tzutc',
  16.     'tzoffset',
  17.     'tzlocal',
  18.     'tzfile',
  19.     'tzrange',
  20.     'tzstr',
  21.     'tzical',
  22.     'tzwin',
  23.     'tzwinlocal',
  24.     'gettz']
  25.  
  26. try:
  27.     from dateutil.tzwin import tzwin, tzwinlocal
  28. except (ImportError, OSError):
  29.     (tzwin, tzwinlocal) = (None, None)
  30.  
  31. ZERO = datetime.timedelta(0)
  32. EPOCHORDINAL = datetime.datetime.utcfromtimestamp(0).toordinal()
  33.  
  34. class tzutc(datetime.tzinfo):
  35.     
  36.     def utcoffset(self, dt):
  37.         return ZERO
  38.  
  39.     
  40.     def dst(self, dt):
  41.         return ZERO
  42.  
  43.     
  44.     def tzname(self, dt):
  45.         return 'UTC'
  46.  
  47.     
  48.     def __eq__(self, other):
  49.         if isinstance(other, tzutc) and isinstance(other, tzoffset):
  50.             pass
  51.         return other._offset == ZERO
  52.  
  53.     
  54.     def __ne__(self, other):
  55.         return not self.__eq__(other)
  56.  
  57.     
  58.     def __repr__(self):
  59.         return '%s()' % self.__class__.__name__
  60.  
  61.     __reduce__ = object.__reduce__
  62.  
  63.  
  64. class tzoffset(datetime.tzinfo):
  65.     
  66.     def __init__(self, name, offset):
  67.         self._name = name
  68.         self._offset = datetime.timedelta(seconds = offset)
  69.  
  70.     
  71.     def utcoffset(self, dt):
  72.         return self._offset
  73.  
  74.     
  75.     def dst(self, dt):
  76.         return ZERO
  77.  
  78.     
  79.     def tzname(self, dt):
  80.         return self._name
  81.  
  82.     
  83.     def __eq__(self, other):
  84.         if isinstance(other, tzoffset):
  85.             pass
  86.         return self._offset == other._offset
  87.  
  88.     
  89.     def __ne__(self, other):
  90.         return not self.__eq__(other)
  91.  
  92.     
  93.     def __repr__(self):
  94.         return '%s(%s, %s)' % (self.__class__.__name__, `self._name`, self._offset.days * 86400 + self._offset.seconds)
  95.  
  96.     __reduce__ = object.__reduce__
  97.  
  98.  
  99. class tzlocal(datetime.tzinfo):
  100.     _std_offset = datetime.timedelta(seconds = -(time.timezone))
  101.     if time.daylight:
  102.         _dst_offset = datetime.timedelta(seconds = -(time.altzone))
  103.     else:
  104.         _dst_offset = _std_offset
  105.     
  106.     def utcoffset(self, dt):
  107.         if self._isdst(dt):
  108.             return self._dst_offset
  109.         return self._std_offset
  110.  
  111.     
  112.     def dst(self, dt):
  113.         if self._isdst(dt):
  114.             return self._dst_offset - self._std_offset
  115.         return ZERO
  116.  
  117.     
  118.     def tzname(self, dt):
  119.         return time.tzname[self._isdst(dt)]
  120.  
  121.     
  122.     def _isdst(self, dt):
  123.         timestamp = (dt.toordinal() - EPOCHORDINAL) * 86400 + dt.hour * 3600 + dt.minute * 60 + dt.second
  124.         return time.localtime(timestamp + time.timezone).tm_isdst
  125.  
  126.     
  127.     def __eq__(self, other):
  128.         if not isinstance(other, tzlocal):
  129.             return False
  130.         if self._std_offset == other._std_offset:
  131.             pass
  132.         return self._dst_offset == other._dst_offset
  133.  
  134.     
  135.     def __ne__(self, other):
  136.         return not self.__eq__(other)
  137.  
  138.     
  139.     def __repr__(self):
  140.         return '%s()' % self.__class__.__name__
  141.  
  142.     __reduce__ = object.__reduce__
  143.  
  144.  
  145. class _ttinfo(object):
  146.     __slots__ = [
  147.         'offset',
  148.         'delta',
  149.         'isdst',
  150.         'abbr',
  151.         'isstd',
  152.         'isgmt']
  153.     
  154.     def __init__(self):
  155.         for attr in self.__slots__:
  156.             setattr(self, attr, None)
  157.         
  158.  
  159.     
  160.     def __repr__(self):
  161.         l = []
  162.         for attr in self.__slots__:
  163.             value = getattr(self, attr)
  164.             if value is not None:
  165.                 l.append('%s=%s' % (attr, `value`))
  166.                 continue
  167.         
  168.         return '%s(%s)' % (self.__class__.__name__, ', '.join(l))
  169.  
  170.     
  171.     def __eq__(self, other):
  172.         if not isinstance(other, _ttinfo):
  173.             return False
  174.         if self.offset == other.offset and self.delta == other.delta and self.isdst == other.isdst and self.abbr == other.abbr and self.isstd == other.isstd:
  175.             pass
  176.         return self.isgmt == other.isgmt
  177.  
  178.     
  179.     def __ne__(self, other):
  180.         return not self.__eq__(other)
  181.  
  182.     
  183.     def __getstate__(self):
  184.         state = { }
  185.         for name in self.__slots__:
  186.             state[name] = getattr(self, name, None)
  187.         
  188.         return state
  189.  
  190.     
  191.     def __setstate__(self, state):
  192.         for name in self.__slots__:
  193.             if name in state:
  194.                 setattr(self, name, state[name])
  195.                 continue
  196.         
  197.  
  198.  
  199.  
  200. class tzfile(datetime.tzinfo):
  201.     
  202.     def __init__(self, fileobj):
  203.         if isinstance(fileobj, basestring):
  204.             self._filename = fileobj
  205.             fileobj = open(fileobj)
  206.         elif hasattr(fileobj, 'name'):
  207.             self._filename = fileobj.name
  208.         else:
  209.             self._filename = `fileobj`
  210.         if fileobj.read(4) != 'TZif':
  211.             raise ValueError, 'magic not found'
  212.         fileobj.read(4) != 'TZif'
  213.         fileobj.read(16)
  214.         (ttisgmtcnt, ttisstdcnt, leapcnt, timecnt, typecnt, charcnt) = struct.unpack('>6l', fileobj.read(24))
  215.         if timecnt:
  216.             self._trans_list = struct.unpack('>%dl' % timecnt, fileobj.read(timecnt * 4))
  217.         else:
  218.             self._trans_list = []
  219.         if timecnt:
  220.             self._trans_idx = struct.unpack('>%dB' % timecnt, fileobj.read(timecnt))
  221.         else:
  222.             self._trans_idx = []
  223.         ttinfo = []
  224.         for i in range(typecnt):
  225.             ttinfo.append(struct.unpack('>lbb', fileobj.read(6)))
  226.         
  227.         abbr = fileobj.read(charcnt)
  228.         if leapcnt:
  229.             leap = struct.unpack('>%dl' % leapcnt * 2, fileobj.read(leapcnt * 8))
  230.         
  231.         if ttisstdcnt:
  232.             isstd = struct.unpack('>%db' % ttisstdcnt, fileobj.read(ttisstdcnt))
  233.         
  234.         if ttisgmtcnt:
  235.             isgmt = struct.unpack('>%db' % ttisgmtcnt, fileobj.read(ttisgmtcnt))
  236.         
  237.         self._ttinfo_list = []
  238.         for i in range(typecnt):
  239.             (gmtoff, isdst, abbrind) = ttinfo[i]
  240.             gmtoff = ((gmtoff + 30) // 60) * 60
  241.             tti = _ttinfo()
  242.             tti.offset = gmtoff
  243.             tti.delta = datetime.timedelta(seconds = gmtoff)
  244.             tti.isdst = isdst
  245.             tti.abbr = abbr[abbrind:abbr.find('\x00', abbrind)]
  246.             if ttisstdcnt > i:
  247.                 pass
  248.             tti.isstd = isstd[i] != 0
  249.             if ttisgmtcnt > i:
  250.                 pass
  251.             tti.isgmt = isgmt[i] != 0
  252.             self._ttinfo_list.append(tti)
  253.         
  254.         trans_idx = []
  255.         for idx in self._trans_idx:
  256.             trans_idx.append(self._ttinfo_list[idx])
  257.         
  258.         self._trans_idx = tuple(trans_idx)
  259.         self._ttinfo_std = None
  260.         self._ttinfo_dst = None
  261.         self._ttinfo_before = None
  262.         if self._ttinfo_list:
  263.             if not self._trans_list:
  264.                 self._ttinfo_std = self._ttinfo_first = self._ttinfo_list[0]
  265.             else:
  266.                 for i in range(timecnt - 1, -1, -1):
  267.                     tti = self._trans_idx[i]
  268.                     if not (self._ttinfo_std) and not (tti.isdst):
  269.                         self._ttinfo_std = tti
  270.                     elif not (self._ttinfo_dst) and tti.isdst:
  271.                         self._ttinfo_dst = tti
  272.                     
  273.                     if self._ttinfo_std and self._ttinfo_dst:
  274.                         break
  275.                         continue
  276.                 elif self._ttinfo_dst and not (self._ttinfo_std):
  277.                     self._ttinfo_std = self._ttinfo_dst
  278.                 
  279.                 for tti in self._ttinfo_list:
  280.                     if not tti.isdst:
  281.                         self._ttinfo_before = tti
  282.                         break
  283.                         continue
  284.                 else:
  285.                     self._ttinfo_before = self._ttinfo_list[0]
  286.         
  287.         laststdoffset = 0
  288.         self._trans_list = list(self._trans_list)
  289.         for i in range(len(self._trans_list)):
  290.             tti = self._trans_idx[i]
  291.             if not tti.isdst:
  292.                 self._trans_list[i] += tti.offset
  293.                 laststdoffset = tti.offset
  294.                 continue
  295.             self._trans_list[i] += laststdoffset
  296.         
  297.         self._trans_list = tuple(self._trans_list)
  298.  
  299.     
  300.     def _find_ttinfo(self, dt, laststd = 0):
  301.         timestamp = (dt.toordinal() - EPOCHORDINAL) * 86400 + dt.hour * 3600 + dt.minute * 60 + dt.second
  302.         idx = 0
  303.         for trans in self._trans_list:
  304.             if timestamp < trans:
  305.                 break
  306.             
  307.             idx += 1
  308.         else:
  309.             return self._ttinfo_std
  310.         if None == 0:
  311.             return self._ttinfo_before
  312.         if laststd:
  313.             while idx > 0:
  314.                 tti = self._trans_idx[idx - 1]
  315.                 if not tti.isdst:
  316.                     return tti
  317.                 idx -= 1
  318.                 continue
  319.                 tti.isdst
  320.             return self._ttinfo_std
  321.         laststd
  322.         return self._trans_idx[idx - 1]
  323.  
  324.     
  325.     def utcoffset(self, dt):
  326.         if not self._ttinfo_std:
  327.             return ZERO
  328.         return self._find_ttinfo(dt).delta
  329.  
  330.     
  331.     def dst(self, dt):
  332.         if not self._ttinfo_dst:
  333.             return ZERO
  334.         tti = self._find_ttinfo(dt)
  335.         if not tti.isdst:
  336.             return ZERO
  337.         return tti.delta - self._find_ttinfo(dt, laststd = 1).delta
  338.  
  339.     
  340.     def tzname(self, dt):
  341.         if not self._ttinfo_std:
  342.             return None
  343.         return self._find_ttinfo(dt).abbr
  344.  
  345.     
  346.     def __eq__(self, other):
  347.         if not isinstance(other, tzfile):
  348.             return False
  349.         if self._trans_list == other._trans_list and self._trans_idx == other._trans_idx:
  350.             pass
  351.         return self._ttinfo_list == other._ttinfo_list
  352.  
  353.     
  354.     def __ne__(self, other):
  355.         return not self.__eq__(other)
  356.  
  357.     
  358.     def __repr__(self):
  359.         return '%s(%s)' % (self.__class__.__name__, `self._filename`)
  360.  
  361.     
  362.     def __reduce__(self):
  363.         if not os.path.isfile(self._filename):
  364.             raise ValueError, 'Unpickable %s class' % self.__class__.__name__
  365.         os.path.isfile(self._filename)
  366.         return (self.__class__, (self._filename,))
  367.  
  368.  
  369.  
  370. class tzrange(datetime.tzinfo):
  371.     
  372.     def __init__(self, stdabbr, stdoffset = None, dstabbr = None, dstoffset = None, start = None, end = None):
  373.         global relativedelta
  374.         if not relativedelta:
  375.             relativedelta = relativedelta
  376.             import dateutil
  377.         
  378.         self._std_abbr = stdabbr
  379.         self._dst_abbr = dstabbr
  380.         if stdoffset is not None:
  381.             self._std_offset = datetime.timedelta(seconds = stdoffset)
  382.         else:
  383.             self._std_offset = ZERO
  384.         if dstoffset is not None:
  385.             self._dst_offset = datetime.timedelta(seconds = dstoffset)
  386.         elif dstabbr and stdoffset is not None:
  387.             self._dst_offset = self._std_offset + datetime.timedelta(hours = +1)
  388.         else:
  389.             self._dst_offset = ZERO
  390.         if dstabbr and start is None:
  391.             self._start_delta = relativedelta.relativedelta(hours = +2, month = 4, day = 1, weekday = relativedelta.SU(+1))
  392.         else:
  393.             self._start_delta = start
  394.         if dstabbr and end is None:
  395.             self._end_delta = relativedelta.relativedelta(hours = +1, month = 10, day = 31, weekday = relativedelta.SU(-1))
  396.         else:
  397.             self._end_delta = end
  398.  
  399.     
  400.     def utcoffset(self, dt):
  401.         if self._isdst(dt):
  402.             return self._dst_offset
  403.         return self._std_offset
  404.  
  405.     
  406.     def dst(self, dt):
  407.         if self._isdst(dt):
  408.             return self._dst_offset - self._std_offset
  409.         return ZERO
  410.  
  411.     
  412.     def tzname(self, dt):
  413.         if self._isdst(dt):
  414.             return self._dst_abbr
  415.         return self._std_abbr
  416.  
  417.     
  418.     def _isdst(self, dt):
  419.         if not self._start_delta:
  420.             return False
  421.         year = datetime.datetime(dt.year, 1, 1)
  422.         start = year + self._start_delta
  423.         end = year + self._end_delta
  424.         dt = dt.replace(tzinfo = None)
  425.         if start < end:
  426.             if dt >= start:
  427.                 pass
  428.             return dt < end
  429.         if not dt >= start:
  430.             pass
  431.         return dt < end
  432.  
  433.     
  434.     def __eq__(self, other):
  435.         if not isinstance(other, tzrange):
  436.             return False
  437.         if self._std_abbr == other._std_abbr and self._dst_abbr == other._dst_abbr and self._std_offset == other._std_offset and self._dst_offset == other._dst_offset and self._start_delta == other._start_delta:
  438.             pass
  439.         return self._end_delta == other._end_delta
  440.  
  441.     
  442.     def __ne__(self, other):
  443.         return not self.__eq__(other)
  444.  
  445.     
  446.     def __repr__(self):
  447.         return '%s(...)' % self.__class__.__name__
  448.  
  449.     __reduce__ = object.__reduce__
  450.  
  451.  
  452. class tzstr(tzrange):
  453.     
  454.     def __init__(self, s):
  455.         global parser
  456.         if not parser:
  457.             parser = parser
  458.             import dateutil
  459.         
  460.         self._s = s
  461.         res = parser._parsetz(s)
  462.         if res is None:
  463.             raise ValueError, 'unknown string format'
  464.         res is None
  465.         if res.stdabbr in ('GMT', 'UTC'):
  466.             res.stdoffset *= -1
  467.         
  468.         tzrange.__init__(self, res.stdabbr, res.stdoffset, res.dstabbr, res.dstoffset, start = False, end = False)
  469.         if not res.dstabbr:
  470.             self._start_delta = None
  471.             self._end_delta = None
  472.         else:
  473.             self._start_delta = self._delta(res.start)
  474.             if self._start_delta:
  475.                 self._end_delta = self._delta(res.end, isend = 1)
  476.             
  477.  
  478.     
  479.     def _delta(self, x, isend = 0):
  480.         kwargs = { }
  481.         if x.month is not None:
  482.             kwargs['month'] = x.month
  483.             if x.weekday is not None:
  484.                 kwargs['weekday'] = relativedelta.weekday(x.weekday, x.week)
  485.                 if x.week > 0:
  486.                     kwargs['day'] = 1
  487.                 else:
  488.                     kwargs['day'] = 31
  489.             elif x.day:
  490.                 kwargs['day'] = x.day
  491.             
  492.         elif x.yday is not None:
  493.             kwargs['yearday'] = x.yday
  494.         elif x.jyday is not None:
  495.             kwargs['nlyearday'] = x.jyday
  496.         
  497.         if not kwargs:
  498.             if not isend:
  499.                 kwargs['month'] = 4
  500.                 kwargs['day'] = 1
  501.                 kwargs['weekday'] = relativedelta.SU(+1)
  502.             else:
  503.                 kwargs['month'] = 10
  504.                 kwargs['day'] = 31
  505.                 kwargs['weekday'] = relativedelta.SU(-1)
  506.         
  507.         if x.time is not None:
  508.             kwargs['seconds'] = x.time
  509.         else:
  510.             kwargs['seconds'] = 7200
  511.         if isend:
  512.             delta = self._dst_offset - self._std_offset
  513.             kwargs['seconds'] -= delta.seconds + delta.days * 86400
  514.         
  515.         return relativedelta.relativedelta(**kwargs)
  516.  
  517.     
  518.     def __repr__(self):
  519.         return '%s(%s)' % (self.__class__.__name__, `self._s`)
  520.  
  521.  
  522.  
  523. class _tzicalvtzcomp:
  524.     
  525.     def __init__(self, tzoffsetfrom, tzoffsetto, isdst, tzname = None, rrule = None):
  526.         self.tzoffsetfrom = datetime.timedelta(seconds = tzoffsetfrom)
  527.         self.tzoffsetto = datetime.timedelta(seconds = tzoffsetto)
  528.         self.tzoffsetdiff = self.tzoffsetto - self.tzoffsetfrom
  529.         self.isdst = isdst
  530.         self.tzname = tzname
  531.         self.rrule = rrule
  532.  
  533.  
  534.  
  535. class _tzicalvtz(datetime.tzinfo):
  536.     
  537.     def __init__(self, tzid, comps = []):
  538.         self._tzid = tzid
  539.         self._comps = comps
  540.         self._cachedate = []
  541.         self._cachecomp = []
  542.  
  543.     
  544.     def _find_comp(self, dt):
  545.         if len(self._comps) == 1:
  546.             return self._comps[0]
  547.         dt = dt.replace(tzinfo = None)
  548.         
  549.         try:
  550.             return self._cachecomp[self._cachedate.index(dt)]
  551.         except ValueError:
  552.             len(self._comps) == 1
  553.             len(self._comps) == 1
  554.         except:
  555.             len(self._comps) == 1
  556.  
  557.         lastcomp = None
  558.         lastcompdt = None
  559.         for comp in self._comps:
  560.             if not comp.isdst:
  561.                 compdt = comp.rrule.before(dt - comp.tzoffsetdiff, inc = True)
  562.             else:
  563.                 compdt = comp.rrule.before(dt, inc = True)
  564.             if compdt:
  565.                 if not lastcompdt or lastcompdt < compdt:
  566.                     lastcompdt = compdt
  567.                     lastcomp = comp
  568.                     continue
  569.         
  570.         if not lastcomp:
  571.             for comp in self._comps:
  572.                 if not comp.isdst:
  573.                     lastcomp = comp
  574.                     break
  575.                     continue
  576.             else:
  577.                 lastcomp = comp[0]
  578.         
  579.         self._cachedate.insert(0, dt)
  580.         self._cachecomp.insert(0, lastcomp)
  581.         if len(self._cachedate) > 10:
  582.             self._cachedate.pop()
  583.             self._cachecomp.pop()
  584.         
  585.         return lastcomp
  586.  
  587.     
  588.     def utcoffset(self, dt):
  589.         return self._find_comp(dt).tzoffsetto
  590.  
  591.     
  592.     def dst(self, dt):
  593.         comp = self._find_comp(dt)
  594.         if comp.isdst:
  595.             return comp.tzoffsetdiff
  596.         return ZERO
  597.  
  598.     
  599.     def tzname(self, dt):
  600.         return self._find_comp(dt).tzname
  601.  
  602.     
  603.     def __repr__(self):
  604.         return '<tzicalvtz %s>' % `self._tzid`
  605.  
  606.     __reduce__ = object.__reduce__
  607.  
  608.  
  609. class tzical:
  610.     
  611.     def __init__(self, fileobj):
  612.         global rrule
  613.         if not rrule:
  614.             rrule = rrule
  615.             import dateutil
  616.         
  617.         if isinstance(fileobj, basestring):
  618.             self._s = fileobj
  619.             fileobj = open(fileobj)
  620.         elif hasattr(fileobj, 'name'):
  621.             self._s = fileobj.name
  622.         else:
  623.             self._s = `fileobj`
  624.         self._vtz = { }
  625.         self._parse_rfc(fileobj.read())
  626.  
  627.     
  628.     def keys(self):
  629.         return self._vtz.keys()
  630.  
  631.     
  632.     def get(self, tzid = None):
  633.         if tzid is None:
  634.             keys = self._vtz.keys()
  635.             if len(keys) == 0:
  636.                 raise ValueError, 'no timezones defined'
  637.             len(keys) == 0
  638.             if len(keys) > 1:
  639.                 raise ValueError, 'more than one timezone available'
  640.             len(keys) > 1
  641.             tzid = keys[0]
  642.         
  643.         return self._vtz.get(tzid)
  644.  
  645.     
  646.     def _parse_offset(self, s):
  647.         s = s.strip()
  648.         if not s:
  649.             raise ValueError, 'empty offset'
  650.         s
  651.         if s[0] in ('+', '-'):
  652.             signal = (-1, +1)[s[0] == '+']
  653.             s = s[1:]
  654.         else:
  655.             signal = +1
  656.         if len(s) == 4:
  657.             return (int(s[:2]) * 3600 + int(s[2:]) * 60) * signal
  658.         if len(s) == 6:
  659.             return (int(s[:2]) * 3600 + int(s[2:4]) * 60 + int(s[4:])) * signal
  660.         raise ValueError, 'invalid offset: ' + s
  661.  
  662.     
  663.     def _parse_rfc(self, s):
  664.         lines = s.splitlines()
  665.         if not lines:
  666.             raise ValueError, 'empty string'
  667.         lines
  668.         i = 0
  669.         while i < len(lines):
  670.             line = lines[i].rstrip()
  671.             if not line:
  672.                 del lines[i]
  673.                 continue
  674.             if i > 0 and line[0] == ' ':
  675.                 lines[i - 1] += line[1:]
  676.                 del lines[i]
  677.                 continue
  678.             i += 1
  679.         tzid = None
  680.         comps = []
  681.         invtz = False
  682.         comptype = None
  683.         for line in lines:
  684.             if not line:
  685.                 continue
  686.             
  687.             (name, value) = line.split(':', 1)
  688.             parms = name.split(';')
  689.             if not parms:
  690.                 raise ValueError, 'empty property name'
  691.             parms
  692.             name = parms[0].upper()
  693.             parms = parms[1:]
  694.             if invtz:
  695.                 if name == 'BEGIN':
  696.                     if value in ('STANDARD', 'DAYLIGHT'):
  697.                         pass
  698.                     else:
  699.                         raise ValueError, 'unknown component: ' + value
  700.                     comptype = value in ('STANDARD', 'DAYLIGHT')
  701.                     founddtstart = False
  702.                     tzoffsetfrom = None
  703.                     tzoffsetto = None
  704.                     rrulelines = []
  705.                     tzname = None
  706.                 elif name == 'END':
  707.                     if value == 'VTIMEZONE':
  708.                         if comptype:
  709.                             raise ValueError, 'component not closed: ' + comptype
  710.                         comptype
  711.                         if not tzid:
  712.                             raise ValueError, 'mandatory TZID not found'
  713.                         tzid
  714.                         if not comps:
  715.                             raise ValueError, 'at least one component is needed'
  716.                         comps
  717.                         self._vtz[tzid] = _tzicalvtz(tzid, comps)
  718.                         invtz = False
  719.                     elif value == comptype:
  720.                         if not founddtstart:
  721.                             raise ValueError, 'mandatory DTSTART not found'
  722.                         founddtstart
  723.                         if tzoffsetfrom is None:
  724.                             raise ValueError, 'mandatory TZOFFSETFROM not found'
  725.                         tzoffsetfrom is None
  726.                         if tzoffsetto is None:
  727.                             raise ValueError, 'mandatory TZOFFSETFROM not found'
  728.                         tzoffsetto is None
  729.                         rr = None
  730.                         if rrulelines:
  731.                             rr = rrule.rrulestr('\n'.join(rrulelines), compatible = True, ignoretz = True, cache = True)
  732.                         
  733.                         comp = _tzicalvtzcomp(tzoffsetfrom, tzoffsetto, comptype == 'DAYLIGHT', tzname, rr)
  734.                         comps.append(comp)
  735.                         comptype = None
  736.                     else:
  737.                         raise ValueError, 'invalid component end: ' + value
  738.                 value == 'VTIMEZONE'
  739.                 if comptype:
  740.                     if name == 'DTSTART':
  741.                         rrulelines.append(line)
  742.                         founddtstart = True
  743.                     elif name in ('RRULE', 'RDATE', 'EXRULE', 'EXDATE'):
  744.                         rrulelines.append(line)
  745.                     elif name == 'TZOFFSETFROM':
  746.                         if parms:
  747.                             raise ValueError, 'unsupported %s parm: %s ' % (name, parms[0])
  748.                         parms
  749.                         tzoffsetfrom = self._parse_offset(value)
  750.                     elif name == 'TZOFFSETTO':
  751.                         if parms:
  752.                             raise ValueError, 'unsupported TZOFFSETTO parm: ' + parms[0]
  753.                         parms
  754.                         tzoffsetto = self._parse_offset(value)
  755.                     elif name == 'TZNAME':
  756.                         if parms:
  757.                             raise ValueError, 'unsupported TZNAME parm: ' + parms[0]
  758.                         parms
  759.                         tzname = value
  760.                     elif name == 'COMMENT':
  761.                         pass
  762.                     else:
  763.                         raise ValueError, 'unsupported property: ' + name
  764.                 name == 'DTSTART'
  765.                 if name == 'TZID':
  766.                     if parms:
  767.                         raise ValueError, 'unsupported TZID parm: ' + parms[0]
  768.                     parms
  769.                     tzid = value
  770.                 elif name in ('TZURL', 'LAST-MODIFIED', 'COMMENT'):
  771.                     pass
  772.                 else:
  773.                     raise ValueError, 'unsupported property: ' + name
  774.             name == 'TZID'
  775.             if name == 'BEGIN' and value == 'VTIMEZONE':
  776.                 tzid = None
  777.                 comps = []
  778.                 invtz = True
  779.                 continue
  780.         
  781.  
  782.     
  783.     def __repr__(self):
  784.         return '%s(%s)' % (self.__class__.__name__, `self._s`)
  785.  
  786.  
  787. if sys.platform != 'win32':
  788.     TZFILES = [
  789.         '/etc/localtime',
  790.         'localtime']
  791.     TZPATHS = [
  792.         '/usr/share/zoneinfo',
  793.         '/usr/lib/zoneinfo',
  794.         '/etc/zoneinfo']
  795. else:
  796.     TZFILES = []
  797.     TZPATHS = []
  798.  
  799. def gettz(name = None):
  800.     tz = None
  801.     if not name:
  802.         
  803.         try:
  804.             name = os.environ['TZ']
  805.         except KeyError:
  806.             pass
  807.         except:
  808.             None<EXCEPTION MATCH>KeyError
  809.         
  810.  
  811.     None<EXCEPTION MATCH>KeyError
  812.     if name is None or name == ':':
  813.         for filepath in TZFILES:
  814.             if not os.path.isabs(filepath):
  815.                 filename = filepath
  816.                 for path in TZPATHS:
  817.                     filepath = os.path.join(path, filename)
  818.                     if os.path.isfile(filepath):
  819.                         break
  820.                         continue
  821.                 
  822.             
  823.             if os.path.isfile(filepath):
  824.                 
  825.                 try:
  826.                     tz = tzfile(filepath)
  827.                 except (IOError, OSError, ValueError):
  828.                     pass
  829.                 except:
  830.                     None<EXCEPTION MATCH>(IOError, OSError, ValueError)
  831.                 
  832.  
  833.             None<EXCEPTION MATCH>(IOError, OSError, ValueError)
  834.         else:
  835.             tz = tzlocal()
  836.     elif name.startswith(':'):
  837.         name = name[:-1]
  838.     
  839.     if os.path.isabs(name):
  840.         if os.path.isfile(name):
  841.             tz = tzfile(name)
  842.         else:
  843.             tz = None
  844.     else:
  845.         for path in TZPATHS:
  846.             filepath = os.path.join(path, name)
  847.             if not os.path.isfile(filepath):
  848.                 filepath = filepath.replace(' ', '_')
  849.                 if not os.path.isfile(filepath):
  850.                     continue
  851.                 
  852.             
  853.             
  854.             try:
  855.                 tz = tzfile(filepath)
  856.             continue
  857.             except (IOError, OSError, ValueError):
  858.                 continue
  859.             
  860.  
  861.         else:
  862.             tz = None
  863.             if tzwin:
  864.                 
  865.                 try:
  866.                     tz = tzwin(name)
  867.                 except OSError:
  868.                     None<EXCEPTION MATCH>(IOError, OSError, ValueError)
  869.                     None<EXCEPTION MATCH>(IOError, OSError, ValueError)
  870.                 except:
  871.                     None<EXCEPTION MATCH>(IOError, OSError, ValueError)<EXCEPTION MATCH>OSError
  872.                 
  873.  
  874.             if not tz:
  875.                 gettz = gettz
  876.                 import dateutil.zoneinfo
  877.                 tz = gettz(name)
  878.             
  879.             if not tz:
  880.                 for c in name:
  881.                     if c in '0123456789':
  882.                         
  883.                         try:
  884.                             tz = tzstr(name)
  885.                         except ValueError:
  886.                             pass
  887.  
  888.                         break
  889.                         continue
  890.                 elif name in ('GMT', 'UTC'):
  891.                     tz = tzutc()
  892.                 elif name in time.tzname:
  893.                     tz = tzlocal()
  894.                 
  895.             
  896.     return tz
  897.  
  898.