[ANN] regobj - Pythonic object-based access to the Windows Registry
Hi All, I've just released the results of a nice Sunday's coding, inspired by one too many turns at futzing around with the _winreg module. The "regobj" module brings a convenient and clean object-based API for accessing the Windows Registry. http://pypi.python.org/pypi/regobj/ More details and some API examples below for those who are interested. Cheers, Ryan """ regobj: Pythonic object-based access to the Windows Registry License: BSD Current Release: 0.1.0 This module provides a thin wrapper around the standard _winreg module, allowing easier and more pythonic access to the Windows Registry. All access to the registry is done through Key objects, which (surprise!) represent a specific registry key. Starting from pre-defined root keys, all subkey access is done using standard attribute syntax:
HKCU.Software.Microsoft.Windows <regobj Key 'HKEY_CURRENT_USER\Software\Microsoft\Windows'> HKCU.Software.MyTests Traceback (most recent call last): ... AttributeError: subkey 'MyTests' does not exist HKCU.Software.MyTests = Key HKCU.Software.MyTests <regobj Key 'HKEY_CURRENT_USER\Software\MyTests'> del HKCU.Software.MyTests
Of course, for keys that don't happen to be named like python identifiers, there are also methods that can accomplish the same thing. The individual values contained in a key can be accessed using standard item access syntax. The returned objects will be instances of the Value class, with 'name', 'type' and 'data' attributes:
HKCU.Software.Microsoft.Clock["iFormat"] <regobj Value (iFormat,1,REG_SZ)> HKCU.Software.Microsoft.Clock["iFormat"].name 'iFormat' HKCU.Software.Microsoft.Clock["iFormat"].data u'1' HKCU.Software.Microsoft.Clock["iFormat"].type 1 HKCU.Software.Microsoft.Clock["notavalue"] Traceback (most recent call last): ... KeyError: "no such value: 'notavalue'"
Iterating over a key generates all the contained values, followed by all the contained subkeys. There are also methods to separately iterate over just the values, or just the subkeys:
winK = HKCU.Software.Microsoft.Windows winK["testvalue"] = 42 for obj in winK: ... print obj <regobj Value (testvalue,42,REG_DWORD)> <regobj Key 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion'> <regobj Key 'HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell'> <regobj Key 'HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam'> [k.name for k in winK.subkeys()] ['CurrentVersion', 'Shell', 'ShellNoRoam'] [v.data for v in winK.values()] [42]
These iterators also provide efficient implementations of the __contains__ and __len__ methods. Finally, there is powerful support for specifying key and value structures at creation time. The simplest case has already been demonstrated, where setting a subkey to the Key class or to None will create it without any data:
HKCU.Software.MyTests = None len(HKCU.Software.MyTests) 0
If a subkey is assigned an existing key object, the data from that key is copied into the subkey:
HKCU.Software.MyTests = HKCU.Software.Microsoft.Windows [k.name for k in HKCU.Software.MyTests] ['CurrentVersion', 'Shell', 'ShellNoRoam']
If a subkey is assigned a dictionary, the structure of that dictionary is copied into the subkey. Scalar values become key values, while nested dictionaries create subkeys:
HKCU.Software.MyTests = {"val1":7, "stuff":{"a":1,"c":2,"e":3}} [v.name for v in HKCU.Software.MyTests.values()] ['val1'] [k.name for k in HKCU.Software.MyTests.subkeys()] ['stuff'] len(HKCU.Software.MyTests.stuff) 3
Any other value assigned to a subkey will become the default value for that key (i.e. the value with name ""):
HKCU.Software.MyTests = "dead parrot" HKCU.Software.MyTests[""].data u'dead parrot'
And that's that - enjoy! """ -- Ryan Kelly http://www.rfk.id.au | This message is digitally signed. Please visit ryan@rfk.id.au | http://www.rfk.id.au/ramblings/gpg/ for details
participants (1)
-
Ryan Kelly