def _cache_fonts_win32():
"""Caches fonts on a Win32 platform."""
key = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
regfonts = []
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key) as fontkey:
idx = 0
enumval = winreg.EnumValue
rappend = regfonts.append
while True:
rappend(enumval(fontkey, idx)[:2])
idx += 1
except WindowsError:
pass
# TODO: integrate alias handling for fonts within the registry.
# TODO: Scan and index fonts from %SystemRoot%\\Fonts that are not in the
# registry
# Received all fonts from the registry.
for name, filename in regfonts:
fonttype = os.path.splitext(filename)[1][1:].lower()
if name.endswith("(TrueType)"):
name = name[:-10].strip()
if name.endswith("(All Res)"):
name = name[:-9].strip()
style = STYLE_NORMAL
if name.find(" Bold") >= 0:
style |= STYLE_BOLD
if name.find(" Italic") >= 0 or name.find(" Oblique") >= 0:
style |= STYLE_ITALIC
family = name
for rm in ("Bold", "Italic", "Oblique"):
family = family.replace(rm, "")
family = family.lower().strip()
fontpath = os.environ.get("SystemRoot", "C:\\Windows")
fontpath = os.path.join(fontpath, "Fonts")
if filename.find("\\") == -1:
# No path delimiter is given; we assume it to be a font in
# %SystemRoot%\Fonts
filename = os.path.join(fontpath, filename)
_add_font(family, name, style, fonttype, filename)
python类EnumValue()的实例源码
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