diff --git a/csv_to_vanilla-i18n/README.md b/csv_to_vanilla-i18n/README.md index c02cb83..bedfd9d 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. 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’. 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. 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. Eg. `python3 csv_to_vanilla_i18n.py` ## 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)