txtsplit.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python
  2. # usage: txtsplit.py <input_file_name> <output_dir>
  3. # splits a txt2gam file into individual location files
  4. # encoded in utf-8, for git to better handle
  5. import os
  6. import sys
  7. import re
  8. import io
  9. import xml.etree.ElementTree as ET
  10. assert len(sys.argv) == 3, "usage:\ntxtsplit.py <input_file_name> <output_dir>"
  11. iname = str(sys.argv[1])
  12. odir = str(sys.argv[2])
  13. # read the project xml file first
  14. # let's do this later in order to implement directory structure
  15. """
  16. tree = ET.parse(iname)
  17. root = tree.getroot()
  18. """
  19. ifile = io.open(iname, 'rt', encoding='utf-16')
  20. counter = 1
  21. oname = None
  22. firstline = ifile.readline().replace(u'\ufeff','')
  23. match = re.search(ur'^#\s(\$?[_.\w]+)$', firstline)
  24. if match:
  25. oname = os.path.join(odir, match.group(1).replace("$","_") + '.qsrc' )
  26. counter += 1
  27. assert oname, "file is in the wrong format, must start with a location name"
  28. ofile = io.open(oname, 'w', encoding='utf-8')
  29. ofile.write(firstline)
  30. for line in ifile:
  31. match = re.search(ur'^#\s(\$?[_.\w]+)$', line)
  32. if match:
  33. ofile.close()
  34. oname = os.path.join(odir, match.group(1).replace("$","_") + '.qsrc' )
  35. counter += 1
  36. ofile = io.open(oname, 'w', encoding='utf-8')
  37. ofile.write(line)