[pywin32-checkins] pywin32/win32/Lib win32timezone.py,1.13,1.14
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Jason R. C. <ja...@us...> - 2009-01-13 03:30:50
|
Update of /cvsroot/pywin32/pywin32/win32/Lib In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv22184 Modified Files: win32timezone.py Log Message: Refactored win32timezone to encapsulate registry operations. Index: win32timezone.py =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/Lib/win32timezone.py,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** win32timezone.py 8 Jan 2009 02:46:13 -0000 1.13 --- win32timezone.py 13 Jan 2009 03:30:44 -0000 1.14 *************** *** 137,140 **** --- 137,141 ---- import operator import warnings + from itertools import count import logging *************** *** 144,158 **** format = '3l8h8h' ! def __init__(self, key, name = None): ! if(not name and len(key) == struct.calcsize(self.format)): ! self.__init_from_bytes__(key) ! else: ! self.__init_from_reg_key__(key, name) ! ! def __init_from_reg_key__(self, key, name = None): ! if not name: ! key, name = os.path.split(key) ! value, type = _winreg.QueryValueEx(key, name) ! self.__init_from_bytes__(value) def __init_from_bytes__(self, bytes): --- 145,150 ---- format = '3l8h8h' ! def __init__(self, bytes): ! self.__init_from_bytes__(bytes) def __init_from_bytes__(self, bytes): *************** *** 218,223 **** def __init__(self, timeZoneName, fixedStandardTime=False): self.timeZoneName = timeZoneName ! key = self._FindTimeZoneKey() ! self._LoadInfoFromKey(key) self.fixedStandardTime = fixedStandardTime --- 210,214 ---- def __init__(self, timeZoneName, fixedStandardTime=False): self.timeZoneName = timeZoneName ! self._LoadInfoFromKey() self.fixedStandardTime = fixedStandardTime *************** *** 230,258 **** # English-based hard-coded time zones. timeZoneName = zoneNames.get(self.timeZoneName, self.timeZoneName) ! tzRegKeyPath = os.path.join(self.tzRegKey, timeZoneName) try: ! key = _winreg.OpenKeyEx(_winreg.HKEY_LOCAL_MACHINE, tzRegKeyPath) except: raise ValueError('Timezone Name %s not found.' % timeZoneName) ! return key def __getinitargs__(self): return (self.timeZoneName,) ! def _LoadInfoFromKey(self, key): """Loads the information from an opened time zone registry key into relevant fields of this TZI object""" ! self.displayName = _winreg.QueryValueEx(key, "Display")[0] ! self.standardName = _winreg.QueryValueEx(key, "Std")[0] ! self.daylightName = _winreg.QueryValueEx(key, "Dlt")[0] ! self.staticInfo = WinTZI(key, "TZI") self._LoadDynamicInfoFromKey(key) def _LoadDynamicInfoFromKey(self, key): try: ! dkey = _winreg.OpenKeyEx(key, 'Dynamic DST') except WindowsError: return - info = _RegKeyDict(dkey) del info['FirstEntry'] del info['LastEntry'] --- 221,249 ---- # English-based hard-coded time zones. timeZoneName = zoneNames.get(self.timeZoneName, self.timeZoneName) ! key = _RegKeyDict.open(_winreg.HKEY_LOCAL_MACHINE, self.tzRegKey) try: ! result = key.subkey(timeZoneName) except: raise ValueError('Timezone Name %s not found.' % timeZoneName) ! return result def __getinitargs__(self): return (self.timeZoneName,) ! def _LoadInfoFromKey(self): """Loads the information from an opened time zone registry key into relevant fields of this TZI object""" ! key = self._FindTimeZoneKey() ! self.displayName = key['Display'] ! self.standardName = key['Std'] ! self.daylightName = key['Dlt'] ! self.staticInfo = WinTZI(key['TZI']) self._LoadDynamicInfoFromKey(key) def _LoadDynamicInfoFromKey(self, key): try: ! info = key.subkey('Dynamic DST') except WindowsError: return del info['FirstEntry'] del info['LastEntry'] *************** *** 345,374 **** # helper methods for accessing the timezone info from the registry def _get_time_zone_key(subkey=None): "Return the registry key that stores time zone details" ! key = _winreg.OpenKeyEx(_winreg.HKEY_LOCAL_MACHINE, TimeZoneInfo.tzRegKey) if subkey: ! key = _winreg.OpenKeyEx(key, subkey) return key - _get_time_zone_key = staticmethod(_get_time_zone_key) def _get_time_zone_key_names(): "Returns the names of the (registry keys of the) time zones" ! return _RegKeyEnumerator(TimeZoneInfo._get_time_zone_key()) ! _get_time_zone_key_names = staticmethod(_get_time_zone_key_names) ! def _get_indexed_time_zone_keys(index_key='Index'): """ Get the names of the registry keys indexed by a value in that key. """ ! key_names = tuple(TimeZoneInfo._get_time_zone_key_names()) def get_index_value(key_name): key = TimeZoneInfo._get_time_zone_key(key_name) ! value, type = _winreg.QueryValueEx(key, index_key) ! return value values = map(get_index_value, key_names) return zip(values, key_names) - _get_indexed_time_zone_keys = staticmethod(_get_indexed_time_zone_keys) def get_sorted_time_zone_names(): "Return a list of time zone names that can be used to initialize TimeZoneInfo instances" --- 336,365 ---- # helper methods for accessing the timezone info from the registry + @staticmethod def _get_time_zone_key(subkey=None): "Return the registry key that stores time zone details" ! key = _RegKeyDict.open(_winreg.HKEY_LOCAL_MACHINE, TimeZoneInfo.tzRegKey) if subkey: ! key = key.subkey(subkey) return key + @staticmethod def _get_time_zone_key_names(): "Returns the names of the (registry keys of the) time zones" ! return TimeZoneInfo._get_time_zone_key().subkeys() ! ! @staticmethod def _get_indexed_time_zone_keys(index_key='Index'): """ Get the names of the registry keys indexed by a value in that key. """ ! key_names = TimeZoneInfo._get_time_zone_key_names() def get_index_value(key_name): key = TimeZoneInfo._get_time_zone_key(key_name) ! return key[index_key] values = map(get_index_value, key_names) return zip(values, key_names) + @staticmethod def get_sorted_time_zone_names(): "Return a list of time zone names that can be used to initialize TimeZoneInfo instances" *************** *** 376,385 **** get_standard_name = lambda tzi: tzi.standardName return map(get_standard_name, tzs) - get_sorted_time_zone_names = staticmethod(get_sorted_time_zone_names) def get_all_time_zones(): return map(TimeZoneInfo, TimeZoneInfo._get_time_zone_key_names()) - get_all_time_zones = staticmethod(get_all_time_zones) def get_sorted_time_zones(key=None): """ --- 367,376 ---- get_standard_name = lambda tzi: tzi.standardName return map(get_standard_name, tzs) + @staticmethod def get_all_time_zones(): return map(TimeZoneInfo, TimeZoneInfo._get_time_zone_key_names()) + @staticmethod def get_sorted_time_zones(key=None): """ *************** *** 394,419 **** zones.sort(key=key) return zones - get_sorted_time_zones = staticmethod(get_sorted_time_zones) - - def _RegKeyEnumerator(key): - return _RegEnumerator(key, _winreg.EnumKey) - - def _RegValueEnumerator(key): - return _RegEnumerator(key, _winreg.EnumValue) ! def _RegEnumerator(key, func): ! "Enumerates an open registry key as an iterable generator" ! index = 0 ! try: ! while 1: ! yield func(key, index) ! index += 1 ! except WindowsError: pass ! def _RegKeyDict(key): ! values = _RegValueEnumerator(key) ! values = tuple(values) ! return dict(map(lambda (name,value,type): (name,value), values)) # for backward compatibility def deprecated(func, name='Unknown'): --- 385,427 ---- zones.sort(key=key) return zones ! class _RegKeyDict(dict): ! def __init__(self, key): ! dict.__init__(self) ! self.key = key ! self.__load_values() ! @classmethod ! def open(cls, *args, **kargs): ! return _RegKeyDict(_winreg.OpenKeyEx(*args, **kargs)) ! ! def subkey(self, name): ! return _RegKeyDict(_winreg.OpenKeyEx(self.key, name)) ! ! def __load_values(self): ! get_name_value = lambda (name, value, type): (name, value) ! pairs = map(get_name_value, self._enumerate_reg_values(self.key)) ! self.update(pairs) ! ! def subkeys(self): ! return self._enumerate_reg_keys(self.key) + @staticmethod + def _enumerate_reg_values(key): + return _RegKeyDict._enumerate_reg(key, _winreg.EnumValue) + + @staticmethod + def _enumerate_reg_keys(key): + return _RegKeyDict._enumerate_reg(key, _winreg.EnumKey) + + @staticmethod + def _enumerate_reg(key, func): + "Enumerates an open registry key as an iterable generator" + try: + for index in count(): + yield func(key, index) + except WindowsError: pass + + # for backward compatibility def deprecated(func, name='Unknown'): *************** *** 455,460 **** """ tzRegKey = r'SYSTEM\CurrentControlSet\Control\TimeZoneInformation' ! key = _winreg.OpenKeyEx(_winreg.HKEY_LOCAL_MACHINE, tzRegKey) ! local = _RegKeyDict(key) # if the user has not checked "Automatically adjust clock for daylight # saving changes" in the Date and Time Properties control, the standard --- 463,467 ---- """ tzRegKey = r'SYSTEM\CurrentControlSet\Control\TimeZoneInformation' ! local = _RegKeyDict.open(_winreg.HKEY_LOCAL_MACHINE, tzRegKey) # if the user has not checked "Automatically adjust clock for daylight # saving changes" in the Date and Time Properties control, the standard |