Spotter-VM/extra/helpers/sahana-lang-convert.py

40 lines
1.4 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
import argparse
import csv
import os
import xlrd
from pprint import pprint
def main(args):
translations = {}
basename, extension = os.path.splitext(args.inputfile)
extension = extension.lower()
if extension == '.csv':
# Read CSV file, assume the same structure as given by the export
with open(args.inputfile, 'r') as csvfile:
reader = csv.reader(csvfile)
for line in reader:
translations[line[1]] = line[2]
elif extension in ('.xls', '.xlsx'):
# Read the XLS(X) file, assume type of columns from their count
sheet = xlrd.open_workbook(args.inputfile).sheet_by_index(0)
source_col,target_col = (1,2) if sheet.row(0)[2] else (0,1)
for i in range(1, sheet.nrows):
row = sheet.row(i)
translations[row[source_col].value] = row[target_col].value
else:
print('Unknown input file extension')
return
with open('{}.py'.format(basename), 'w') as langfile:
# Write the translation file
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())