Advertisement
Guest User

broken parser

a guest
Sep 3rd, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. import re
  2.  
  3. colors_dict = {
  4.     "fred"   : "RED_FONT",
  5.     "fgreen" : "GREEN_FONT",
  6.     "bwhite" : "BACK_WHITE",
  7.     "endc"   : "END_COLOR",
  8. }
  9.  
  10.  
  11. variables_dict = {
  12.     "hero_name": "'Dude'",
  13.     "hero_hp"  : "100",
  14. }
  15.  
  16. strings = [
  17.     "${fred:Hero name is} : %{local:hero_name}\n",
  18.     "${fred:Hero hp is} : ${fgreen:%{local:hero_hp}}"
  19. ]
  20.  
  21. collect = []
  22. patterns = [r"\$\{\s*(.*?)\s*\:\s*(.*?)\}", r"\%\{\s*(.*?)\s*\:\s*(.*?)\}"]
  23.  
  24. for i in strings:
  25.     match_color = re.search(patterns[0], i)
  26.     match_variable = re.search(patterns[1], i)
  27.  
  28.     if match_color:
  29.         colors = match_color.group(1)
  30.         string = match_color.group(2)
  31.         colors = "".join([colors_dict[i] for i in re.split(r"\s", colors)])
  32.         string = colors + string + colors_dict["endc"]
  33.         collect.append(string)
  34.  
  35.     if match_variable:
  36.         var_scope = match_variable.group(1)
  37.         var_name = match_variable.group(2)
  38.  
  39.         if var_scope == "local":
  40.             collect.append(variables_dict[var_name])
  41.  
  42. print(" ".join(collect))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement