From a04f7286b66a813dc58b0b55fe39a3e0dfc60ea8 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Wed, 8 Jun 2022 16:24:02 +0200 Subject: [PATCH] started working on the szs and wit wrapper --- source/mkw/Arena.py | 0 source/mkw/Cup.py | 0 source/mkw/Game.py | 0 source/mkw/ModConfig.py | 0 source/mkw/Track.py | 0 source/mkw/TrackGroup.py | 0 source/mkw/__init__.py | 0 source/wt/__init__.py | 39 +++++++++++++++++++++++++++++++++++ source/wt/bmg.py | 0 source/wt/img.py | 0 source/wt/str.py | 0 source/wt/szs.py | 44 ++++++++++++++++++++++++++++++++++++++++ 12 files changed, 83 insertions(+) create mode 100644 source/mkw/Arena.py create mode 100644 source/mkw/Cup.py create mode 100644 source/mkw/Game.py create mode 100644 source/mkw/ModConfig.py create mode 100644 source/mkw/Track.py create mode 100644 source/mkw/TrackGroup.py create mode 100644 source/mkw/__init__.py create mode 100644 source/wt/__init__.py create mode 100644 source/wt/bmg.py create mode 100644 source/wt/img.py create mode 100644 source/wt/str.py create mode 100644 source/wt/szs.py diff --git a/source/mkw/Arena.py b/source/mkw/Arena.py new file mode 100644 index 0000000..e69de29 diff --git a/source/mkw/Cup.py b/source/mkw/Cup.py new file mode 100644 index 0000000..e69de29 diff --git a/source/mkw/Game.py b/source/mkw/Game.py new file mode 100644 index 0000000..e69de29 diff --git a/source/mkw/ModConfig.py b/source/mkw/ModConfig.py new file mode 100644 index 0000000..e69de29 diff --git a/source/mkw/Track.py b/source/mkw/Track.py new file mode 100644 index 0000000..e69de29 diff --git a/source/mkw/TrackGroup.py b/source/mkw/TrackGroup.py new file mode 100644 index 0000000..e69de29 diff --git a/source/mkw/__init__.py b/source/mkw/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/source/wt/__init__.py b/source/wt/__init__.py new file mode 100644 index 0000000..be058c6 --- /dev/null +++ b/source/wt/__init__.py @@ -0,0 +1,39 @@ +import subprocess +from pathlib import Path + +tools_szs_dir = Path("./tools/szs/") +tools_wit_dir = Path("./tools/wit/") + + +class WTError(Exception): + def __init__(self, tool_path: Path, return_code: int): + try: + error = subprocess.run( + [tool_path, "ERROR", str(return_code)], + stdout=subprocess.PIPE, + check=True, + creationflags=subprocess.CREATE_NO_WINDOW, + ).stdout.decode() + except subprocess.CalledProcessError as e: + error = "- Can't get the error message -" + + super().__init__(f"{tool_path} raised {return_code} :\n{error}\n") + + +def better_error(tool_path: Path): + """ + Raise a better error when the subprocess return with a non 0 value. + :param tool_path: path of the used tools + :return: wrapper + """ + + def decorator(func): + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except subprocess.CalledProcessError as e: + raise WTError(tool_path, e.returncode) from e + + return wrapper + + return decorator diff --git a/source/wt/bmg.py b/source/wt/bmg.py new file mode 100644 index 0000000..e69de29 diff --git a/source/wt/img.py b/source/wt/img.py new file mode 100644 index 0000000..e69de29 diff --git a/source/wt/str.py b/source/wt/str.py new file mode 100644 index 0000000..e69de29 diff --git a/source/wt/szs.py b/source/wt/szs.py new file mode 100644 index 0000000..d432940 --- /dev/null +++ b/source/wt/szs.py @@ -0,0 +1,44 @@ +from source.wt import * + +tools_path = tools_szs_dir / "wszst.exe" + + +class SZSPath: + + def __init__(self, path: Path | str): + self.path: Path = path if isinstance(path, Path) else Path(path) + + @better_error(tools_path) + def _run(self, *args) -> bytes: + """ + Return a command with wszst and return the output + :param args: command arguments + :return: the output of the command + """ + return subprocess.run( + [tools_path, *args], + stdout=subprocess.PIPE, + check=True, + creationflags=subprocess.CREATE_NO_WINDOW + ).stdout + + def cat(self, subfile: str) -> bytes: + """ + Run the cat command (read a subfile) and return the output + :param subfile: subfile name + :return: the content of the subfile + """ + return self._run("cat", self.path / subfile) + + def extract(self, subfile: str, dest: Path | str) -> Path: + """ + Extract a subfile to a destination + :param subfile: subfile name + :param dest: destination path + :return: the extracted file path + """ + dest = dest if isinstance(dest, Path) else Path(dest) + with dest.open("wb") as file: + file.write(self.cat(subfile)) + + return dest