Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
A.Shpak committed Nov 12, 2024
1 parent f87febd commit d26c722
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 157 deletions.
17 changes: 5 additions & 12 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
File renamed without changes.
Empty file removed tests/__init__.py
Empty file.
199 changes: 54 additions & 145 deletions winregistry.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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()
Expand All @@ -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]:
Expand All @@ -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)
Expand All @@ -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'

0 comments on commit d26c722

Please sign in to comment.