Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Google sheet capability (#10) fixes #9 #10

Merged
merged 3 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions csv_to_vanilla-i18n/README.md
Original file line number Diff line number Diff line change
@@ -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`.

Expand All @@ -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

Expand Down
53 changes: 41 additions & 12 deletions csv_to_vanilla-i18n/csv_to_vanilla_i18.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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:
Expand All @@ -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)