Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- colors_dict = {
- "fred" : "RED_FONT",
- "fgreen" : "GREEN_FONT",
- "bwhite" : "BACK_WHITE",
- "endc" : "END_COLOR",
- }
- variables_dict = {
- "hero_name": "'Dude'",
- "hero_hp" : "100",
- }
- strings = [
- "${fred:Hero name is} : %{local:hero_name}\n",
- "${fred:Hero hp is} : ${fgreen:%{local:hero_hp}}"
- ]
- collect = []
- patterns = [r"\$\{\s*(.*?)\s*\:\s*(.*?)\}", r"\%\{\s*(.*?)\s*\:\s*(.*?)\}"]
- for i in strings:
- match_color = re.search(patterns[0], i)
- match_variable = re.search(patterns[1], i)
- if match_color:
- colors = match_color.group(1)
- string = match_color.group(2)
- colors = "".join([colors_dict[i] for i in re.split(r"\s", colors)])
- string = colors + string + colors_dict["endc"]
- collect.append(string)
- if match_variable:
- var_scope = match_variable.group(1)
- var_name = match_variable.group(2)
- if var_scope == "local":
- collect.append(variables_dict[var_name])
- print(" ".join(collect))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement