From a93e6cf3735ca66f4b62065fdbca6d4f2f57aa90 Mon Sep 17 00:00:00 2001 From: Randall Wert Date: Tue, 8 Jun 2021 13:12:15 -0400 Subject: [PATCH 1/3] Add Google sheet capability --- .../csv_or_sheet_to_vanilla_i18.py | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 csv_to_vanilla-i18n/csv_or_sheet_to_vanilla_i18.py diff --git a/csv_to_vanilla-i18n/csv_or_sheet_to_vanilla_i18.py b/csv_to_vanilla-i18n/csv_or_sheet_to_vanilla_i18.py new file mode 100755 index 0000000..bce6ed5 --- /dev/null +++ b/csv_to_vanilla-i18n/csv_or_sheet_to_vanilla_i18.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Tue Jun 8 11:04:16 2021 + +@author: randallwert +""" + +import csv +import io +import json +import os +import requests +import sys + +OUTPUT_DIR = 'vanilla-i18n' + + +def add_to_dict(root, key, value): + keys = key.split('.') + cur_key = keys[0] + if cur_key in root.keys(): + if len(keys) == 1: + print( + "ERROR: duplicate key '{}' - keys with nesting cannot have non-nested elements!" + .format(cur_key)) + exit(1) + else: + if not isinstance(root[cur_key], dict): + print( + "ERROR: duplicate key '{}' - keys with nesting cannot have non-nested elements!" + .format(cur_key)) + exit(1) + add_to_dict(root[cur_key], '.'.join(keys[1:]), value) + else: + if len(keys) == 1: + root[cur_key] = value + else: + root[cur_key] = {} + add_to_dict(root[cur_key], '.'.join(keys[1:]), value) + + +def main(path_to_file): + languages = dict() + reader = None + if 'google.com' in path_to_file: + r = requests.get(path_to_file) + buff = io.StringIO(r.text) + reader = csv.DictReader(buff) + else: + f = open(path_to_file, 'r') + reader = csv.DictReader(f) + id_field_key = reader.fieldnames[0] + for language in reader.fieldnames[1:]: + languages[language] = dict() + print("DEBUG: Languages found are {}".format(list(languages.keys()))) + for i, row in enumerate(reader): + for language in languages.keys(): + add_to_dict(languages[language], row[id_field_key], + row[language]) + if 'google.com' not in path_to_file: + f.close() + os.makedirs(OUTPUT_DIR, exist_ok=True) + for language in languages.keys(): + with open(os.path.join(OUTPUT_DIR, language + '.json'), 'w') as f: + json.dump(languages[language], f, separators=(',', ':')) + print("INFO: language json have been output in {}".format( + os.path.abspath(OUTPUT_DIR))) + + +if __name__ == "__main__": + path_to_file = None + sheet_id = None + worksheet_name = None + if len(sys.argv) == 3: + sheet_id = sys.argv[1] + worksheet_name = sys.argv[2] + path_to_file = 'https://docs.google.com/spreadsheets/d/{0}/gviz/tq?tqx=out:csv&sheet={1}'.format(sheet_id, worksheet_name) + elif len(sys.argv) == 2: + path_to_file = sys.argv[1] + elif len(sys.argv) <= 1 and path_to_file is None: + path_to_file = input("Enter path to CSV file: ") + main(path_to_file) From 3a04cc03a2fae31ab0ee350988dba6b6e7c96665 Mon Sep 17 00:00:00 2001 From: Randall Wert Date: Wed, 9 Jun 2021 15:10:04 -0400 Subject: [PATCH 2/3] Updating readme and Python script --- csv_to_vanilla-i18n/README.md | 14 +++++- csv_to_vanilla-i18n/csv_to_vanilla_i18.py | 53 ++++++++++++++++++----- 2 files changed, 53 insertions(+), 14 deletions(-) mode change 100644 => 100755 csv_to_vanilla-i18n/csv_to_vanilla_i18.py diff --git a/csv_to_vanilla-i18n/README.md b/csv_to_vanilla-i18n/README.md index c02cb83..4537d1b 100644 --- a/csv_to_vanilla-i18n/README.md +++ b/csv_to_vanilla-i18n/README.md @@ -1,6 +1,6 @@ # CSV to Language JSONs -The script generates language JSON files from CSV. +The script generates language JSON files from CSV or from a Google Sheets worksheet. For reference, please check `example.csv` and generated language JSON files in `vanilla-i18n`. @@ -10,7 +10,17 @@ For reference, please check `example.csv` and generated language JSON files in ` NOTE: Python3 should be installed to use the script, follow instructions [here](https://realpython.com/installing-python/) to install it. -To run the script, `python3 csv_to_vanilla_i18n.py PATH_TO_LANGUAGE_CSV`. +To run the script, `python3 csv_to_vanilla_i18n.py [parameters]`. + +For parameters in the above line, you have several options: + +* You can specify the sheet ID and worksheet name of a Google Sheets worksheet, in that order, separated by a space. (Do NOT enter the entire URL to the Google Sheet.) + +* You can specify only the sheet ID and let the worksheet name default to ‘Sheet1’. + +* You can specify only the path to a local language CSV file. Be sure to include the .csv extension. + +* You can omit the parameters completely. Then you will be prompted for the name of a local language CSV file. Be sure to include the .csv extension. ## How to use NodeJS script diff --git a/csv_to_vanilla-i18n/csv_to_vanilla_i18.py b/csv_to_vanilla-i18n/csv_to_vanilla_i18.py old mode 100644 new mode 100755 index fef2c13..894d0c2 --- a/csv_to_vanilla-i18n/csv_to_vanilla_i18.py +++ b/csv_to_vanilla-i18n/csv_to_vanilla_i18.py @@ -1,6 +1,16 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Tue Jun 8 11:04:16 2021 + +@author: randallwert +""" + import csv +import io import json import os +import requests import sys OUTPUT_DIR = 'vanilla-i18n' @@ -32,16 +42,24 @@ def add_to_dict(root, key, value): def main(path_to_file): languages = dict() - with open(path_to_file, 'r') as f: + reader = None + if 'google.com' in path_to_file: + r = requests.get(path_to_file) + buff = io.StringIO(r.text) + reader = csv.DictReader(buff) + else: + f = open(path_to_file, 'r') reader = csv.DictReader(f) - id_field_key = reader.fieldnames[0] - for language in reader.fieldnames[1:]: - languages[language] = dict() - print("DEBUG: Languages found are {}".format(list(languages.keys()))) - for i, row in enumerate(reader): - for language in languages.keys(): - add_to_dict(languages[language], row[id_field_key], - row[language]) + id_field_key = reader.fieldnames[0] + for language in reader.fieldnames[1:]: + languages[language] = dict() + print("DEBUG: Languages found are {}".format(list(languages.keys()))) + for i, row in enumerate(reader): + for language in languages.keys(): + add_to_dict(languages[language], row[id_field_key], + row[language]) + if 'google.com' not in path_to_file: + f.close() os.makedirs(OUTPUT_DIR, exist_ok=True) for language in languages.keys(): with open(os.path.join(OUTPUT_DIR, language + '.json'), 'w') as f: @@ -52,8 +70,19 @@ def main(path_to_file): if __name__ == "__main__": path_to_file = None - if len(sys.argv) <= 1 and path_to_file is None: + sheet_id = None + worksheet_name = None + if len(sys.argv) == 3: + sheet_id = sys.argv[1] + worksheet_name = sys.argv[2] + path_to_file = 'https://docs.google.com/spreadsheets/d/{0}/gviz/tq?tqx=out:csv&sheet={1}'.format(sheet_id, worksheet_name) + elif len(sys.argv) == 2: + if '.csv' in sys.argv[1]: + path_to_file = sys.argv[1] + else: + sheet_id = sys.argv[1] + worksheet_name = 'Sheet1' + path_to_file = 'https://docs.google.com/spreadsheets/d/{0}/gviz/tq?tqx=out:csv&sheet={1}'.format(sheet_id, worksheet_name) + elif len(sys.argv) <= 1 and path_to_file is None: path_to_file = input("Enter path to CSV file: ") - else: - path_to_file = sys.argv[1] main(path_to_file) From 741dfb84f48f401616e367406af8ca0f8c2975ca Mon Sep 17 00:00:00 2001 From: Randall Wert Date: Wed, 9 Jun 2021 16:42:46 -0400 Subject: [PATCH 3/3] Update readme and remove unnecessary script --- csv_to_vanilla-i18n/README.md | 8 +- .../csv_or_sheet_to_vanilla_i18.py | 83 ------------------- 2 files changed, 4 insertions(+), 87 deletions(-) delete mode 100755 csv_to_vanilla-i18n/csv_or_sheet_to_vanilla_i18.py diff --git a/csv_to_vanilla-i18n/README.md b/csv_to_vanilla-i18n/README.md index 4537d1b..bedfd9d 100644 --- a/csv_to_vanilla-i18n/README.md +++ b/csv_to_vanilla-i18n/README.md @@ -14,13 +14,13 @@ To run the script, `python3 csv_to_vanilla_i18n.py [parameters]`. For parameters in the above line, you have several options: -* You can specify the sheet ID and worksheet name of a Google Sheets worksheet, in that order, separated by a space. (Do NOT enter the entire URL to the Google Sheet.) +* You can specify the sheet ID and worksheet name of a Google Sheets worksheet, in that order, separated by a space. Eg. `python3 csv_to_vanilla_i18n.py 1KfPLetq4VUvEApiGtWEUuQUhAYCrocyLmyiHV2cl_ks Sheet1` -* You can specify only the sheet ID and let the worksheet name default to ‘Sheet1’. +* You can specify only the sheet ID and let the worksheet name default to ‘Sheet1’. Eg. `python3 csv_to_vanilla_i18n.py 1KfPLetq4VUvEApiGtWEUuQUhAYCrocyLmyiHV2cl_ks` -* You can specify only the path to a local language CSV file. Be sure to include the .csv extension. +* You can specify only the path to a local language CSV file. Be sure to include the .csv extension. Eg. `python3 csv_to_vanilla_i18n.py /path/to/csv` -* You can omit the parameters completely. Then you will be prompted for the name of a local language CSV file. Be sure to include the .csv extension. +* You can omit the parameters completely. Then you will be prompted for the name of a local language CSV file. Be sure to include the .csv extension. Eg. `python3 csv_to_vanilla_i18n.py` ## How to use NodeJS script diff --git a/csv_to_vanilla-i18n/csv_or_sheet_to_vanilla_i18.py b/csv_to_vanilla-i18n/csv_or_sheet_to_vanilla_i18.py deleted file mode 100755 index bce6ed5..0000000 --- a/csv_to_vanilla-i18n/csv_or_sheet_to_vanilla_i18.py +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -Created on Tue Jun 8 11:04:16 2021 - -@author: randallwert -""" - -import csv -import io -import json -import os -import requests -import sys - -OUTPUT_DIR = 'vanilla-i18n' - - -def add_to_dict(root, key, value): - keys = key.split('.') - cur_key = keys[0] - if cur_key in root.keys(): - if len(keys) == 1: - print( - "ERROR: duplicate key '{}' - keys with nesting cannot have non-nested elements!" - .format(cur_key)) - exit(1) - else: - if not isinstance(root[cur_key], dict): - print( - "ERROR: duplicate key '{}' - keys with nesting cannot have non-nested elements!" - .format(cur_key)) - exit(1) - add_to_dict(root[cur_key], '.'.join(keys[1:]), value) - else: - if len(keys) == 1: - root[cur_key] = value - else: - root[cur_key] = {} - add_to_dict(root[cur_key], '.'.join(keys[1:]), value) - - -def main(path_to_file): - languages = dict() - reader = None - if 'google.com' in path_to_file: - r = requests.get(path_to_file) - buff = io.StringIO(r.text) - reader = csv.DictReader(buff) - else: - f = open(path_to_file, 'r') - reader = csv.DictReader(f) - id_field_key = reader.fieldnames[0] - for language in reader.fieldnames[1:]: - languages[language] = dict() - print("DEBUG: Languages found are {}".format(list(languages.keys()))) - for i, row in enumerate(reader): - for language in languages.keys(): - add_to_dict(languages[language], row[id_field_key], - row[language]) - if 'google.com' not in path_to_file: - f.close() - os.makedirs(OUTPUT_DIR, exist_ok=True) - for language in languages.keys(): - with open(os.path.join(OUTPUT_DIR, language + '.json'), 'w') as f: - json.dump(languages[language], f, separators=(',', ':')) - print("INFO: language json have been output in {}".format( - os.path.abspath(OUTPUT_DIR))) - - -if __name__ == "__main__": - path_to_file = None - sheet_id = None - worksheet_name = None - if len(sys.argv) == 3: - sheet_id = sys.argv[1] - worksheet_name = sys.argv[2] - path_to_file = 'https://docs.google.com/spreadsheets/d/{0}/gviz/tq?tqx=out:csv&sheet={1}'.format(sheet_id, worksheet_name) - elif len(sys.argv) == 2: - path_to_file = sys.argv[1] - elif len(sys.argv) <= 1 and path_to_file is None: - path_to_file = input("Enter path to CSV file: ") - main(path_to_file)