0

I have been working on this function to read a registry value from Windows. It seems very verbose and repetitive. I could not find a way to refer to the variables imported from winreg other than to type them explicitly. If I could somehow refer to them, this could be cleaned up and shortened greatly. Suggestions?

from winreg import *
import winreg
from tkinter import *
from tkinter import messagebox

# to read the (Default) value of a key, put a '.' at the end of the KeyString!

def mymessagebox(messagestr):
    root=Tk()
    root.withdraw()
    messagebox.showwarning('Warning!', messagestr)

def ReadRegValue(KeyString):
    try:
        KeyRoot, KeyFolder = KeyString.split('\\',1)
        if KeyFolder == '.':
            KeyFolder = ''
            Key = ''
        else:
            try:
                KeyFolder, Key = KeyFolder.rsplit('\\',1)
                if Key == '.':
                    Key = ''
            except ValueError:
                Key = ''
        if Key == '':
            if KeyRoot == 'HKEY_CLASSES_ROOT':
                try:
                    keyhandle = winreg.ConnectRegistry(None, HKEY_CLASSES_ROOT)
                    keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(KeyFolder + ' not found in registry')
            elif KeyRoot == 'HKEY_CURRENT_USER':
                try:
                    keyhandle = winreg.ConnectRegistry(None, HKEY_CURRENT_USER)
                    keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(KeyFolder + ' not found in registry')
            elif KeyRoot == 'HKEY_LOCAL_MACHINE':
                try:
                    keyhandle = winreg.ConnectRegistry(None, HKEY_LOCAL_MACHINE)
                    keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(KeyFolder + ' not found in registry')
            elif KeyRoot == 'HKEY_USERS':
                try:
                    keyhandle = winreg.ConnectRegistry(None, HKEY_USERS)
                    keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(KeyFolder + ' not found in registry')
            elif KeyRoot == 'HKEY_PERFORMANCE_DATA':
                try:
                    keyhandle = winreg.ConnectRegistry(None, HKEY_PERFORMANCE_DATA)
                    keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(KeyFolder + ' not found in registry')
            elif KeyRoot == 'HKEY_CURRENT_CONFIG':
                try:
                    keyhandle = winreg.ConnectRegistry(None, HKEY_CURRENT_CONFIG)
                    keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(KeyFolder + ' not found in registry')
            elif KeyRoot == 'HKEY_DYN_DATA':
                try:
                    keyhandle = winreg.ConnectRegistry(None, HKEY_DYN_DATA)
                    keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(KeyFolder + ' not found in registry')
            else:
                mymessagebox(KeyRoot + ' is not a valid Key Root')
        else:
            if KeyRoot == 'HKEY_CLASSES_ROOT':
                try:
                    keyhandle = winreg.OpenKey(HKEY_CLASSES_ROOT, KeyFolder, 0, KEY_READ)
                    keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(Key + ' not found in registry')
            elif KeyRoot == 'HKEY_CURRENT_USER':
                try:
                    keyhandle = winreg.OpenKey(HKEY_CURRENT_USER, KeyFolder, 0, KEY_READ)
                    keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(Key + ' not found in registry')
            elif KeyRoot == 'HKEY_LOCAL_MACHINE':
                try:
                    keyhandle = winreg.OpenKey(HKEY_LOCAL_MACHINE, KeyFolder, 0, KEY_READ)
                    keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(Key + ' not found in registry')
            elif KeyRoot == 'HKEY_USERS':
                try:
                    keyhandle = winreg.OpenKey(HKEY_USERS, KeyFolder, 0, KEY_READ)
                    keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(Key + ' not found in registry')
            elif KeyRoot == 'HKEY_PERFORMANCE_DATA':
                try:
                    keyhandle = winreg.OpenKey(HKEY_PERFORMANCE_DATA, KeyFolder, 0, KEY_READ)
                    keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(Key + ' not found in registry')
            elif KeyRoot == 'HKEY_CURRENT_CONFIG':
                try:
                    keyhandle = winreg.OpenKey(HKEY_CURRENT_CONFIG, KeyFolder, 0, KEY_READ)
                    keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(Key + ' not found in registry')
            elif KeyRoot == 'HKEY_DYN_DATA':
                try:
                    keyhandle = winreg.OpenKey(HKEY_DYN_DATA, KeyFolder, 0, KEY_READ)
                    keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox(Key + ' not found in registry')
            else:
                mymessagebox(KeyRoot + ' is not a valid Key Root')
    except ValueError:
        mymessagebox('Your key string Must be in the format SOME_VALID_ROOT_KEY\folder\key .  To show a (Default) key value, put a "." at the end of your string.')
