Skip to content

Commit

Permalink
isort and protected _get_handler (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
shpaker committed Jul 9, 2021
1 parent 57e7106 commit 8a7f935
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.html
.xml
.idea/
.vscode/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ repos:
-
repo: local
hooks:
# -
# id: isort
# name: isort
# entry: isort
# language: system
# files: ^(winregistry|test)/.+\.py$
-
id: isort
name: isort
entry: isort
language: system
files: ^(winregistry|test)/.+\.py$
-
id: black
name: black
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "winregistry"
version = "1.0.0"
version = "1.0.1"
description = "Library aimed at working with Windows registry"
authors = ["Aleksandr Shpak <shpaker@gmail.com>"]
readme = "readme.md"
Expand Down Expand Up @@ -29,6 +29,7 @@ pre-commit = "^2.13.0"
pytest = "^6.2.4"
black = "^21.6b0"
robotframework = "^4.0.3"
isort = "^5.9.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down Expand Up @@ -57,7 +58,7 @@ force_grid_wrap = 0
use_parentheses = true
ensure_newline_before_comments = true
line_length = 120
src_paths = ["soxyproxy", "test"]
src_paths = ["winregistry", "test"]
skip = [".mypy_cache", ".pytest_cache", "venv"]

[tool.pylint.messages_control]
Expand Down
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ TEST_REG_PATH = r"HKLM\SOFTWARE\_REMOVE_ME_"


if __name__ == "__main__":
with WinRegistry() as client:
client.create_key(TEST_REG_PATH)
client.write_entry(TEST_REG_PATH, "remove_me", "test")
test_entry = client.read_entry(TEST_REG_PATH, "remove_me")
assert test_entry.value == "test"
client.delete_entry(TEST_REG_PATH, "remove_me")
with WinRegistry() as client:
client.create_key(TEST_REG_PATH)
client.write_entry(TEST_REG_PATH, "remove_me", "test")
test_entry = client.read_entry(TEST_REG_PATH, "remove_me")
assert test_entry.value == "test"
client.delete_entry(TEST_REG_PATH, "remove_me")
```

Usage with ``Robot Testing Framework`` Library
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_parse_path() -> None:

def test_get_key_handle() -> None:
with WinRegistry() as reg:
handler = reg.get_handler(
handler = reg._get_handler(
r"HKLM\SOFTWARE",
access=KEY_READ,
key_wow64_32key=False,
Expand Down
21 changes: 12 additions & 9 deletions winregistry/winregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(
self._handler = None
self._root: Optional[HKEYType] = None

def get_handler(
def _get_handler(
self,
key: str,
access: int,
Expand All @@ -50,12 +50,15 @@ def get_handler(
)
return key_handle

def close(self) -> None:
if self._client:
self._client.Close()

def __enter__(self) -> "WinRegistry":
return self

def __exit__(self, exc_type, exc_val, exc_tb): # type: ignore
if self._client:
self._client.Close()
self.close()
if exc_val:
raise

Expand All @@ -65,7 +68,7 @@ def read_entry(
name: str,
key_wow64_32key: bool = False,
) -> RegEntry:
handle = self.get_handler(reg_key, KEY_READ, key_wow64_32key)
handle = self._get_handler(reg_key, KEY_READ, key_wow64_32key)
raw_value, raw_type = QueryValueEx(handle, name)
return RegEntry(
reg_key=reg_key,
Expand All @@ -85,7 +88,7 @@ def write_entry(
) -> None:
if isinstance(reg_type, int):
reg_type = WinregType(reg_type)
handle = self.get_handler(reg_key, KEY_SET_VALUE, key_wow64_32key)
handle = self._get_handler(reg_key, KEY_SET_VALUE, key_wow64_32key)
SetValueEx(handle, name, 0, reg_type.value, value)

def delete_entry(
Expand All @@ -94,15 +97,15 @@ def delete_entry(
name: str,
key_wow64_32key: bool = False,
) -> None:
handle = self.get_handler(key, KEY_SET_VALUE, key_wow64_32key)
handle = self._get_handler(key, KEY_SET_VALUE, key_wow64_32key)
DeleteValue(handle, name)

def read_key(
self,
name: str,
key_wow64_32key: bool = False,
) -> RegKey:
handle = self.get_handler(name, KEY_READ, key_wow64_32key)
handle = self._get_handler(name, KEY_READ, key_wow64_32key)
keys_num, values_num, modify = QueryInfoKey(handle)
modify_at = datetime(1601, 1, 1) + timedelta(microseconds=modify / 10)
keys = list()
Expand Down Expand Up @@ -138,7 +141,7 @@ def create_key(
while i < len(sub_keys) and not handler:
try:
current = "\\".join(sub_keys[: len(sub_keys) - i])
handler = self.get_handler(current, KEY_WRITE, key_wow64_32key)
handler = self._get_handler(current, KEY_WRITE, key_wow64_32key)
except FileNotFoundError:
i += 1
before_index = len(sub_keys) - i
Expand All @@ -156,5 +159,5 @@ def delete_key(
key_wow64_32key: bool = False,
) -> None:
parental, key_name = name.rsplit(sep="\\", maxsplit=1)
handle = self.get_handler(parental, KEY_WRITE, key_wow64_32key)
handle = self._get_handler(parental, KEY_WRITE, key_wow64_32key)
DeleteKey(handle, key_name)

0 comments on commit 8a7f935

Please sign in to comment.