Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
A.Shpak committed Nov 16, 2024
1 parent 4fa9d8c commit 288372d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.html
.xml
*.html
*.xml
.idea/
.vscode/

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ KEY_NAME_FOR_TESTING = 'HKLM\SOFTWARE\_REMOVE_ME_'
# create key
winregistry.create_key(KEY_NAME_FOR_TESTING)
winregistry.create_key(f'{KEY_NAME_FOR_TESTING}\some_subkey')
print(winregistry.child_keys_names(f'{KEY_NAME_FOR_TESTING}\_REMOVE_ME_'))
print(list(winregistry.child_keys_names(f'{KEY_NAME_FOR_TESTING}\_REMOVE_ME_')))

# manipulations with values
winregistry.values_names(KEY_NAME_FOR_TESTING)
Expand All @@ -38,6 +38,7 @@ print(winregistry.read_value_data(KEY_NAME_FOR_TESTING, 'smth'))
winregistry.delete_value(KEY_NAME_FOR_TESTING, 'smth')

# delete key
winregistry.delete_key(f'{KEY_NAME_FOR_TESTING}\some_subkey')
winregistry.delete_key(KEY_NAME_FOR_TESTING)
```

Expand Down
37 changes: 22 additions & 15 deletions winregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def info(
class Value(
RegEntity,
):
raw_info: ValueInfo

def __init__(
self,
Expand Down Expand Up @@ -161,7 +160,6 @@ def type(
class Key(
RegEntity,
):
raw_info: KeyInfo

def __init__(
self,
Expand All @@ -176,7 +174,7 @@ def __init__(
def refresh(
self,
) -> None:
self._raw_info = KeyInfo(
self._info = KeyInfo(
*winreg.QueryInfoKey(
self._hkey,
)
Expand Down Expand Up @@ -220,7 +218,7 @@ def values_count(
def modified_at(
self,
) -> datetime:
return datetime(1601, 1, 1) + timedelta(microseconds=self._raw_info.modified_at / 10)
return datetime(1601, 1, 1) + timedelta(microseconds=self.info.modified_at / 10)

@property
def child_keys_names(
Expand Down Expand Up @@ -394,45 +392,54 @@ def open_key(


@contextmanager
def read_value(key_name: str, value_name: Any) -> Generator[Value, None, None]:
with open_key(key_name, sub_key_access=winreg.KEY_ALL_ACCESS) as client:
def read_value(
key_name: str,
value_name: Any,
auto_refresh: bool = True,
) -> Generator[Value, None, None]:
with open_key(key_name, sub_key_access=winreg.KEY_ALL_ACCESS, auto_refresh=auto_refresh) as client:
yield client.read_value(name=value_name)


def read_value_data(key_name: str, value_name: Any) -> Any:
with open_key(key_name, sub_key_access=winreg.KEY_ALL_ACCESS) as client:
with open_key(key_name, sub_key_access=winreg.KEY_ALL_ACCESS, auto_refresh=False) as client:
value = client.read_value(name=value_name)
return value.data


def create_key(key_name: str) -> None:
key_name, sub_key_name = key_name.split('\\', maxsplit=1)
with open_key(key_name, sub_key=sub_key_name, sub_key_ensure=True, sub_key_access=winreg.KEY_ALL_ACCESS) as client:
client.create_key(sub_key_name)
sub_key_name, new_key_name = sub_key_name.rsplit('\\', maxsplit=1)

with open_key(
key_name, sub_key=sub_key_name, sub_key_ensure=True, sub_key_access=winreg.KEY_ALL_ACCESS, auto_refresh=False
) as client:
client.create_key(new_key_name)


def child_keys_names(key_name: str) -> None:
with open_key(key_name) as client:
yield from client.child_keys_names
with open_key(key_name, auto_refresh=False) as key:
key.refresh()
yield from key.child_keys_names


def delete_key(key_name: str) -> None:
key_name, sub_key_name = key_name.rsplit('\\', maxsplit=1)
with open_key(key_name, sub_key_access=winreg.KEY_ALL_ACCESS) as client:
with open_key(key_name, sub_key_access=winreg.KEY_ALL_ACCESS, auto_refresh=False) as client:
client.delete_key(sub_key_name)


def set_value(key_name: str, value_name: str, type: int, data: Any = None) -> None:
with open_key(key_name, sub_key_access=winreg.KEY_ALL_ACCESS) as key:
with open_key(key_name, sub_key_access=winreg.KEY_ALL_ACCESS, auto_refresh=False) as key:
key.set_value(name=value_name, type=type, data=data)


def values_names(key_name: str) -> None:
with open_key(key_name) as client:
with open_key(key_name, auto_refresh=False) as client:
for value in client.values:
yield value.name


def delete_value(key_name: str, value_name: str) -> None:
with open_key(key_name, sub_key_access=winreg.KEY_ALL_ACCESS) as key:
with open_key(key_name, sub_key_access=winreg.KEY_ALL_ACCESS, auto_refresh=False) as key:
key.delete_value(value_name)

0 comments on commit 288372d

Please sign in to comment.