moved _run, _run_popen and _run_dict out of the class to make _tools_run, _tools_run_popen and _tools_run_dict for the whole module

This commit is contained in:
Faraphel 2022-07-10 23:32:58 +02:00
parent b690b58b60
commit 803e9603e7
3 changed files with 78 additions and 64 deletions

View file

@ -1,10 +1,28 @@
from source.wt import *
from source.wt import _run
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) -> 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)
class BMGPath:
"""
Represent a path to a bmg file (game file containing text data)
@ -14,18 +32,10 @@ class BMGPath:
def __init__(self, path: Path | str):
self.path: Path = Path(path)
@better_wt_error(tools_path)
def _run(self, *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 get_decoded_data(self):
"""
Return the decoded content of the bmg file
:return:
"""
return self._run("DECODE", self.path, "--DEST", "-")
return _tools_run("DECODE", self.path, "--DEST", "-")

View file

@ -4,6 +4,26 @@ 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)
@better_wt_error(tools_path)
def autoadd(course_directory: Path | str, destination_path: Path | str) -> Path:
"""
@ -30,31 +50,13 @@ class SZSPath:
def __eq__(self, other: "SZSPath") -> bool:
return self.path == other.path
@better_wt_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 _run(tools_path, *args)
@better_wt_error(tools_path)
def _run_dict(self, *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)
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)
return _tools_run("cat", self.path / subfile)
def extract(self, subfile: str, dest: Path | str) -> Path:
"""
@ -74,7 +76,7 @@ class SZSPath:
dest: Path = Path(dest)
if dest.is_dir(): dest /= self.path.name
self._run("EXTRACT", self.path, "-D", dest)
_tools_run("EXTRACT", self.path, "-D", dest)
return dest
def analyze(self) -> dict:
@ -82,7 +84,7 @@ class SZSPath:
Return the analyze of the file
:return: dictionnary of key and value of the analyze
"""
if self._analyze is None: self._analyze = self._run_dict("ANALYZE", self.path)
if self._analyze is None: self._analyze = _tools_run_dict("ANALYZE", self.path)
return self._analyze
def list_raw(self) -> list[str]:
@ -95,7 +97,7 @@ class SZSPath:
# add it to the list. Finally, remove the first line because this is a description of the command
return [
subfile.strip()
for subfile in self._run("list", self.path).decode().splitlines()
for subfile in _tools_run("list", self.path).decode().splitlines()
if subfile.startswith("./")
]

View file

@ -9,6 +9,35 @@ 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) -> 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)
@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):
"""
Enum for game extension
@ -52,33 +81,6 @@ class WITPath:
def __eq__(self, other: "WITPath") -> bool:
return self.path == other.path
@better_wt_error(tools_path)
def _run(self, *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)
@classmethod
def _run_popen(cls, *args) -> 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)
@better_wt_error(tools_path)
def _run_dict(self, *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)
def _get_fst_root(self) -> Path:
"""
If the game is a FST, return the root of the FST
@ -92,7 +94,7 @@ class WITPath:
Return the analyze of the file
:return: dictionnary of key and value of the analyze
"""
if self._analyze is None: self._analyze = self._run_dict("ANALYZE", self.path)
if self._analyze is None: self._analyze = _tools_run_dict("ANALYZE", self.path)
return self._analyze
def list_raw(self) -> list[str]:
@ -108,7 +110,7 @@ class WITPath:
return [
subfile.strip() for subfile
in self._run("files", self.path).decode().splitlines()
in _tools_run("files", self.path).decode().splitlines()
if subfile.startswith("./")
]
@ -153,7 +155,7 @@ class WITPath:
shutil.copytree(self._get_fst_root(), dest)
else:
process = self._run_popen("EXTRACT", self.path, "-d", dest, "--progress")
process = _tools_run_popen("EXTRACT", self.path, "-d", dest, "--progress")
while process.poll() is None:
m = re.match(r'\s*(?P<percentage>\d*)%(?:.*?ETA (?P<estimation>\d*:\d*))?\s*', process.stdout.readline())
@ -244,7 +246,7 @@ class WITSubPath:
else:
args = []
if flat: args.append("--flat")
self.wit_path._run("EXTRACT", self.wit_path.path, f"--files=+{self.subfile}", "-d", dest, *args)
_tools_run("EXTRACT", self.wit_path.path, f"--files=+{self.subfile}", "-d", dest, *args)
return dest / self.basename()
def is_dir(self) -> bool: