mirror of
https://github.com/Faraphel/Atlas-Install.git
synced 2025-07-05 04:08:21 +02:00
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
This commit is contained in:
parent
448170dad9
commit
15d84f757c
7 changed files with 19 additions and 26 deletions
|
@ -17,7 +17,7 @@ class Patch:
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"<{self.__class__.__name__} {self.path}>"
|
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
|
Safe eval with a patch environment
|
||||||
:param multiple: should the expression be a multiple safe eval or a single safe eval
|
: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)(
|
return (multiple_safe_eval if multiple else safe_eval)(
|
||||||
template,
|
template,
|
||||||
extra_token_map={"mod_config": "mod_config"} | {key: key for key in env},
|
env={"mod_config": self.mod_config} | (env if env is not None else {}),
|
||||||
env={"mod_config": self.mod_config} | env,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def install(self, extracted_game: "ExtractedGame") -> Generator[dict, None, None]:
|
def install(self, extracted_game: "ExtractedGame") -> Generator[dict, None, None]:
|
||||||
|
|
|
@ -24,7 +24,7 @@ class PatchDirectory(PatchObject):
|
||||||
"""
|
"""
|
||||||
yield {"description": f"Patching {self}"}
|
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"]:
|
match self.configuration["mode"]:
|
||||||
# if the mode is copy, then simply patch the subfile into the game with the same path
|
# if the mode is copy, then simply patch the subfile into the game with the same path
|
||||||
|
|
|
@ -31,7 +31,7 @@ class PatchFile(PatchObject):
|
||||||
yield {"description": f"Patching {self}"}
|
yield {"description": f"Patching {self}"}
|
||||||
|
|
||||||
# check if the file should be patched considering the "if" configuration
|
# 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
|
# 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",
|
for szs_subpath in filter(lambda path: path.suffix == ".d",
|
||||||
|
|
|
@ -18,7 +18,7 @@ class FormatOriginalTrackLayer(AbstractLayer):
|
||||||
self.template = template
|
self.template = template
|
||||||
|
|
||||||
def patch_bmg(self, patch: "Patch", decoded_content: str) -> str:
|
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] = []
|
new_bmg_lines: list[str] = []
|
||||||
|
|
||||||
for line in originals_track.split("\n"):
|
for line in originals_track.split("\n"):
|
||||||
|
|
|
@ -11,7 +11,7 @@ class PatchLayer(AbstractLayer):
|
||||||
|
|
||||||
mode = "patch"
|
mode = "patch"
|
||||||
|
|
||||||
def __init__(self, patchs: dict[str, str | None]):
|
def __init__(self, patchs: list[str]):
|
||||||
self.patchs = patchs
|
self.patchs = patchs
|
||||||
|
|
||||||
def patch_bmg(self, patch: "Patch", decoded_content: str) -> str:
|
def patch_bmg(self, patch: "Patch", decoded_content: str) -> str:
|
||||||
|
|
|
@ -53,13 +53,14 @@ class Track:
|
||||||
:return: formatted representation of the track
|
:return: formatted representation of the track
|
||||||
"""
|
"""
|
||||||
|
|
||||||
extra_token_map = { # replace the suffix and the prefix by the corresponding values
|
return multiple_safe_eval(
|
||||||
"prefix": f'{self.get_prefix(mod_config, "")!r}',
|
template,
|
||||||
"suffix": f'{self.get_suffix(mod_config, "")!r}',
|
env={
|
||||||
"track": "track"
|
"track": self,
|
||||||
}
|
"prefix": self.get_prefix(mod_config, ""),
|
||||||
|
"suffix": self.get_suffix(mod_config, "")
|
||||||
return multiple_safe_eval(template, extra_token_map, {"track": self})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
def get_tag_template(self, templates: dict[str, str], default: any = None) -> any:
|
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):
|
for tag in filter(lambda tag: tag in templates, self.tags):
|
||||||
template: str = templates[tag]
|
template: str = templates[tag]
|
||||||
return multiple_safe_eval(
|
return multiple_safe_eval(template, env={"TAG": tag, "bmg_color_text": bmg_color_text})
|
||||||
template,
|
|
||||||
extra_token_map={"TAG": "TAG", "bmg_color_text": "bmg_color_text"},
|
|
||||||
env={"TAG": tag, "bmg_color_text": bmg_color_text}
|
|
||||||
)
|
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def get_prefix(self, mod_config: "ModConfig", default: any = None) -> any:
|
def get_prefix(self, mod_config: "ModConfig", default: any = None) -> any:
|
||||||
|
|
|
@ -51,17 +51,15 @@ class SafeFunction:
|
||||||
return attr
|
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
|
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 env: variables to use when using eval
|
||||||
:param template: template to evaluate
|
:param template: template to evaluate
|
||||||
"""
|
"""
|
||||||
if extra_token_map is None: extra_token_map = {}
|
|
||||||
if env is None: env = {}
|
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 = ""
|
final_token: str = ""
|
||||||
|
|
||||||
def matched(match: re.Match | str | None, value: str = None) -> bool:
|
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
|
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:
|
def format_part_template(match: re.Match) -> str:
|
||||||
"""
|
"""
|
||||||
when a token is found, replace it by the corresponding value
|
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
|
# get the token string without the brackets, then strip it. Also double antislash
|
||||||
part_template = match.group(1).strip().replace("\\", "\\\\")
|
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
|
# pass everything between TOKEN_START and TOKEN_END in the function
|
||||||
return re.sub(rf"{TOKEN_START}(.*?){TOKEN_END}", format_part_template, template)
|
return re.sub(rf"{TOKEN_START}(.*?){TOKEN_END}", format_part_template, template)
|
||||||
|
|
||||||
# TODO: see if extra_token_map can't be shortened
|
|
||||||
|
|
Loading…
Reference in a new issue