def _detect_distro_official(self, given_enabled, given_label, given_path):
dist_props = {
'enabled': given_enabled,
'label': given_label,
'exe_file': None,
'cmd_args': ['-load', '%1'],
'sessions': []}
# label
if not dist_props['label']:
dist_props['label'] = "PuTTY"
# enabled? don't go further if not
if dist_props['enabled'] is None:
dist_props['enabled'] = True
if not dist_props['enabled']:
return dist_props
# find executable
exe_file = None
if given_path:
exe_file = os.path.normpath(os.path.join(given_path, self.EXE_NAME_OFFICIAL))
if not os.path.exists(exe_file):
exe_file = None
if not exe_file:
exe_file = self._autodetect_official_installreg()
if not exe_file:
exe_file = self._autodetect_startmenu(self.EXE_NAME_OFFICIAL, "PuTTY.lnk")
if not exe_file:
exe_file = self._autodetect_official_progfiles()
if not exe_file:
exe_file = self._autodetect_path(self.EXE_NAME_OFFICIAL)
#if not exe_file:
# exe_file = self._autodetect_startmenu(self.EXE_NAME_OFFICIAL, "*putty*.lnk")
if not exe_file:
return None
dist_props['exe_file'] = exe_file
# list configured sessions
try:
hkey = winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
'Software\\SimonTatham\\PuTTY\\Sessions')
index = 0
while True:
try:
dist_props['sessions'].append(urllib.parse.unquote(
winreg.EnumKey(hkey, index), encoding='mbcs'))
index += 1
except OSError:
break
winreg.CloseKey(hkey)
except OSError:
pass
return dist_props
python类EnumKey()的实例源码
def __init__(self):
if self.info is not None:
return
info = []
try:
#XXX: Bad style to use so long `try:...except:...`. Fix it!
if sys.version_info[0] >= 3:
import winreg
else:
import _winreg as winreg
prgx = re.compile(r"family\s+(?P<FML>\d+)\s+model\s+(?P<MDL>\d+)"\
"\s+stepping\s+(?P<STP>\d+)", re.IGNORECASE)
chnd=winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, self.pkey)
pnum=0
while True:
try:
proc=winreg.EnumKey(chnd, pnum)
except winreg.error:
break
else:
pnum+=1
info.append({"Processor":proc})
phnd=winreg.OpenKey(chnd, proc)
pidx=0
while True:
try:
name, value, vtpe=winreg.EnumValue(phnd, pidx)
except winreg.error:
break
else:
pidx=pidx+1
info[-1][name]=value
if name=="Identifier":
srch=prgx.search(value)
if srch:
info[-1]["Family"]=int(srch.group("FML"))
info[-1]["Model"]=int(srch.group("MDL"))
info[-1]["Stepping"]=int(srch.group("STP"))
except:
print(sys.exc_info()[1], '(ignoring)')
self.__class__.info = info