Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
A.Shpak committed Nov 22, 2024
1 parent 98cc5b7 commit ed90a10
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
30 changes: 12 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pip install winregistry

```python
import winreg
from winregistry import open_key
from winregistry import open_key, open_value

# connect to registry and ensure sub-key
with open_key(
Expand Down Expand Up @@ -58,26 +58,20 @@ with open_key(
value="Remove me!",
)

# read value
# create value
with open_key(
winreg.HKEY_LOCAL_MACHINE,
sub_key="SOFTWARE\_REMOVE_ME_",
) as key:
value = key.read_value("remove_me")

# change data of value
with open_key(
winreg.HKEY_LOCAL_MACHINE,
sub_key="SOFTWARE\_REMOVE_ME_",
"HKLM\SOFTWARE\_REMOVE_ME_",
sub_key_ensure=True,
sub_key_access=winreg.KEY_SET_VALUE,
) as key:
value.data = "Don't forget remove me!"
key.set_value("foo", "SZ")

# delete value in subkey
with open_key(
winreg.HKEY_LOCAL_MACHINE,
sub_key="SOFTWARE\_REMOVE_ME_",
) as key:
value = key.delete_value("remove_me")
# read value
with open_value(
"HKLM\SOFTWARE\_REMOVE_ME_",
value_name="remove_me",
) as value:
...
```

## Usage with [Robot Testing Framework](https://robotframework.org/)
Expand Down
8 changes: 8 additions & 0 deletions winregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ def open_key(
"""
Opens the specified key
"""
if access != winreg.KEY_READ and self._is_auto_refreshable:
access = access | winreg.KEY_READ
return Key(
winreg.OpenKey(
key=self._hkey,
Expand All @@ -283,6 +285,8 @@ def create_key(
"""
Creates or opens the specified key
"""
if access != winreg.KEY_READ and self._is_auto_refreshable:
access = access | winreg.KEY_READ
key = Key(
winreg.CreateKeyEx(
key=self._hkey,
Expand Down Expand Up @@ -386,6 +390,8 @@ def open_key(
"""
Establishes a connection with registry
"""
if sub_key_access != winreg.KEY_READ and auto_refresh:
sub_key_access = sub_key_access | winreg.KEY_READ
if isinstance(key, str):
key, sub_key = _make_int_key(key, sub_key)
with Key(
Expand Down Expand Up @@ -415,6 +421,8 @@ def open_value(
auto_refresh: bool = True,
sub_key_access: int = winreg.KEY_READ,
) -> Generator[Value, None, None]:
if sub_key_access != winreg.KEY_READ and auto_refresh:
sub_key_access = sub_key_access | winreg.KEY_READ
with open_key(
key_name,
computer_name=computer_name,
Expand Down

0 comments on commit ed90a10

Please sign in to comment.