<s> from __future__ import with_statement <EOL> import os <EOL> import re <EOL> import pytz <EOL> _cache_tz = None <EOL> def _tz_from_env ( tzenv ) : <EOL> if tzenv [ <NUM_LIT:0> ] == '<STR_LIT::>' : <EOL> tzenv = tzenv [ <NUM_LIT:1> : ] <EOL> if os . path . exists ( tzenv ) : <EOL> with open ( tzenv , '<STR_LIT:rb>' ) as tzfile : <EOL> return pytz . tzfile . build_tzinfo ( '<STR_LIT>' , tzfile ) <EOL> try : <EOL> tz = pytz . timezone ( tzenv ) <EOL> return tz <EOL> except pytz . UnknownTimeZoneError : <EOL> raise pytz . UnknownTimeZoneError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _get_localzone ( _root = '<STR_LIT:/>' ) : <EOL> """<STR_LIT>""" <EOL> tzenv = os . environ . get ( '<STR_LIT>' ) <EOL> if tzenv : <EOL> return _tz_from_env ( tzenv ) <EOL> tzpath = os . path . join ( _root , '<STR_LIT>' ) <EOL> if os . path . exists ( tzpath ) : <EOL> with open ( tzpath , '<STR_LIT:rb>' ) as tzfile : <EOL> data = tzfile . read ( ) <EOL> if data [ : <NUM_LIT:5> ] != '<STR_LIT>' : <EOL> etctz = data . strip ( ) . decode ( ) <EOL> if '<STR_LIT:U+0020>' in etctz : <EOL> etctz , dummy = etctz . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if '<STR_LIT:#>' in etctz : <EOL> etctz , dummy = etctz . split ( '<STR_LIT:#>' , <NUM_LIT:1> ) <EOL> return pytz . timezone ( etctz . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) ) <EOL> zone_re = re . compile ( '<STR_LIT>' ) <EOL> timezone_re = re . compile ( '<STR_LIT>' ) <EOL> end_re = re . compile ( '<STR_LIT>' ) <EOL> for filename in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> tzpath = os . path . join ( _root , filename ) <EOL> if not os . path . exists ( tzpath ) : <EOL> continue <EOL> with open ( tzpath , '<STR_LIT>' ) as tzfile : <EOL> data = tzfile . readlines ( ) <EOL> for line in data : <EOL> match = zone_re . match ( line ) <EOL> if match is None : <EOL> match = timezone_re . match ( line ) <EOL> if match is not None : <EOL> line = line [ match . end ( ) : ] <EOL> etctz = line [ : end_re . search ( line ) . start ( ) ] <EOL> return pytz . timezone ( etctz . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) ) <EOL> for filename in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> tzpath = os . path . join ( _root , filename ) <EOL> if not os . path . exists ( tzpath ) : <EOL> continue <EOL> with open ( tzpath , '<STR_LIT:rb>' ) as tzfile : <EOL> return pytz . tzfile . build_tzinfo ( '<STR_LIT>' , tzfile ) <EOL> raise pytz . UnknownTimeZoneError ( '<STR_LIT>' ) <EOL> def get_localzone ( ) : <EOL> """<STR_LIT>""" <EOL> global _cache_tz <EOL> if _cache_tz is None : <EOL> _cache_tz = _get_localzone ( ) <EOL> return _cache_tz <EOL> def reload_localzone ( ) : <EOL> """<STR_LIT>""" <EOL> global _cache_tz <EOL> _cache_tz = _get_localzone ( ) <EOL> return _cache_tz </s>
