From e06dafdc92d6999ca405bc6ae0ee6e60f21811a6 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Tue, 26 Apr 2022 11:46:41 +0200 Subject: properly type and sanity check variables, replace relative imports, mypy --- tox.ini | 2 +- unixreg/__init__.py | 6 ++-- unixreg/functions.py | 78 +++++++++++++++++++++++++++------------------------- unixreg/key.py | 9 +++--- unixreg/utils.py | 23 ---------------- 5 files changed, 49 insertions(+), 69 deletions(-) delete mode 100644 unixreg/utils.py diff --git a/tox.ini b/tox.ini index d100e60..f824619 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py38 +envlist = py38,mypy [testenv] deps = pytest diff --git a/unixreg/__init__.py b/unixreg/__init__.py index bf99ffd..73733c3 100644 --- a/unixreg/__init__.py +++ b/unixreg/__init__.py @@ -9,6 +9,6 @@ __version__ = "0.1.1" try: from winreg import * except ImportError: - from .functions import * - from .constants import * - from .key import * + from unixreg.functions import * + from unixreg.constants import * + from unixreg.key import * diff --git a/unixreg/functions.py b/unixreg/functions.py index 2bd544d..ce3b9fe 100644 --- a/unixreg/functions.py +++ b/unixreg/functions.py @@ -9,15 +9,15 @@ from typing import Union from re import findall from tempfile import TemporaryDirectory from warnings import warn +from typing import Optional, List -from .key import RegKey -from .constants import STANDARD_RIGHTS_REQUIRED, KEY_WOW64_64KEY, KEY_WRITE, KEY_READ -from .utils import strict_types +from unixreg.key import RegKey +from unixreg.constants import STANDARD_RIGHTS_REQUIRED, KEY_WOW64_64KEY, KEY_WRITE, KEY_READ KEY_TYPE = Union[str, RegKey] SUBKEY_TYPE = Union[str, RegKey, None] -_KEY_CACHE = [] +_KEY_CACHE: List[RegKey] = [] _ENV_REPLACE = { "USERPROFILE": "HOME" } @@ -33,7 +33,6 @@ if not _CONFIG_DIR: warn(f"Could not find directory to put registry in. Falling back to {_CONFIG_DIR}") _CONFIG_DIR = os.path.join(_CONFIG_DIR, "unixreg") -@strict_types def __init_values(key: KEY_TYPE, sub_key: SUBKEY_TYPE = None, access = STANDARD_RIGHTS_REQUIRED): if isinstance(key, str): key = RegKey(key) @@ -45,11 +44,11 @@ def __init_values(key: KEY_TYPE, sub_key: SUBKEY_TYPE = None, access = STANDARD_ return key -@strict_types def __create_key(key: RegKey): - path = os.path.join(_CONFIG_DIR, key.key) + if _CONFIG_DIR and key and key.key: + path = os.path.join(_CONFIG_DIR, key.key) - os.makedirs(path, exist_ok=True) + os.makedirs(path, exist_ok=True) def CloseKey(key: RegKey): """ @@ -74,17 +73,14 @@ def ConnectRegistry(computer: Union[str, None], key: RegKey): return OpenKey(key, None) raise OSError("Not Implemented") -@strict_types def OpenKeyEx(key: RegKey, sub_key: SUBKEY_TYPE, reserved=0, access=KEY_READ): return CreateKeyEx(key, sub_key, reserved, access) OpenKey = OpenKeyEx -@strict_types def CreateKey(key: RegKey, sub_key: SUBKEY_TYPE): return CreateKeyEx(key, sub_key) -@strict_types def CreateKeyEx(key: RegKey, sub_key: SUBKEY_TYPE, reserved=0, access=KEY_WRITE): key = __init_values(key, sub_key, access) @@ -99,20 +95,22 @@ def DeleteKey(key: KEY_TYPE, sub_key: SUBKEY_TYPE): return DeleteKeyEx(key, sub_key) def DeleteKeyEx(key: KEY_TYPE, sub_key: SUBKEY_TYPE, access=KEY_WOW64_64KEY, reserved=0): - key = __init_values(key, sub_key, access) + kkey = __init_values(key, sub_key, access) - path = os.path.join(_CONFIG_DIR, key.key) - if os.path.isfile(path): - os.remove(path) + if _CONFIG_DIR: + path = os.path.join(_CONFIG_DIR, kkey.key) + if os.path.isfile(path): + os.remove(path) def DeleteValue(key: KEY_TYPE, value: str): - key = __init_values(key) + kkey = __init_values(key) - filepath = os.path.join(_CONFIG_DIR, key.key, value) - try: - os.remove(filepath) - except FileNotFoundError: - pass + if _CONFIG_DIR: + filepath = os.path.join(_CONFIG_DIR, kkey.key, value) + try: + os.remove(filepath) + except FileNotFoundError: + pass def EnumKey(key: KEY_TYPE, index: int): raise NotImplementedError("Not Implemented") @@ -142,36 +140,39 @@ def QueryInfoKey(key: KEY_TYPE): raise NotImplementedError("Not Implemented") def QueryValueEx(key: KEY_TYPE, sub_key: SUBKEY_TYPE) -> str: - key = __init_values(key, sub_key) + kkey = __init_values(key, sub_key) - filepath = os.path.join(_CONFIG_DIR, key.key) - with open(filepath, "r") as file: - return file.read() + if _CONFIG_DIR: + filepath = os.path.join(_CONFIG_DIR, kkey.key) + with open(filepath, "r") as file: + return file.read() + + return "" QueryValue = QueryValueEx -@strict_types def LoadKey(key: RegKey, sub_key: SUBKEY_TYPE, file_name: str): # this requires a win32 permission compatibility layer raise OSError("Not Implemented") -@strict_types def SaveKey(key: RegKey, file_name: str) -> None: # this requires a win32 permission compatibility layer raise OSError("Not Implemented") -def SetValue(key: KEY_TYPE, sub_key: SUBKEY_TYPE, typearg: int, value: str): +def SetValue(key: KEY_TYPE, sub_key: SUBKEY_TYPE, typearg: int, value: str) -> None: if isinstance(sub_key, RegKey): sub_key = sub_key.key - return SetValueEx(key, sub_key, 0, typearg, value) + if sub_key: + return SetValueEx(key, sub_key, 0, typearg, value) def SetValueEx(key: KEY_TYPE, value_name: str, reserved: int, typearg: int, value: str) -> None: - key = __init_values(key) + kkey = __init_values(key) - filepath = os.path.join(_CONFIG_DIR, key.key, value_name) - with open(filepath, "w") as file: - file.write(value) + if _CONFIG_DIR: + filepath = os.path.join(_CONFIG_DIR, kkey.key, value_name) + with open(filepath, "w") as file: + file.write(value) def DisableReflectionKey(key: KEY_TYPE): raise NotImplementedError("Not Implemented") @@ -184,7 +185,7 @@ def QueryReflectionKey(key: KEY_TYPE): # Non winreg functions -def LoadRegFile(file_name: str) -> str: +def LoadRegFile(file_name: str) -> Optional[str]: def _strip_quotes(val) -> str: _QUOTE_LIST = ("\"", '\'') @@ -201,7 +202,7 @@ def LoadRegFile(file_name: str) -> str: with open(file_name, "r") as reg: nextline = reg.readline() - key = None + key: Optional[str] = None while nextline: line = nextline.strip() @@ -222,7 +223,10 @@ def LoadRegFile(file_name: str) -> str: os.makedirs(key, exist_ok=True) - with open(os.path.join(_CONFIG_DIR, key.key, name), "w") as regvalue: - regvalue.write(value) + if _CONFIG_DIR: + with open(os.path.join(_CONFIG_DIR, key, name), "w") as regvalue: + regvalue.write(value) print(f"[{key}] {name}={value}") + + return None \ No newline at end of file diff --git a/unixreg/key.py b/unixreg/key.py index eccf9b6..77932aa 100644 --- a/unixreg/key.py +++ b/unixreg/key.py @@ -2,12 +2,11 @@ """ Implements anything related to the Registry Handle """ +from __future__ import annotations import os from copy import deepcopy from typing import TypeVar, Union -RegKeyT = TypeVar("RegKeyT", bound="RegKey") - _HANDLE_COUNTER = 0 class RegKey: @@ -28,8 +27,8 @@ class RegKey: self.handle = _HANDLE_COUNTER self.access = access - def __add__(self, other: Union[str, RegKeyT]) -> RegKeyT: - if isinstance(other, __class__): + def __add__(self, other: Union[str, RegKey]) -> RegKey: + if isinstance(other, self.__class__): other = other.key if isinstance(other, str): @@ -40,7 +39,7 @@ class RegKey: raise TypeError("Invalid Type") - def __enter__(self) -> RegKeyT: + def __enter__(self) -> RegKey: return self def __exit__(self, *args, **kwargs): diff --git a/unixreg/utils.py b/unixreg/utils.py deleted file mode 100644 index 9435ceb..0000000 --- a/unixreg/utils.py +++ /dev/null @@ -1,23 +0,0 @@ -""" -Utilities -""" -from typing import get_args - -def strict_types(function): - """ - enforce type hinting on functions - """ - - def _decorator(*args, **kwargs): - hints = function.__annotations__ - all_args = kwargs.copy() - all_args.update(dict(zip(function.__code__.co_varnames, args))) - for argument, argument_type in [(i, type(j)) for i, j in all_args.items()]: - if (argument in hints and - argument_type != hints[argument] - and not isinstance(argument, get_args(hints[argument]))): - raise TypeError(f"{argument} is not {hints[argument]}") - - return function(*args, **kwargs) - - return _decorator -- cgit v1.2.3