In a spirit of being helpful... :)

The code below (which I imagine every Windows programmer writes
sometime in their Python life) mimics the os.walk functionality, yielding
the key, subkeys, and values under a particular starting point in the
registry. The "if __name__ == '__main__'" test run at the bottom does
more-or-less what you were asking for originally, I think, converting
some name to some other name wherever it appears.

<module regwalk.py>
import _winreg

HIVES = {
 "HKEY_LOCAL_MACHINE" : _winreg.HKEY_LOCAL_MACHINE,
 "HKEY_CURRENT_USER" : _winreg.HKEY_CURRENT_USER,
 "HKEY_CLASSES_ROOT" : _winreg.HKEY_CLASSES_ROOT,
 "HKEY_USERS" : _winreg.HKEY_USERS,
 "HKEY_CURRENT_CONFIG" : _winreg.HKEY_CURRENT_CONFIG
}

class RegKey:
def __init__ (self, name, key):
    self.name = name
    self.key = key
def __str__ (self):
   return self.name

def walk (top):
 """walk the registry starting from the key represented by
 top in the form HIVE\\key\\subkey\\..\\subkey and generating
 key, subkey_names, values at each level.
key is a lightly wrapped registry key, including the name
 and the HKEY object.
 subkey_names are simply names of the subkeys of that key
 values are 3-tuples containing (name, data, data-type).
 See the documentation for _winreg.EnumValue for more details.
 """
 if "\\" not in top: top += "\\"
 root, subkey = top.split ("\\", 1)
 key = _winreg.OpenKey (HIVES[root], subkey, 0, _winreg.KEY_READ | 
_winreg.KEY_SET_VALUE)
subkeys = []
 i = 0
 while True:
   try:
     subkeys.append (_winreg.EnumKey (key, i))
     i += 1
   except EnvironmentError:
     break
values = []
 i = 0
 while True:
   try:
     values.append (_winreg.EnumValue (key, i))
     i += 1
   except EnvironmentError:
     break

 yield RegKey (top, key), subkeys, values
for subkey in subkeys:
   for result in walk (top + "\\" + subkey):
     yield result
if __name__ == '__main__':
 for key, subkey_names, values in walk ("HKEY_LOCAL_MACHINE\\Software\\Python"):
   print key
   for (name, data, type) in values:
     print "  ", name, "=>", data
     if type == _winreg.REG_SZ and "TJG" in data:
       _winreg.SetValueEx (key.key, name, 0, type, data.replace ("TJG", "XYZ"))

</module regwalk.py>

TJG
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to