tw_update.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python3
  2. # This script replaces in place.
  3. import os
  4. from glob import glob
  5. import fileinput
  6. import re
  7. import sys
  8. import variables
  9. path = os.path.join("sugarcube","src")
  10. filesNames = [y for x in os.walk(path) for y in glob(os.path.join(x[0], '*.tw'))]
  11. #filesNames = ["sugarcube/src/sidebar/Sidebar.tw"]
  12. for filesName in filesNames:
  13. print(filesName)
  14. for line in fileinput.input(filesName, inplace=True, encoding="utf8"):
  15. output = line
  16. for variable_replacement in variables.variable_replacements:
  17. if len(variable_replacement) > 2:
  18. if variable_replacement[2] == 1:
  19. output = re.sub(variable_replacement[0],variable_replacement[1],output)
  20. else:
  21. output = output.replace(variable_replacement[0],variable_replacement[1])
  22. else:
  23. output = output.replace(variable_replacement[0],variable_replacement[1])
  24. for quest_variable in variables.quest_variables:
  25. output = re.sub(r"<<set(?:init)?\s+"+quest_variable[0]+"\s*=\s*(?P<value>.*?)\s*>>","<<run $q."+quest_variable[1]+".set('"+quest_variable[2]+r"',\g<value>)>>",output)
  26. output = re.sub(r"<<set(?:init)?\s+"+quest_variable[0]+"\s*\+=\s*(?P<value>.*?)\s*>>","<<run $q."+quest_variable[1]+".inc('"+quest_variable[2]+r"',\g<value>)>>",output)
  27. output = re.sub(r"<<set(?:init)?\s+"+quest_variable[0]+"\s*-=\s*(?P<value>.*?)\s*>>","<<run $q."+quest_variable[1]+".dec('"+quest_variable[2]+r"',\g<value>)>>",output)
  28. output = re.sub(r"(?:getvar\(\")?"+quest_variable[0]+r"(?:\"\))?","$q."+quest_variable[1]+"?.get('"+quest_variable[2]+"')",output)
  29. for inventory_variable in variables.inventory_variables:
  30. output = re.sub(rf"<<set(?:init)?\s+\${inventory_variable[0]}\s*=\s*(.*?)\s*>>",rf"<<run $inventory.set('{inventory_variable[1]}',\1)>>",output)
  31. output = re.sub(rf"<<set(?:init)?\s+\${inventory_variable[0]}\s*-=\s*(.*?)\s*>>",rf"<<run $inventory.dec('{inventory_variable[1]}',\1)>>",output)
  32. output = re.sub(rf"<<set(?:init)?\s+\${inventory_variable[0]}\s*\+=\s*(.*?)\s*>>",rf"<<run $inventory.inc('{inventory_variable[1]}',\1)>>",output)
  33. output = re.sub(rf"<<{inventory_variable[0]}>>",rf"""$inventory.get("{inventory_variable[1]}")""",output)
  34. output = re.sub(rf"""getvar\("\${inventory_variable[0]}"\)""",rf"""$inventory.get("{inventory_variable[1]}")""",output)
  35. output = re.sub(rf"""\${inventory_variable[0]}([^\w])""",rf"""$inventory.get("{inventory_variable[1]}")\1""",output)
  36. for get_set_variable in variables.get_set_variables:
  37. output = re.sub(rf"<<set(?:init)?\s+{get_set_variable[0]}\s*=\s*(.*?)\s*>>",rf"<<run {get_set_variable[2]}>>",output)
  38. if(len(get_set_variable) == 5):
  39. output = re.sub(rf"<<set(?:init)?\s+{get_set_variable[0]}\s*\+=\s*(.*?)\s*>>",rf"<<run {get_set_variable[3]}>>",output)
  40. output = re.sub(rf"<<set(?:init)?\s+{get_set_variable[0]}\s*\-=\s*(.*?)\s*>>",rf"<<run {get_set_variable[4]}>>",output)
  41. #output = re.sub(rf"<<set(?:init)?\s+\${get_set_variable[0]}\s*-=\s*(.*?)\s*>>",rf"<<run $inventory.dec('{inventory_variable[1]}',\1)>>",output)
  42. #output = re.sub(rf"<<set(?:init)?\s+\${get_set_variable[0]}\s*\+=\s*(.*?)\s*>>",rf"<<run $inventory.inc('{inventory_variable[1]}',\1)>>",output)
  43. output = re.sub(rf"<<{get_set_variable[0]}>>",get_set_variable[1],output)
  44. output = re.sub(rf"""getvar\("{get_set_variable[0]}"\)""",get_set_variable[1],output)
  45. output = re.sub(rf"""{get_set_variable[0]}(?P<stuffToTheRight>[^\w])""",rf"""{get_set_variable[1]}\g<stuffToTheRight>""",output)
  46. for pgsvr in variables.post_get_set_variables_replacements:
  47. output = output.replace(pgsvr[0],pgsvr[1])
  48. print(output, end='')
  49. #print(result)