def loadFromRegistryCurrentUser(self):
''' Load configuration from Windows registry. '''
# We manually build a .INI file in memory from the registry.
inilines = ['[%s]' % applicationConfig.CONFIG_SECTIONNAME]
try:
import _winreg
except ImportError, exc:
raise ImportError, "applicationConfig.loadFromRegistryCurrentUser() can only be used under Windows (requires the _winreg module).\nCould not import module because: %s" % exc
try:
key = _winreg.OpenKey(
_winreg.HKEY_CURRENT_USER, applicationConfig.CONFIG_REGPATH, 0, _winreg.KEY_READ)
# Now get all values in this key:
i = 0
try:
while True:
# mmm..strange, Should unpack to 3 values, but seems to
# unpack to more. Bug of EnumValue() ?
valueobj = _winreg.EnumValue(key, i)
valuename = str(valueobj[0]).strip()
valuedata = str(valueobj[1]).strip()
valuetype = valueobj[2]
if valuetype != _winreg.REG_SZ:
raise TypeError, "The registry value %s does not have the correct type (REG_SZ). Please delete it." % valuename
else:
if valuename not in applicationConfig.NONEXPORTABLE_PARAMETERS:
# Build the .INI file.
inilines += ['%s=%s' % (valuename, str(valuedata))]
i += 1
except EnvironmentError:
# EnvironmentError means: "No more values to read". We simply
# exit the 'While True' loop.
pass
# Then parse the generated .INI file.
self.fromINI('\n'.join(inilines))
except EnvironmentError:
raise WindowsError, "Could not read configuration from registry !"
_winreg.CloseKey(key)
评论列表
文章目录