Thursday 2 October 2008 — This is 16 years old. Be careful.
In writing the Python registry switcher, I needed to search the registry for references to my old Python version. Another good use for a Python script:
""" Search the Windows registry.
"""
import _winreg as reg
import itertools
RegRoots = {
reg.HKEY_CLASSES_ROOT: 'HKEY_CLASSES_ROOT',
reg.HKEY_CURRENT_USER: 'HKEY_CURRENT_USER',
reg.HKEY_LOCAL_MACHINE: 'HKEY_LOCAL_MACHINE',
reg.HKEY_USERS: 'HKEY_USERS',
}
class RegKey:
""" A handy wrapper around the raw stuff in the _winreg module.
"""
def __init__(self, rawkey, root, path):
self.key = rawkey
self.root = root
self.path = path
def __str__(self):
return "%s\\%s" % (RegRoots.get(self.root, hex(self.root)), self.path)
def close(self):
reg.CloseKey(self.key)
def values(self):
""" Enumerate the values in this key.
"""
for ikey in itertools.count():
try:
yield reg.EnumValue(self.key, ikey)
except EnvironmentError:
break
def subkey_names(self):
""" Enumerate the names of the subkeys in this key.
"""
for ikey in itertools.count():
try:
yield reg.EnumKey(self.key, ikey)
except EnvironmentError:
break
def subkeys(self):
""" Enumerate the subkeys in this key.
"""
for subkey_name in self.subkey_names():
if self.path:
sub = self.path + '\\' + subkey_name
else:
sub = subkey_name
yield OpenRegKey(self.root, sub)
def OpenRegKey(root, path):
try:
rawkey = reg.OpenKey(root, path)
except Exception, e:
#print "Couldn't open %r %r: %s" % (root, path, e)
return None
return RegKey(rawkey, root, path)
def grep_key(key, target):
for name, value, typ in key.values():
if isinstance(value, basestring) and target in value:
print "%s\\%s = %r" % (key, name, value)
for subkey in key.subkeys():
if not subkey:
continue
grep_key(subkey, target)
subkey.close()
def grep_registry(args):
for root in RegRoots.keys():
grep_key(OpenRegKey(root, ""), args[1])
if __name__ == '__main__':
import sys
grep_registry(sys.argv)
Most of this is a pythonic wrapper around the _winreg module, with a few simple functions at the end to actually search the registry.
Comments
A couple of comments:
1. Your grep_registry function is weird. It expects sys.argv as an argument and this isn't intuitive for general-purpose use. It's better to make it expect just a list of search items
2. Whenever I'm dealing with the registry, Regscanner is very helpful. Hope you know about it.
Add a comment: