[Python-Dev] winreg

Paul Prescod paul@prescod.net
Mon, 26 Jun 2000 08:43:31 -0700


[I'm having mail troubles so my response time may be slow...]

I've just had a chance to look at the winreg module. I think that it is
too low-level. I wrote a wrapper class that I feel is more Pythonic.
I'll outline the basic ideas and then if there is interest I'll develop
a test suite and send it out for eyeballing by Mark and others. Yes, I
am proposing this for 1.6. If we're going to put in a registry module,
it should be as Pythonic as possible.

First, about Winreg 1:

It was perfect when it was part of the win32 package and was supposed to
mirror the win32 APIs exactly (for reasons of documentation and
familiarity) but as the "standard" Python registry manipulation module
it seems too low-level to me.

I'm sure Mark would be the first to admit that it isn't very Pythonic.
It's more Microsoft-ic. In one part of the docs he says: "this API is
lame, lame, lame. Don't use it." :) 

There are also bogus parameters that Microsoft hasn't got around to
assigning values to, undocumented constants and so forth. My favorite is
"WHOLE_HIVE_VOLATILE".

Winreg 2:

The basic idea in the pythonic wrapper is that there are key "objects"
rather than just handles. winreg already has a primtive "handle object"
but for some reason most of the stuff that would logically be methods
are actually functions that take the handle as the first param.

Keys can have subkeys. So in winreg 1 you would say:

winreg.CreateKey( winreg.CreateKey( winreg.HKEY_LOCAL_MACHINE,
"HARDWARE"), "DESCRIPTION" )

now you say:

winreg.HKEY_LOCAL_MACHINE.createKey( "HARDWARE").createKey(
"DESCRIPTION" )

(you could also use a path syntax in either case)

You can get a complete list of existant subkeys with a getSubkeys()
method call. This list behaves like a Python mapping and also like a
sequence. You can use either string key names or integer ordinal
indexes. You can fetch and delete keys based on those indexes:

for key in winreg.HKEY_LOCAL_MACHINE.getSubkeys():
	dosomething( key )

Before it was something like this:

for index in xrange( 0, sys.maxint ):
	try:
		dosomething( winreg.EnumKey( index ) )
	except WindowsError:
		break

"Values" (in the Microsoft sense) are handled the same basic way.
Looping, deleting, etc. is the same.

When you fetch a value, you get a (type,value) tuple. Types are objects
with properties:

typeobj.intval -> 0..10
typeobj.msname -> REG_SZ, REG_SZ_MULTI, ...
typeobj.friendlyname -> "String", "Sting List", ...

Type coercions are all done by the underlying module (the old winreg)
except that I've decided that binary data should be returned as an
array.array('c') rather than an 8-bit string.

-- 
 Paul Prescod
Out of timber so crooked as that which man is made nothing entirely
straight can be built. - Immanuel Kant