From 6c521d367fc911b88b696c6a152c0d9492d467e1 Mon Sep 17 00:00:00 2001 From: Disassembler Date: Sat, 16 Sep 2017 18:52:16 +0200 Subject: [PATCH] Rewrite Sahana translation converter to produce files without escape sequences --- sahana/sahana-lang-csv.py | 48 ++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/sahana/sahana-lang-csv.py b/sahana/sahana-lang-csv.py index 70dd096..c247a2c 100755 --- a/sahana/sahana-lang-csv.py +++ b/sahana/sahana-lang-csv.py @@ -1,32 +1,34 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 -from __future__ import print_function from pprint import pprint +import argparse import csv import os -import sys import xlrd -if len(sys.argv) == 1: - print('Usage: {} input.(csv|xls)'.format(sys.argv[0])) - sys.exit(1) +def main(args): + translations = {} + basename, extension = os.path.splitext(args.inputfile.lower()) -basename, extension = os.path.splitext(sys.argv[1].lower()) -translations = {} + if extension == '.csv': + with open(args.inputfile, 'r') as csvfile: + reader = csv.reader(csvfile, delimiter=',', quotechar='\'') + for line in reader: + translations[line[1]] = line[2] + elif extension == '.xls': + sheet = xlrd.open_workbook(args.inputfile).sheet_by_index(0) + for i in range(1, sheet.nrows): + row = sheet.row(i) + translations[row[1].value] = row[2].value + else: + print('Unknown input file extension') + return -if extension == '.csv': - with open(sys.argv[1], 'rb') as csvfile: - reader = csv.reader(csvfile, delimiter=',', quotechar='\'') - for line in reader: - translations[line[1].encode('utf-8')] = line[2].encode('utf-8') -elif extension == '.xls': - sheet = xlrd.open_workbook(sys.argv[1]).sheet_by_index(0) - for i in range(1, sheet.nrows): - row = sheet.row(i) - translations[row[1].value.encode('utf-8')] = row[2].value.encode('utf-8') -else: - print('Unknown input file extension') - sys.exit(1) + with open('{}.py'.format(basename), 'w') as langfile: + print('# -*- coding: utf-8 -*-', file=langfile) + pprint(translations, langfile, 0, 8192) -with open('{}.py'.format(basename), 'wb') as langfile: - pprint(translations, langfile) +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())