From 4c1c8833b16cd98bb0bf6c3a66362851bc151d87 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Tue, 12 Jul 2022 20:56:20 +0200 Subject: [PATCH] moved multiple safe eval from Track.py to safe_eval.py --- source/mkw/Track.py | 25 ++++++------------------- source/safe_eval.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/source/mkw/Track.py b/source/mkw/Track.py index c2554b7..d78f396 100644 --- a/source/mkw/Track.py +++ b/source/mkw/Track.py @@ -2,10 +2,7 @@ from typing import Generator import re from source.mkw import Tag, Slot -from source.safe_eval import safe_eval - -TOKEN_START = "{{" -TOKEN_END = "}}" +from source.safe_eval import safe_eval, multiple_safe_eval # representation of a custom track @@ -45,9 +42,10 @@ class Track: for _ in range(self.weight): yield self - def repr_format(self, mod_config: "ModConfig", format: str) -> str: + def repr_format(self, mod_config: "ModConfig", template: str) -> str: """ return the representation of the track from the format + :param template: template for the way the text will be represented :param mod_config: configuration of the mod :return: formatted representation of the track """ @@ -58,18 +56,7 @@ class Track: "track": "track" } - def format_template(match: re.Match) -> str: - """ - when a token is found, replace it by the corresponding value - :param match: match in the format - :return: corresponding value - """ - # get the token string without the brackets, then strip it. Also double antislash - template = match.group(1).strip().replace("\\", "\\\\") - return safe_eval(template, extra_token_map, {"track": self}) - - # pass everything between TOKEN_START and TOKEN_END in the function - return re.sub(rf"{TOKEN_START}(.*?){TOKEN_END}", format_template, format) + return multiple_safe_eval(template, extra_token_map, {"track": self}) def get_prefix(self, mod_config: "ModConfig", default: any = None) -> any: """ @@ -103,8 +90,8 @@ class Track: :hidden: if the track is in a group :return: ctfile """ - menu_name = f'{self.repr_format(mod_config=mod_config, format=mod_config.track_formatting["menu_name"])!r}' - file_name = f'{self.repr_format(mod_config=mod_config, format=mod_config.track_formatting["file_name"])!r}' + menu_name = f'{self.repr_format(mod_config=mod_config, template=mod_config.track_formatting["menu_name"])!r}' + file_name = f'{self.repr_format(mod_config=mod_config, template=mod_config.track_formatting["file_name"])!r}' return ( f'{"H" if hidden else "T"} {self.music}; ' # track type diff --git a/source/safe_eval.py b/source/safe_eval.py index 5e81c69..3c9b0b1 100644 --- a/source/safe_eval.py +++ b/source/safe_eval.py @@ -15,6 +15,8 @@ common_token_map = { # these operators and function are considered safe to use if not method.startswith("__") } +TOKEN_START, TOKEN_END = "{{", "}}" + class TemplateParsingError(Exception): def __init__(self, token: str): @@ -108,3 +110,18 @@ def safe_eval(template: str, extra_token_map: dict[str, str] = None, env: dict[s # if final_token is set, eval final_token and return the result if final_token: return str(eval(final_token, SafeFunction.get_all_safe_methods(), env)) else: return final_token + + +def multiple_safe_eval(template: str, extra_token_map: dict[str, str] = None, 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 + :param match: match in the format + :return: corresponding value + """ + # 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) + + # pass everything between TOKEN_START and TOKEN_END in the function + return re.sub(rf"{TOKEN_START}(.*?){TOKEN_END}", format_part_template, template)