started working on the szs and wit wrapper

This commit is contained in:
Faraphel 2022-06-08 16:24:02 +02:00
parent 8d6141703b
commit a04f7286b6
12 changed files with 83 additions and 0 deletions

0
source/mkw/Arena.py Normal file
View file

0
source/mkw/Cup.py Normal file
View file

0
source/mkw/Game.py Normal file
View file

0
source/mkw/ModConfig.py Normal file
View file

0
source/mkw/Track.py Normal file
View file

0
source/mkw/TrackGroup.py Normal file
View file

0
source/mkw/__init__.py Normal file
View file

39
source/wt/__init__.py Normal file
View file

@ -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

0
source/wt/bmg.py Normal file
View file

0
source/wt/img.py Normal file
View file

0
source/wt/str.py Normal file
View file

44
source/wt/szs.py Normal file
View file

@ -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