2017-09-16 18:52:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-08-31 11:27:57 +02:00
|
|
|
|
2017-09-16 18:52:16 +02:00
|
|
|
import argparse
|
2017-08-31 11:27:57 +02:00
|
|
|
import csv
|
2017-09-10 21:11:48 +02:00
|
|
|
import os
|
|
|
|
import xlrd
|
2020-04-18 12:32:40 +02:00
|
|
|
from pprint import pprint
|
2017-08-31 11:27:57 +02:00
|
|
|
|
2017-09-16 18:52:16 +02:00
|
|
|
def main(args):
|
|
|
|
translations = {}
|
2020-04-18 12:32:40 +02:00
|
|
|
basename, extension = os.path.splitext(args.inputfile)
|
|
|
|
extension = extension.lower()
|
2017-08-31 11:27:57 +02:00
|
|
|
|
2017-09-16 18:52:16 +02:00
|
|
|
if extension == '.csv':
|
2020-04-18 12:32:40 +02:00
|
|
|
# Read CSV file, assume the same structure as given by the export
|
2017-09-16 18:52:16 +02:00
|
|
|
with open(args.inputfile, 'r') as csvfile:
|
2020-04-18 12:32:40 +02:00
|
|
|
reader = csv.reader(csvfile)
|
2017-09-16 18:52:16 +02:00
|
|
|
for line in reader:
|
|
|
|
translations[line[1]] = line[2]
|
2020-04-16 23:08:06 +02:00
|
|
|
elif extension in ('.xls', '.xlsx'):
|
2020-04-18 12:32:40 +02:00
|
|
|
# Read the XLS(X) file, assume type of columns from their count
|
2017-09-16 18:52:16 +02:00
|
|
|
sheet = xlrd.open_workbook(args.inputfile).sheet_by_index(0)
|
2020-04-18 12:32:40 +02:00
|
|
|
source_col,target_col = (1,2) if sheet.row(0)[2] else (0,1)
|
2017-09-16 18:52:16 +02:00
|
|
|
for i in range(1, sheet.nrows):
|
|
|
|
row = sheet.row(i)
|
2020-04-18 12:32:40 +02:00
|
|
|
translations[row[source_col].value] = row[target_col].value
|
2017-09-16 18:52:16 +02:00
|
|
|
else:
|
|
|
|
print('Unknown input file extension')
|
|
|
|
return
|
2017-08-31 11:27:57 +02:00
|
|
|
|
2017-09-16 18:52:16 +02:00
|
|
|
with open('{}.py'.format(basename), 'w') as langfile:
|
2020-04-18 12:32:40 +02:00
|
|
|
# Write the translation file
|
2017-09-16 18:52:16 +02:00
|
|
|
print('# -*- coding: utf-8 -*-', file=langfile)
|
|
|
|
pprint(translations, langfile, 0, 8192)
|
2017-08-31 11:27:57 +02:00
|
|
|
|
2017-09-16 18:52:16 +02:00
|
|
|
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())
|