From d26c722a87f35663887ca920a2cae9378e143915 Mon Sep 17 00:00:00 2001 From: "A.Shpak" Date: Tue, 12 Nov 2024 15:24:25 +0300 Subject: [PATCH] - --- .github/workflows/tests.yml | 17 +- tests/test_utils.py => test_winregistry.py | 0 tests/__init__.py | 0 winregistry.py | 199 ++++++--------------- 4 files changed, 59 insertions(+), 157 deletions(-) rename tests/test_utils.py => test_winregistry.py (100%) delete mode 100644 tests/__init__.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 555bc22..8227f0a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -13,27 +13,20 @@ jobs: strategy: matrix: python-version: - - "3.7" - - "3.8" - "3.9" - "3.10" - "3.11" - "3.12" - "3.13" - - "3.14" steps: - - - name: Set up Python ${{ matrix.python-version }} + - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v2 with: - python-version: ${{ matrix.python-version }} - - - uses: actions/checkout@v2 - - - name: Install dependencies + python-version: ${{ matrix.python-version }} + - uses: actions/checkout@v2 + - name: Install dependencies run: | python -m pip install poetry==1.1.7 poetry install - - - name: pytest + - name: pytest run: poetry run pytest -vv diff --git a/tests/test_utils.py b/test_winregistry.py similarity index 100% rename from tests/test_utils.py rename to test_winregistry.py diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/winregistry.py b/winregistry.py index e6de35d..72385b7 100644 --- a/winregistry.py +++ b/winregistry.py @@ -1,23 +1,24 @@ import typing as tp import winreg - from abc import ABC, abstractmethod from contextlib import contextmanager from datetime import datetime, timedelta -from _winregistry._utils import str_to_winreg +def _cast_winreg(value: str) -> tp.Any: + if not (obj := getattr(winreg, value)): + raise ValueError(f'unknown value {value}') + return obj -class _RegEntity(ABC): - _winreg_pkg = winreg +class _RegEntity(ABC): def __init__(self, reg_key, wow64_32=False): self._reg_key = reg_key - self._wow = self._winreg_pkg.wow64_32 if wow64_32 else self._winreg_pkg.KEY_WOW64_64KEY - self._raw = self._get_reg_raw_data() + self._wow = winreg.KEY_WOW64_32KEY if wow64_32 else winreg.KEY_WOW64_64KEY + self._raw = self._get_raw() @abstractmethod - def _get_reg_raw_data(self): ... + def _get_raw(self): ... def _get_access_key(self, winreg_access_mode) -> int: return winreg_access_mode | self._wow @@ -28,8 +29,8 @@ def __init__(self, reg_key, name, wow64_32=False): super().__init__(reg_key, wow64_32) self._name = name - def _get_reg_raw_data(self): - return self._winreg_pkg.QueryValueEx(self._reg_key, self._name) + def _get_raw(self): + return winreg.QueryValueEx(self._reg_key, self._name) @property def value(self): @@ -50,8 +51,8 @@ def delete(self): ... class RegKey(_RegEntity): - def _get_reg_raw_data(self): - return self._winreg_pkg.QueryInfoKey(self._reg_key) + def _get_raw(self): + return winreg.QueryInfoKey(self._reg_key) def close(self): self._reg_key.Close() @@ -67,7 +68,7 @@ def modified_at(self): @property def child_keys_names(self) -> tp.Generator[str, None]: for name in range(self.child_keys_count()): - yield self._winreg_pkg.EnumKey(self._reg_key, name) + yield winreg.EnumKey(self._reg_key, name) @property def child_keys(self) -> tp.Generator[None, str]: @@ -82,27 +83,27 @@ def child_keys(self) -> tp.Generator[None, str]: @contextmanager def open_key(self, name: str): key = RegKey( - self._winreg_pkg.OpenKey( + winreg.OpenKey( key=self._reg_key, sub_key=name, reserved=0, - access=self._get_access_key(self._winreg_pkg.KEY_READ), + access=self._get_access_key(winreg.KEY_READ), ) ) yield key key.close() def create_key(self, name): - self._winreg_pkg.CreateKeyEx( - key=self._reg_key, # type: ignore + winreg.CreateKeyEx( + key=self._reg_key, sub_key=name, reserved=0, - access=self._get_access_key(self._winreg_pkg.KEY_WRITE), + access=self._get_access_key(winreg.KEY_WRITE), ) def delete_key(self, name, recursive=False): if not recursive: - self._winreg_pkg.DeleteKey(self._reg_key, name) + winreg.DeleteKey(self._reg_key, name) def read_value(self, name): return RegValue(self._reg_key, name, self._wow) @@ -127,131 +128,39 @@ def connect(cls, root, host=None, wow64_32=False) -> tp.Self: ) -def read_registry_key( - key: str, - wow64_32: bool = False, - host=None, -) -> RegKey: - """Reading registry key""" - with WinRegistry() as client: - return client.read_key(key, wow64_32) - - -def create_registry_key( - key: str, - wow64_32: bool = False, - host=None, -) -> None: - """Creating registry key""" - with WinRegistry() as client: - client.create_key(key, wow64_32) - - -def delete_registry_key( - key: str, - wow64_32: bool = False, - host=None, -) -> None: - """Deleting registry key""" - with WinRegistry() as client: - client.delete_key(key, wow64_32) - - -def read_registry_value( - key: str, - value: tp.Any, - wow64_32: bool = False, - host=None, -) -> RegEntry: - """Reading value from registry""" - with WinRegistry() as client: - return client.read_entry(key, value, wow64_32) - - -def set_registry_value( - key: str, - value: str, - data: Any = None, - reg_type: str = "REG_SZ", - wow64_32: bool = False, - host=None, -) -> None: - """Writing (or creating) data in value""" - with WinRegistry() as client: - client.write_entry(key, value, data, 'WinregType'[reg_type], wow64_32) - - -def delete_registry_value( - key: str, - value: str, - wow64_32: bool = False, - host=None, -) -> None: - """Deleting value from registry""" - with WinRegistry() as client: - client.delete_entry(key, value, wow64_32) - - -class robot: - def __init__(self, root, host=None, wow64_32=False): ... - def read_registry_key( - key: str, - wow64_32: bool = False, - ) -> RegKey: - """Reading registry key""" - with WinRegistry() as client: - return client.read_key(key, wow64_32) - - def create_registry_key( - self, - key: str, - wow64_32: bool = False, - ) -> None: - """Creating registry key""" - with WinRegistry() as client: - client.create_key(key, wow64_32) - - def delete_registry_key( - self, - key: str, - wow64_32: bool = False, - ) -> None: - """Deleting registry key""" - with WinRegistry() as client: - client.delete_key(key, wow64_32) - - def read_registry_value( - self, - key: str, - value: tp.Any, - wow64_32: bool = False, - ) -> RegEntry: - """Reading value from registry""" - with WinRegistry() as client: - return client.read_entry(key, value, wow64_32) - - def set_registry_value( - self, - key: str, - value: str, - data: Any = None, - reg_type: str = "REG_SZ", - wow64_32: bool = False, - ) -> None: - """Writing (or creating) data in value""" - with WinRegistry() as client: - client.write_entry(key, value, data, 'WinregType'[reg_type], wow64_32) - - def delete_registry_value( - self, - key: str, - value: str, - wow64_32: bool = False, - ) -> None: - """Deleting value from registry""" - with WinRegistry() as client: - client.delete_entry(key, value, wow64_32) - - -__all__ = ['WinRegistry', 'RegKey', 'RegValue', 'robot'] +# +# class robot: +# def __init__(self, root, host=None, wow64_32=False): +# self._cls_params = { +# 'wow64_32': wow64_32, +# 'host': host, +# } +# +# def create_registry_key(self, key_name: str, wow64_32: bool = False) -> None: +# """Creating registry key""" +# with WinRegistry(**self._cls_params) as client: +# client.create_key(key, wow64_32) +# +# def delete_registry_key(self, key_name: str) -> None: +# """Deleting registry key""" +# with WinRegistry(**self._cls_params) as client: +# client.delete_key(key, wow64_32) +# +# def read_registry_value(self, key: str, value: tp.Any) -> RegEntry: +# """Reading value from registry""" +# with WinRegistry(**self._cls_params) as client: +# return client.read_entry(key, value) +# +# def set_registry_value(self, key: str, value: str, data: tp.Any = None, reg_type: str = "REG_SZ") -> None: +# """Writing (or creating) data in value""" +# with WinRegistry(**self._cls_params) as client: +# client.write_entry(key, value, data, 'WinregType'[reg_type], wow64_32) +# +# def delete_registry_value(self, key: str, value: str, wow64_32: bool = False) -> None: +# """Deleting value from registry""" +# with WinRegistry(**self._cls_params) as client: +# client.delete_entry(key, value, wow64_32) + + +__all__ = ['WinRegistry', 'RegKey', 'RegValue'] __version__ = '0.0.0'