Rewrite Sahana translation converter to produce files without escape sequences

This commit is contained in:
Disassembler 2017-09-16 18:52:16 +02:00
parent 01154a7d24
commit 6c521d367f

View File

@ -1,32 +1,34 @@
#!/usr/bin/env python #!/usr/bin/env python3
from __future__ import print_function
from pprint import pprint from pprint import pprint
import argparse
import csv import csv
import os import os
import sys
import xlrd import xlrd
if len(sys.argv) == 1: def main(args):
print('Usage: {} input.(csv|xls)'.format(sys.argv[0])) translations = {}
sys.exit(1) basename, extension = os.path.splitext(args.inputfile.lower())
basename, extension = os.path.splitext(sys.argv[1].lower()) if extension == '.csv':
translations = {} with open(args.inputfile, 'r') as csvfile:
if extension == '.csv':
with open(sys.argv[1], 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='\'') reader = csv.reader(csvfile, delimiter=',', quotechar='\'')
for line in reader: for line in reader:
translations[line[1].encode('utf-8')] = line[2].encode('utf-8') translations[line[1]] = line[2]
elif extension == '.xls': elif extension == '.xls':
sheet = xlrd.open_workbook(sys.argv[1]).sheet_by_index(0) sheet = xlrd.open_workbook(args.inputfile).sheet_by_index(0)
for i in range(1, sheet.nrows): for i in range(1, sheet.nrows):
row = sheet.row(i) row = sheet.row(i)
translations[row[1].value.encode('utf-8')] = row[2].value.encode('utf-8') translations[row[1].value] = row[2].value
else: else:
print('Unknown input file extension') print('Unknown input file extension')
sys.exit(1) return
with open('{}.py'.format(basename), 'wb') as langfile: with open('{}.py'.format(basename), 'w') as langfile:
pprint(translations, langfile) print('# -*- coding: utf-8 -*-', file=langfile)
pprint(translations, langfile, 0, 8192)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Spotter Cluster Sahana Eden translation converter')
parser.add_argument('inputfile', help='CSV or XLS translation file.')
main(parser.parse_args())