Skip to content

Registry Path

RegistryPath

RegistryPath(path: str)

Represents a registry location in Windows.

Provides a convenient interface for interacting with Windows Registry paths. This class mimics the API of pathlib.Path, allowing intuitive manipulation of registry paths.

Source code in context_menu_toolkit/registry/registry_structs/registry_path.py
42
43
44
45
46
47
def __init__(self, path: str) -> None:
    """Construct RegistryLocation object from a human-readable location string."""
    normalized_path = self._normalize_raw_path(path)
    self._validate_path(normalized_path)

    self._path = normalized_path

top_level_key property

top_level_key: TopLevelKey

The top level key of the path.

Example
RegistryPath("HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*").top_level_key
# ["HKEY_LOCAL_MACHINE", "SOFTWARE", "Classes", "*"]

subkeys property

subkeys: str

The path without the top level key, i.e. the sub keys.

Example
RegistryPath("HKEY_CURRENT_USER\Software\classes\*").subkeys
# Software\classes\*

parts property

parts: list[str]

The path split into parts.

Example
RegistryPath("HKEY_CURRENT_USER\Software\classes\*").parts
# ["HKEY_CURRENT_USER", "Software", "classes", "*"]

read

read() -> RegistryKey

Read key at self location, recursively.

Source code in context_menu_toolkit/registry/registry_structs/registry_path.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def read(self) -> RegistryKey:
    """Read key at `self` location, recursively."""
    tree = RegistryKey(name=self.parts[-1])
    tree.values.extend(self.read_values())

    with (
        winreg.OpenKey(
            self.top_level_key,
            self.subkeys,
            RESERVED_FLAG,
            winreg.KEY_READ,
        ) as key,
        contextlib.suppress(OSError),
    ):
        for i in range(1024):
            subkey_name = winreg.EnumKey(key, i)
            sub_location = self / subkey_name
            tree.add_subkey(sub_location.read())
    return tree

read_values

read_values() -> list[RegistryValue]

Read all values of the key at current location.

Source code in context_menu_toolkit/registry/registry_structs/registry_path.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def read_values(self) -> list[RegistryValue]:
    """Read all values of the key at current location."""
    values: list[RegistryValue] = []

    with (
        winreg.OpenKey(
            self.top_level_key,
            self.subkeys,
            RESERVED_FLAG,
            winreg.KEY_READ,
        ) as key,
        contextlib.suppress(OSError),
    ):
        for i in range(1024):
            name, data, data_type = winreg.EnumValue(key, i)
            values.append(
                RegistryValue(
                    name=name,
                    type=DataType(data_type),
                    data=data,
                ),
            )

    return values

write

write(key: RegistryKey) -> None

Add RegistryKey to current location.

Source code in context_menu_toolkit/registry/registry_structs/registry_path.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def write(self, key: RegistryKey) -> None:
    """Add RegistryKey to current location."""
    new_loc = self / key.name
    winreg.CloseKey(
        winreg.CreateKey(
            new_loc.top_level_key,
            new_loc.subkeys,
        ),
    )

    for value in key.values:
        new_loc.write_value(value)

    for subkey in key.subkeys:
        new_loc.write(subkey)

write_value

write_value(value: RegistryValue) -> None

Add value to current location.

Source code in context_menu_toolkit/registry/registry_structs/registry_path.py
151
152
153
154
155
156
157
158
159
def write_value(self, value: RegistryValue) -> None:
    """Add value to current location."""
    with winreg.OpenKey(
        self.top_level_key,
        self.subkeys,
        RESERVED_FLAG,
        winreg.KEY_WRITE,
    ) as key:
        winreg.SetValueEx(key, value.name, RESERVED_FLAG, value.type, value.data)

Top level keys

TopLevelKey

Bases: IntEnum

An enum representing the possible top-level registry keys.

HKEY_CLASSES_ROOT

HKEY_CLASSES_ROOT = HKEY_CLASSES_ROOT

HKEY_CURRENT_USER

HKEY_CURRENT_USER = HKEY_CURRENT_USER

HKEY_LOCAL_MACHINE

HKEY_LOCAL_MACHINE = HKEY_LOCAL_MACHINE

HKEY_USERS

HKEY_USERS = HKEY_USERS

HKEY_CURRENT_CONFIG

HKEY_CURRENT_CONFIG = HKEY_CURRENT_CONFIG