Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
A.Shpak committed Nov 14, 2024
1 parent 699c76a commit 2ec9d90
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
41 changes: 26 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@
[![PyPI](https://img.shields.io/pypi/v/winregistry.svg)](https://pypi.python.org/pypi/winregistry)
[![PyPI](https://img.shields.io/pypi/dm/winregistry.svg)](https://pypi.python.org/pypi/winregistry)

Python library aimed at working with Windows Registry
Python package aimed at working with Windows Registry

## Installation

Install via PyPI:

```bash
pip install winregistry
```

## Usage

```py
```python
import winreg

from winregistry import connect_registry

_SUB_KEY_NAME = "SOFTWARE\_REMOVE_ME_"

if __name__ == "__main__":
with connect_registry(winreg.HKEY_LOCAL_MACHINE) as hklm:
hklm.create_key(_SUB_KEY_NAME)
with hklm.open_key(_SUB_KEY_NAME) as subkey:
subkey.set_value("remove_me", winreg.REG_SZ, "Don't forget remove me!")
value = subkey.read_value("remove_me")
print(f"{value=}: {value.data}")
value.data = "remove me again!"
print(f"{value=}: {value.data}")
print(f"{list(subkey.values)=}")
subkey.delete_value("remove_me")
print(f"{list(subkey.values)=}")
hklm.delete_key(_SUB_KEY_NAME)

# connect to registry
with connect_registry(winreg.HKEY_LOCAL_MACHINE) as hklm:

# create registry key
hklm.create_key(_SUB_KEY_NAME)

# open registry subkey
with hklm.open_key(_SUB_KEY_NAME) as subkey:

# set value to subkey
subkey.set_value("remove_me", winreg.REG_SZ, "Remove me!")

# read value
value = subkey.read_value("remove_me")

# change data of value
value.data = "Don't forget remove me!"

# delete value in subkey
hklm.delete_key(_SUB_KEY_NAME)
```
33 changes: 33 additions & 0 deletions winregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,19 @@ def name(
def data(
self,
) -> Any:
"""
The data of the registry value item
"""
return self.raw_info.data

@data.setter
def data(
self,
value: Any,
) -> None:
"""
Stores data in the value field of an open registry key
"""
winreg.SetValueEx(
self._hkey,
self._name,
Expand All @@ -113,6 +119,9 @@ def data(
def type(
self,
) -> int:
"""
An integer giving the registry type for this value
"""
return self.raw_info.type


Expand Down Expand Up @@ -160,12 +169,18 @@ def from_index(
def child_keys_count(
self,
) -> int:
"""
Number of sub keys this key has
"""
return self.raw_info.child_keys_count

@property
def values_count(
self,
) -> int:
"""
Number of values this key has
"""
return self.raw_info.values_count

@property
Expand Down Expand Up @@ -227,13 +242,19 @@ def delete_key(
self,
sub_key: str,
) -> None:
"""
Deletes the specified key
"""
winreg.DeleteKey(self._hkey, sub_key)
self._auto_refresh()

def read_value(
self,
name: str,
) -> Value:
"""
Retrieves data for a specified value name
"""
return Value(
hkey=self._hkey,
name=name,
Expand All @@ -245,19 +266,28 @@ def set_value(
type: int,
value: Any,
) -> None:
"""
Associates a value with a specified key
"""
winreg.SetValueEx(self._hkey, name, 0, type, value)
self._auto_refresh()

def delete_value(
self,
name: str,
) -> None:
"""
Removes a named value from a registry key
"""
winreg.DeleteValue(self._hkey, name)
self._auto_refresh()

def close(
self,
) -> None:
"""
Closes a previously opened registry key
"""
self._hkey.Close()

def __enter__(
Expand All @@ -279,6 +309,9 @@ def connect_registry(
computer_name: str | None = None,
auto_refresh: bool = True,
) -> Key:
"""
Establishes a connection with registry
"""
return Key(
hkey=winreg.ConnectRegistry(computer_name, key),
auto_refresh=auto_refresh,
Expand Down

0 comments on commit 2ec9d90

Please sign in to comment.