#
# Lets do some tests...
# 

results = ReadRegValue(r'HKEY_CLASSES_ROOT\.')
print (results)
4

3 に答える 3

0

It is indeed extremely repetitive. You can use getattr() to take most of that repetition out:

root = getattr(winreg, KeyRoot, None)
if root is None or not KeyRoot.startswith('HKEY_'):
    mymessagebox('{} is not a valid Key Root'.format(KeyRoot))
    return

if not Key:
    try:
        keyhandle = winreg.ConnectRegistry(None, root)
        return winreg.QueryValue(keyhandle, KeyFolder)
    except FileNotFoundError:
        mymessagebox('{} not found in registry'.format(KeyFolder))
else:
    try:
        keyhandle = winreg.OpenKey(root, KeyFolder, 0, KEY_READ)
        return winreg.QueryValueEx(keyhandle, Key)[0]
    except FileNotFoundError:
        mymessagebox('{} not found in registry'.format(KeyFolder))
于 2013-02-17T11:54:12.333 に答える
0

Why not using a dict?

hives = {
    'HKEY_CLASSES_ROOT': HKEY_CLASSES_ROOT,
    'HKEY_CURRENT_USER': HKEY_CURRENT_USER,
    'HKEY_LOCAL_MACHINE': HKEY_LOCAL_MACHINE
}

if key_root in hives:
    try:
        key_handle = winreg.ConnectRegistry(None, hives[key_root])
        key_value = winreg.QueryValue(key_handle, key_folder)
        return key_value
    except FileNotFoundError:
        mymessagebox(key_folder + ' not found in registry')
于 2013-02-17T11:54:53.770 に答える
0

After version:

from winreg import *
import winreg
from tkinter import *
from tkinter import messagebox

# to read the (Default) value of a key, put a '.' at the end of the KeyString!

def mymessagebox(messagestr):
    root=Tk()
    root.withdraw()
    messagebox.showwarning('Warning!', messagestr)

def ReadRegValue(KeyString):
    validroots = {
    'HKEY_CLASSES_ROOT': HKEY_CLASSES_ROOT,
    'HKEY_CURRENT_USER':HKEY_CURRENT_USER,
    'HKEY_LOCAL_MACHINE': HKEY_LOCAL_MACHINE,
    'HKEY_USERS': HKEY_USERS,
    'HKEY_PERFORMANCE_DATA': HKEY_PERFORMANCE_DATA,
    'HKEY_CURRENT_CONFIG': HKEY_CURRENT_CONFIG,
    'HKEY_DYN_DATA': HKEY_DYN_DATA
}
    try:
        KeyRoot, KeyFolder = KeyString.split('\\',1)
        if KeyFolder == '.':
            KeyFolder = ''
            Key = ''
        else:
            try:
                KeyFolder, Key = KeyFolder.rsplit('\\',1)
                if Key == '.':
                    Key = ''
            except ValueError:
                Key = ''
        if KeyRoot in validroots:
            if Key == '':
                try:
                    keyhandle = winreg.ConnectRegistry(None, getattr(winreg, KeyRoot, None))
                    keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox('{} not found in registry'.format(KeyFolder))
            else:
                try:
                    keyhandle = winreg.OpenKey(getattr(winreg, KeyRoot, None), KeyFolder, 0, KEY_READ)
                    keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
                    return keyvalue
                except FileNotFoundError:
                    mymessagebox('{} not found in registry'.format(Key))
        else:
            mymessagebox('{} is not a valid Key Root'.format(KeyRoot))
    except ValueError:
        mymessagebox('Your key string Must be in the format SOME_VALID_ROOT_KEY\folder\key .  To show a (Default) key value, put a "." at the end of your string.')
    return

#
# Lets do some tests...
# 
results = ReadRegValue(r'HKEY_CLASSES_ROOT\.')
print (results)
results = ReadRegValue(r'HKEY_CLASSES_ROOT\.7z\.')
print (results)
results = ReadRegValue(r'HKEY_CLASSES_ROOT\.ac3\PerceivedType')
print (results)
results = ReadRegValue(r'HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\1\ProcessorNameString')
print (results)
results = ReadRegValue(r'HKEY_CLASSES_ROOT\.')
print (results)
于 2013-02-17T22:48:29.580 に答える