mirror of
https://github.com/Faraphel/Atlas-Install.git
synced 2025-07-06 04:38:29 +02:00
moved multiple safe eval from Track.py to safe_eval.py
This commit is contained in:
parent
cb38bf3ae5
commit
4c1c8833b1
2 changed files with 23 additions and 19 deletions
|
@ -2,10 +2,7 @@ from typing import Generator
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from source.mkw import Tag, Slot
|
from source.mkw import Tag, Slot
|
||||||
from source.safe_eval import safe_eval
|
from source.safe_eval import safe_eval, multiple_safe_eval
|
||||||
|
|
||||||
TOKEN_START = "{{"
|
|
||||||
TOKEN_END = "}}"
|
|
||||||
|
|
||||||
|
|
||||||
# representation of a custom track
|
# representation of a custom track
|
||||||
|
@ -45,9 +42,10 @@ class Track:
|
||||||
for _ in range(self.weight):
|
for _ in range(self.weight):
|
||||||
yield self
|
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
|
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
|
:param mod_config: configuration of the mod
|
||||||
:return: formatted representation of the track
|
:return: formatted representation of the track
|
||||||
"""
|
"""
|
||||||
|
@ -58,18 +56,7 @@ class Track:
|
||||||
"track": "track"
|
"track": "track"
|
||||||
}
|
}
|
||||||
|
|
||||||
def format_template(match: re.Match) -> str:
|
return multiple_safe_eval(template, extra_token_map, {"track": self})
|
||||||
"""
|
|
||||||
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)
|
|
||||||
|
|
||||||
def get_prefix(self, mod_config: "ModConfig", default: any = None) -> any:
|
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
|
:hidden: if the track is in a group
|
||||||
:return: ctfile
|
:return: ctfile
|
||||||
"""
|
"""
|
||||||
menu_name = f'{self.repr_format(mod_config=mod_config, format=mod_config.track_formatting["menu_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, format=mod_config.track_formatting["file_name"])!r}'
|
file_name = f'{self.repr_format(mod_config=mod_config, template=mod_config.track_formatting["file_name"])!r}'
|
||||||
|
|
||||||
return (
|
return (
|
||||||
f'{"H" if hidden else "T"} {self.music}; ' # track type
|
f'{"H" if hidden else "T"} {self.music}; ' # track type
|
||||||
|
|
|
@ -15,6 +15,8 @@ common_token_map = { # these operators and function are considered safe to use
|
||||||
if not method.startswith("__")
|
if not method.startswith("__")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TOKEN_START, TOKEN_END = "{{", "}}"
|
||||||
|
|
||||||
|
|
||||||
class TemplateParsingError(Exception):
|
class TemplateParsingError(Exception):
|
||||||
def __init__(self, token: str):
|
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 is set, eval final_token and return the result
|
||||||
if final_token: return str(eval(final_token, SafeFunction.get_all_safe_methods(), env))
|
if final_token: return str(eval(final_token, SafeFunction.get_all_safe_methods(), env))
|
||||||
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 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)
|
||||||
|
|
Loading…
Reference in a new issue