4
1

tw_update.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. output = output.replace('getvar("'+variable_replacement[1]+'")',variable_replacement[1])
  25. for quest_variable in variables.quest_variables:
  26. 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)
  27. 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)
  28. 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)
  29. output = re.sub(r"(?:getvar\(\")?"+quest_variable[0]+r"(?:\"\))?","$q."+quest_variable[1]+"?.get('"+quest_variable[2]+"')",output)
  30. for inventory_variable in variables.inventory_variables:
  31. output = re.sub(rf"<<set(?:init)?\s+\${inventory_variable[0]}\s*=\s*(.*?)\s*>>",rf"<<run $inventory.set('{inventory_variable[1]}',\1)>>",output)
  32. output = re.sub(rf"<<set(?:init)?\s+\${inventory_variable[0]}\s*-=\s*(.*?)\s*>>",rf"<<run $inventory.dec('{inventory_variable[1]}',\1)>>",output)
  33. output = re.sub(rf"<<set(?:init)?\s+\${inventory_variable[0]}\s*\+=\s*(.*?)\s*>>",rf"<<run $inventory.inc('{inventory_variable[1]}',\1)>>",output)
  34. output = re.sub(rf"<<{inventory_variable[0]}>>",rf"""$inventory.get("{inventory_variable[1]}")""",output)
  35. output = re.sub(rf"""getvar\("\${inventory_variable[0]}"\)""",rf"""$inventory.get("{inventory_variable[1]}")""",output)
  36. output = re.sub(rf"""\${inventory_variable[0]}([^\w])""",rf"""$inventory.get("{inventory_variable[1]}")\1""",output)
  37. for get_set_variable in variables.get_set_variables:
  38. output = re.sub(rf"<<set(?:init)?\s+{get_set_variable[0]}\s*=\s*(.*?)\s*>>",rf"<<run {get_set_variable[2]}>>",output)
  39. if(len(get_set_variable) == 5):
  40. output = re.sub(rf"<<set(?:init)?\s+{get_set_variable[0]}\s*\+=\s*(.*?)\s*>>",rf"<<run {get_set_variable[3]}>>",output)
  41. output = re.sub(rf"<<set(?:init)?\s+{get_set_variable[0]}\s*\-=\s*(.*?)\s*>>",rf"<<run {get_set_variable[4]}>>",output)
  42. #output = re.sub(rf"<<set(?:init)?\s+\${get_set_variable[0]}\s*-=\s*(.*?)\s*>>",rf"<<run $inventory.dec('{inventory_variable[1]}',\1)>>",output)
  43. #output = re.sub(rf"<<set(?:init)?\s+\${get_set_variable[0]}\s*\+=\s*(.*?)\s*>>",rf"<<run $inventory.inc('{inventory_variable[1]}',\1)>>",output)
  44. output = re.sub(rf"<<{get_set_variable[0]}>>",get_set_variable[1],output)
  45. output = re.sub(rf"""getvar\("{get_set_variable[0]}"\)""",get_set_variable[1],output)
  46. output = re.sub(rf"""{get_set_variable[0]}(?P<stuffToTheRight>[^\w])""",rf"""{get_set_variable[1]}\g<stuffToTheRight>""",output)
  47. for pgsvr in variables.post_get_set_variables_replacements:
  48. output = output.replace(pgsvr[0],pgsvr[1])
  49. print(output, end='')
  50. #print(result)