txtmerge.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. # usage: txtmerge.py <input_dir> <output_file_name>
  3. # does the exact opposite of txtsplit.py
  4. import os
  5. import sys
  6. import re
  7. import io
  8. import xml.etree.ElementTree as ET
  9. assert len(sys.argv) == 4, "usage:\ntxtmerge.py <input_file_name> <input_dir> <output_file_name>"
  10. iname = str(sys.argv[1])
  11. idir = str(sys.argv[2])
  12. oname = str(sys.argv[3])
  13. # read the project xml file first
  14. # let's do this later in order to implement directory structure
  15. tree = ET.parse(iname)
  16. root = tree.getroot()
  17. ofile = io.open(oname, 'w', encoding='utf-16', newline='\r\n')
  18. for location in root.iter('Location'):
  19. iname = location.attrib['name']
  20. iname = iname.replace("$","_")
  21. try:
  22. ifile = io.open(os.path.join(idir,iname + '.qsrc'), 'rt', encoding='utf-8')
  23. text = ifile.read()
  24. # make sure there's a line at the end of file
  25. # (why wouldn't there be one? WINDOWS!
  26. if text[-1] != u'\n':
  27. text += u'\n\n'
  28. ofile.write(text)
  29. ifile.close()
  30. except IOError:
  31. print("WARNING: missing location %s" % iname)
  32. pass
  33. ofile.close()