registry - A simple interface to the Windows registry

Purpose:

This module provides a simple interface to the Windows registry. It contains a Registry class that can be used to interact with the registry; and is supported by the Key and Value classes.

This module also provides the following objects that can be used to directly access the registry methods for each root HKEY.

  • hkcr = HKEY_CLASSES_ROOT

  • hkcu = HKEY_CURRENT_USER

  • hklm = HKEY_LOCAL_MACHINE

  • hkus = HKEY_USERS

  • hkcc = HKEY_CURRENT_CONFIG

Platform:

Windows | Python 3.6+

Developer:

M Critchard

Email:

support@s3dev.uk

Example:

For a use example, refer to the Registry docstring.

class registry.Key(key)[source]

This class encapsulates the Windows registry keys.

It provides methods that can be used to interact with the values of the key.

__init__(key)[source]

Initialise a private self._key attribute.

This attribute is used throughout the class to provide a set of key specific functions.

get_value(name)[source]

Get the key value specified by name.

Parameters:

name (str) – Name of the registry key.

Returns:

If the value exists, a value object is returned, otherwise None is returned.

set_data(name: str, data: str)[source]

Set the data of the key value specified by name.

Parameters:
  • name (str) – Name of the registry key.

  • data (str) – Data to store into the key.

Note

Currently, only strings or unicode values are supported.

class registry.Registry(hkey)[source]

This class encapsulates the Windows registry.

It provides methods that can be used to interact with the keys and values in the registry.

Example:

Example of how to get the value of a registry key:

>>> from utils4 import registry

>>> hkcu = registry.hkcu
>>> my_value = hkcu.get_value('control panel\desktop', 'wallpaper')
>>> my_wallpaper = my_value.data
__init__(hkey)[source]

Initialise a private self._hkey attribute.

This attribute is used throughout the class to provide a set of hkey specific functions.

_open_key(path: str, sam)[source]

Open and return the key specified by the path.

Parameters:
  • path (str) – Path to the registry key.

  • sam – The SAM privilege level used to open the key.

Design:

The key is opened with privilege level defined by sam. The user is notified if an error occurs.

Returns:

If opened successfully, a winreg.OpenKey object is returned, otherwise None is returned.

create_key(path: str)[source]

Create the key specified by the path.

Parameters:

path (str) – Path to the registry key.

Design:

If a creation error occurs, the user is notified.

Returns:

If created successfully, a winreg.CreateKey object is returned, otherwise None is returned.

delete_key(path: str)[source]

Delete the key specified by the path.

Parameters:

path (str) – Path to the registry key.

Note

All of the values under the key are also deleted but subkeys cannot be deleted with this method. The user is notified if an error occurs.

Returns:

If deleted successfully, a winreg.DeleteKey object is returned, otherwise None is returned.

get_value(path: str, name: str)[source]

Get the key value specified by the path and name

Parameters:
  • path (str) – Path to the registry key.

  • name (str) – Name of the registry key.

Returns:

If the value exists, a value object is returned, otherwise None is returned.

set_data(path: str, name: str, data: str)[source]

Set the data of the key value specified by the path and name.

Parameters:
  • path (str) – Path to the registry key.

  • name (str) – Name of the registry key.

  • data (str) – Data to store into the key.

Design:

If the value doesn’t exist, this method creates it and sets its type and data accordingly. A check is performed to determine if the operation has been completed successfully. If it hasn’t the user is notified.

Returns:

The value of the registry key.

class registry.Value(name: str, regtype, data)[source]

This class encapsulates the Windows registry values.

It provides the following attributes to mimic the attributes of a value in the registry:

  • name

  • type

  • data

__init__(name: str, regtype, data)[source]

Set the name, type and data attributes.