From 15d84f757c8ab195f1c9055a9864e6c0a581640f Mon Sep 17 00:00:00 2001 From: Faraphel Date: Thu, 21 Jul 2022 01:24:24 +0200 Subject: [PATCH] simplified safe_eval by removing extra_token_map because env do the same thing but better and by making patch.safe_eval more similar to the normal safe_eval --- source/mkw/Patch/Patch.py | 5 ++--- source/mkw/Patch/PatchDirectory.py | 2 +- source/mkw/Patch/PatchFile.py | 2 +- .../Layer/FormatOriginalTrackLayer.py | 2 +- .../BmgTxtEditor/Layer/PatchLayer.py | 2 +- source/mkw/Track.py | 21 ++++++++----------- source/safe_eval.py | 11 ++++------ 7 files changed, 19 insertions(+), 26 deletions(-) diff --git a/source/mkw/Patch/Patch.py b/source/mkw/Patch/Patch.py index 4de624b..6117b99 100644 --- a/source/mkw/Patch/Patch.py +++ b/source/mkw/Patch/Patch.py @@ -17,7 +17,7 @@ class Patch: def __repr__(self) -> str: return f"<{self.__class__.__name__} {self.path}>" - def safe_eval(self, template: str, multiple: bool = False, **env) -> str: + def safe_eval(self, template: str, multiple: bool = False, env: dict[str, any] = None) -> str: """ Safe eval with a patch environment :param multiple: should the expression be a multiple safe eval or a single safe eval @@ -27,8 +27,7 @@ class Patch: """ return (multiple_safe_eval if multiple else safe_eval)( template, - extra_token_map={"mod_config": "mod_config"} | {key: key for key in env}, - env={"mod_config": self.mod_config} | env, + env={"mod_config": self.mod_config} | (env if env is not None else {}), ) def install(self, extracted_game: "ExtractedGame") -> Generator[dict, None, None]: diff --git a/source/mkw/Patch/PatchDirectory.py b/source/mkw/Patch/PatchDirectory.py index 62203da..1ce1bba 100644 --- a/source/mkw/Patch/PatchDirectory.py +++ b/source/mkw/Patch/PatchDirectory.py @@ -24,7 +24,7 @@ class PatchDirectory(PatchObject): """ yield {"description": f"Patching {self}"} - if self.patch.safe_eval(self.configuration["if"], extracted_game=extracted_game) == "False": return + if self.patch.safe_eval(self.configuration["if"], {"extracted_game": extracted_game}) == "False": return match self.configuration["mode"]: # if the mode is copy, then simply patch the subfile into the game with the same path diff --git a/source/mkw/Patch/PatchFile.py b/source/mkw/Patch/PatchFile.py index ea602ac..da85130 100644 --- a/source/mkw/Patch/PatchFile.py +++ b/source/mkw/Patch/PatchFile.py @@ -31,7 +31,7 @@ class PatchFile(PatchObject): yield {"description": f"Patching {self}"} # check if the file should be patched considering the "if" configuration - if self.patch.safe_eval(self.configuration["if"], extracted_game=extracted_game) == "False": return + if self.patch.safe_eval(self.configuration["if"], {"extracted_game": extracted_game}) == "False": return # check if the path to the game_subpath is inside a szs, and if yes extract it for szs_subpath in filter(lambda path: path.suffix == ".d", diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py index e1e9123..e76f18a 100644 --- a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py +++ b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py @@ -18,7 +18,7 @@ class FormatOriginalTrackLayer(AbstractLayer): self.template = template def patch_bmg(self, patch: "Patch", decoded_content: str) -> str: - originals_track = bmg.cat_data(decoded_content, filters={"TRACKS+ARENAS": None}) + originals_track = bmg.cat_data(decoded_content, filters=["TRACKS+ARENAS"]) new_bmg_lines: list[str] = [] for line in originals_track.split("\n"): diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/PatchLayer.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/PatchLayer.py index 2601f03..291a101 100644 --- a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/PatchLayer.py +++ b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/PatchLayer.py @@ -11,7 +11,7 @@ class PatchLayer(AbstractLayer): mode = "patch" - def __init__(self, patchs: dict[str, str | None]): + def __init__(self, patchs: list[str]): self.patchs = patchs def patch_bmg(self, patch: "Patch", decoded_content: str) -> str: diff --git a/source/mkw/Track.py b/source/mkw/Track.py index 7609621..e068e3f 100644 --- a/source/mkw/Track.py +++ b/source/mkw/Track.py @@ -53,13 +53,14 @@ class Track: :return: formatted representation of the track """ - extra_token_map = { # replace the suffix and the prefix by the corresponding values - "prefix": f'{self.get_prefix(mod_config, "")!r}', - "suffix": f'{self.get_suffix(mod_config, "")!r}', - "track": "track" - } - - return multiple_safe_eval(template, extra_token_map, {"track": self}) + return multiple_safe_eval( + template, + env={ + "track": self, + "prefix": self.get_prefix(mod_config, ""), + "suffix": self.get_suffix(mod_config, "") + } + ) def get_tag_template(self, templates: dict[str, str], default: any = None) -> any: """ @@ -70,11 +71,7 @@ class Track: """ for tag in filter(lambda tag: tag in templates, self.tags): template: str = templates[tag] - return multiple_safe_eval( - template, - extra_token_map={"TAG": "TAG", "bmg_color_text": "bmg_color_text"}, - env={"TAG": tag, "bmg_color_text": bmg_color_text} - ) + return multiple_safe_eval(template, env={"TAG": tag, "bmg_color_text": bmg_color_text}) return default def get_prefix(self, mod_config: "ModConfig", default: any = None) -> any: diff --git a/source/safe_eval.py b/source/safe_eval.py index 9329f5f..cd34b3d 100644 --- a/source/safe_eval.py +++ b/source/safe_eval.py @@ -51,17 +51,15 @@ class SafeFunction: return attr -def safe_eval(template: str, extra_token_map: dict[str, str] = None, env: dict[str, any] = None) -> str: +def safe_eval(template: str, env: dict[str, any] = None) -> str: """ Evaluate the template and return the result in a safe way - :param extra_token_map: additionnal tokens to use in the template :param env: variables to use when using eval :param template: template to evaluate """ - if extra_token_map is None: extra_token_map = {} if env is None: env = {} - token_map: dict[str, str] = common_token_map | extra_token_map + token_map: dict[str, str] = common_token_map | {var: var for var in env} final_token: str = "" def matched(match: re.Match | str | None, value: str = None) -> bool: @@ -113,7 +111,7 @@ def safe_eval(template: str, extra_token_map: dict[str, str] = None, env: dict[s else: return final_token -def multiple_safe_eval(template: str, extra_token_map: dict[str, str] = None, env: dict[str, any] = None) -> str: +def multiple_safe_eval(template: str, env: dict[str, any] = None) -> str: def format_part_template(match: re.Match) -> str: """ when a token is found, replace it by the corresponding value @@ -122,9 +120,8 @@ def multiple_safe_eval(template: str, extra_token_map: dict[str, str] = None, en """ # get the token string without the brackets, then strip it. Also double antislash part_template = match.group(1).strip().replace("\\", "\\\\") - return safe_eval(part_template, extra_token_map, env) + return safe_eval(part_template, env) # pass everything between TOKEN_START and TOKEN_END in the function return re.sub(rf"{TOKEN_START}(.*?){TOKEN_END}", format_part_template, template) -# TODO: see if extra_token_map can't be shortened