From d9ec57a04cbc11dfac6b69b2d3f4e2b4322293ef Mon Sep 17 00:00:00 2001 From: Faraphel Date: Mon, 11 Jul 2022 00:16:03 +0200 Subject: [PATCH] compacted _tools_run, _tools_run_popen and _tools_run_dict by creating a get function returning the shortcut function for every module from the tools_path --- source/wt/__init__.py | 32 ++++++++++++++++++++++++++++++-- source/wt/bmg.py | 20 ++------------------ source/wt/img.py | 20 ++------------------ source/wt/str.py | 3 +++ source/wt/szs.py | 20 ++------------------ source/wt/wit.py | 31 +++---------------------------- 6 files changed, 42 insertions(+), 84 deletions(-) diff --git a/source/wt/__init__.py b/source/wt/__init__.py index 10b6995..1ad1876 100644 --- a/source/wt/__init__.py +++ b/source/wt/__init__.py @@ -1,6 +1,7 @@ import subprocess from pathlib import Path import os +from typing import Callable class WTError(Exception): @@ -53,7 +54,7 @@ def better_wt_error(tools_path: Path | str): return decorator -def _run(tools_path: Path | str, *args) -> bytes: +def _run(tools_path: Path | str, *args, **kwargs) -> bytes: """ Run a command and return the output :param args: command arguments @@ -67,7 +68,16 @@ def _run(tools_path: Path | str, *args) -> bytes: ).stdout -def _run_dict(tools_path: Path | str, *args) -> dict: +def get_tools_run_function(tools_path: Path | str) -> Callable: + """ + Return a new function with the tool argument already entered + :param tools_path: path to the tool + :return: new function + """ + return lambda *args, **kwargs: better_wt_error(tools_path)(_run)(tools_path, *args, **kwargs) + + +def _run_dict(tools_path: Path | str, *args, **kwargs) -> dict: """ Return a dictionary of a command that return value associated to a key with a equal sign :param tools_path: tools to use @@ -92,6 +102,15 @@ def _run_dict(tools_path: Path | str, *args) -> dict: return d +def get_tools_run_dict_function(tools_path: Path | str) -> Callable: + """ + Return a new function with the tool argument already entered + :param tools_path: path to the tool + :return: new function + """ + return lambda *args, **kwargs: better_wt_error(tools_path)(_run_dict)(tools_path, *args, **kwargs) + + def _run_popen(tools_path: Path | str, *args, universal_newlines=False) -> subprocess.Popen: """ Run a command and return the process @@ -106,3 +125,12 @@ def _run_popen(tools_path: Path | str, *args, universal_newlines=False) -> subpr bufsize=1 if universal_newlines else None, universal_newlines=universal_newlines, ) + + +def get_tools_run_popen_function(tools_path: Path | str) -> Callable: + """ + Return a new function with the tool argument already entered + :param tools_path: path to the tool + :return: new function + """ + return lambda *args, **kwargs: _run_popen(tools_path, *args, **kwargs) diff --git a/source/wt/bmg.py b/source/wt/bmg.py index 7e3bbe1..f254279 100644 --- a/source/wt/bmg.py +++ b/source/wt/bmg.py @@ -1,26 +1,10 @@ from source.wt import * -from source.wt import _run, _run_popen tools_path = tools_szs_dir / ("wimgt.exe" if system == "win64" else "wimgt") -@better_wt_error(tools_path) -def _tools_run(*args) -> bytes: - """ - Return a command with wbmgt and return the output - :param args: command arguments - :return: the output of the command - """ - return _run(tools_path, *args) - - -def _tools_run_popen(*args, **kwargs) -> subprocess.Popen: - """ - Return a popen of command with wbmgt - :param args: command arguments - :return: the process of the command - """ - return _run_popen(tools_path, *args, **kwargs) +_tools_run = get_tools_run_function(tools_path) +_tools_run_popen = get_tools_run_popen_function(tools_path) class BMGPath: diff --git a/source/wt/img.py b/source/wt/img.py index 9d17b31..b4801b4 100644 --- a/source/wt/img.py +++ b/source/wt/img.py @@ -1,26 +1,10 @@ from source.wt import * -from source.wt import _run, _run_popen tools_path = tools_szs_dir / ("wimgt.exe" if system == "win64" else "wimgt") -@better_wt_error(tools_path) -def _tools_run(*args) -> bytes: - """ - Return a command with wimgt and return the output - :param args: command arguments - :return: the output of the command - """ - return _run(tools_path, *args) - - -def _tools_run_popen(*args) -> subprocess.Popen: - """ - Return a popen of command with wimgt - :param args: command arguments - :return: the process of the command - """ - return _run_popen(tools_path, *args) +_tools_run = get_tools_run_function(tools_path) +_tools_run_popen = get_tools_run_popen_function(tools_path) @better_wt_error(tools_path) diff --git a/source/wt/str.py b/source/wt/str.py index e69de29..a20cd65 100644 --- a/source/wt/str.py +++ b/source/wt/str.py @@ -0,0 +1,3 @@ +from source.wt import * + +tools_path = tools_szs_dir / ("wstrt.exe" if system == "win64" else "wstrt") \ No newline at end of file diff --git a/source/wt/szs.py b/source/wt/szs.py index 0abba38..700f40f 100644 --- a/source/wt/szs.py +++ b/source/wt/szs.py @@ -4,24 +4,8 @@ from source.wt import _run, _run_dict tools_path = tools_szs_dir / ("wszst.exe" if system == "win64" else "wszst") -@better_wt_error(tools_path) -def _tools_run(*args) -> bytes: - """ - Return a command with wszst and return the output - :param args: command arguments - :return: the output of the command - """ - return _run(tools_path, *args) - - -@better_wt_error(tools_path) -def _tools_run_dict(*args) -> dict: - """ - Return a dictionary of a command that return value associated to a key with a equal sign - :param args: others arguments - :return: the dictionary - """ - return _run_dict(tools_path, *args) +_tools_run = get_tools_run_function(tools_path) +_tools_run_dict = get_tools_run_dict_function(tools_path) @better_wt_error(tools_path) diff --git a/source/wt/wit.py b/source/wt/wit.py index 0301d2f..c73db7a 100644 --- a/source/wt/wit.py +++ b/source/wt/wit.py @@ -4,38 +4,13 @@ import shutil from typing import Generator from source.wt import * -from source.wt import _run, _run_dict, _run_popen tools_path = tools_wit_dir / ("wit.exe" if system == "win64" else "wit") -@better_wt_error(tools_path) -def _tools_run(*args) -> bytes: - """ - Return a command with wit and return the output - :param args: command arguments - :return: the output of the command - """ - return _run(tools_path, *args) - - -def _tools_run_popen(*args, **kwargs) -> subprocess.Popen: - """ - Return a command with wit and return the output - :param args: command arguments - :return: the output of the command - """ - return _run_popen(tools_path, *args, **kwargs) - - -@better_wt_error(tools_path) -def _tools_run_dict(*args) -> dict: - """ - Return a dictionary of a command that return value associated to a key with a equal sign - :param args: others arguments - :return: the dictionary - """ - return _run_dict(tools_path, *args) +_tools_run = get_tools_run_function(tools_path) +_tools_run_dict = get_tools_run_dict_function(tools_path) +_tools_run_popen = get_tools_run_popen_function(tools_path) class Extension(enum.Enum):