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

This commit is contained in:
Faraphel 2022-07-11 00:16:03 +02:00
parent 804c9f38f6
commit d9ec57a04c
6 changed files with 42 additions and 84 deletions

View file

@ -1,6 +1,7 @@
import subprocess import subprocess
from pathlib import Path from pathlib import Path
import os import os
from typing import Callable
class WTError(Exception): class WTError(Exception):
@ -53,7 +54,7 @@ def better_wt_error(tools_path: Path | str):
return decorator 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 Run a command and return the output
:param args: command arguments :param args: command arguments
@ -67,7 +68,16 @@ def _run(tools_path: Path | str, *args) -> bytes:
).stdout ).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 Return a dictionary of a command that return value associated to a key with a equal sign
:param tools_path: tools to use :param tools_path: tools to use
@ -92,6 +102,15 @@ def _run_dict(tools_path: Path | str, *args) -> dict:
return d 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: def _run_popen(tools_path: Path | str, *args, universal_newlines=False) -> subprocess.Popen:
""" """
Run a command and return the process 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, bufsize=1 if universal_newlines else None,
universal_newlines=universal_newlines, 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)

View file

@ -1,26 +1,10 @@
from source.wt import * from source.wt import *
from source.wt import _run, _run_popen
tools_path = tools_szs_dir / ("wimgt.exe" if system == "win64" else "wimgt") tools_path = tools_szs_dir / ("wimgt.exe" if system == "win64" else "wimgt")
@better_wt_error(tools_path) _tools_run = get_tools_run_function(tools_path)
def _tools_run(*args) -> bytes: _tools_run_popen = get_tools_run_popen_function(tools_path)
"""
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)
class BMGPath: class BMGPath:

View file

@ -1,26 +1,10 @@
from source.wt import * from source.wt import *
from source.wt import _run, _run_popen
tools_path = tools_szs_dir / ("wimgt.exe" if system == "win64" else "wimgt") tools_path = tools_szs_dir / ("wimgt.exe" if system == "win64" else "wimgt")
@better_wt_error(tools_path) _tools_run = get_tools_run_function(tools_path)
def _tools_run(*args) -> bytes: _tools_run_popen = get_tools_run_popen_function(tools_path)
"""
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)
@better_wt_error(tools_path) @better_wt_error(tools_path)

View file

@ -0,0 +1,3 @@
from source.wt import *
tools_path = tools_szs_dir / ("wstrt.exe" if system == "win64" else "wstrt")

View file

@ -4,24 +4,8 @@ from source.wt import _run, _run_dict
tools_path = tools_szs_dir / ("wszst.exe" if system == "win64" else "wszst") tools_path = tools_szs_dir / ("wszst.exe" if system == "win64" else "wszst")
@better_wt_error(tools_path) _tools_run = get_tools_run_function(tools_path)
def _tools_run(*args) -> bytes: _tools_run_dict = get_tools_run_dict_function(tools_path)
"""
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)
@better_wt_error(tools_path) @better_wt_error(tools_path)

View file

@ -4,38 +4,13 @@ import shutil
from typing import Generator from typing import Generator
from source.wt import * 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") tools_path = tools_wit_dir / ("wit.exe" if system == "win64" else "wit")
@better_wt_error(tools_path) _tools_run = get_tools_run_function(tools_path)
def _tools_run(*args) -> bytes: _tools_run_dict = get_tools_run_dict_function(tools_path)
""" _tools_run_popen = get_tools_run_popen_function(tools_path)
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)
class Extension(enum.Enum): class Extension(enum.Enum):