Add comments in script

This commit is contained in:
IshaVenikar 2025-06-03 11:52:10 +05:30
parent ec1e885799
commit 66e8135800
2 changed files with 65 additions and 19 deletions

View File

@ -3,16 +3,18 @@ import requests
import pandas as pd
import json
import re
import argparse
def get_excel_download_url(google_sheet_url):
"""
Convert a Google Sheets URL to its Excel export URL.
"""
# Extract the Sheet ID to construct the download URL
match = re.search(r'/d/([a-zA-Z0-9-_]+)', google_sheet_url)
if not match:
raise ValueError('Invalid Google Sheets URL')
sheet_id = match.group(1)
# Export the first sheet as Excel
# Export the entire spreadsheet as an Excel file (includes all sheets)
return f'https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=xlsx&id={sheet_id}'
def download_excel(url, output_path):
@ -25,11 +27,11 @@ def download_excel(url, output_path):
with open(output_path, 'wb') as f:
f.write(response.content)
def convert_excel_to_json(excel_path, json_path):
def convert_excel_to_json(excel_path, json_path, sheet_name):
"""
Read the Excel file, extract columns from the 'Genesis Allocation' sheet, and save as JSON.
Read the Excel file, extract columns from the specified sheet, and save as JSON.
"""
df = pd.read_excel(excel_path, sheet_name='Genesis Allocation')
df = pd.read_excel(excel_path, sheet_name=sheet_name)
# Ensure columns exist
required_columns = [
'Placeholder',
@ -45,39 +47,48 @@ def convert_excel_to_json(excel_path, json_path):
result = {}
for _, row in df.iterrows():
placeholder = str(row['Placeholder']) if not pd.isna(row['Placeholder']) else ''
laconic_address = str(row['Laconic Address']) if not pd.isna(row['Laconic Address']) else ''
# Use laconic_address as key if placeholder is missing or empty
key = placeholder if placeholder and placeholder.lower() != 'nan' else laconic_address
# Skip the row if both 'Placeholder' and 'Laconic Address' are missing or invalid
if not key or key.lower() == 'nan':
continue
entry = {
'total_lps_allocation': row['Total LPS Allocation'] if not pd.isna(row['Total LPS Allocation']) else None,
'lock_months': row['Lock (months)'] if not pd.isna(row['Lock (months)']) else None,
'vest_months': row['Vest (months)'] if not pd.isna(row['Vest (months)']) else None,
'laconic_address': row['Laconic Address'] if not pd.isna(row['Laconic Address']) else None
}
result[key] = entry
with open(json_path, 'w') as f:
json.dump(result, f, indent=2)
def main():
if len(sys.argv) != 2:
print('Usage: python generate-lps-distribution-json.py <google_sheet_url_or_excel_file_path>')
sys.exit(1)
input_arg = sys.argv[1]
if input_arg.startswith('https://'):
excel_url = get_excel_download_url(input_arg)
parser = argparse.ArgumentParser(description='Generate LPS distribution JSON from Excel or Google Sheet')
parser.add_argument('--input', '-i', required=True, help='Input: Google Sheet URL or local Excel file path')
parser.add_argument('--output', '-o', default='distribution.json', help='Output JSON file path (default: distribution.json)')
parser.add_argument('--sheet', '-s', default='Genesis Allocation', help='Sheet name to read (default: Genesis Allocation)')
args = parser.parse_args()
if args.input.startswith('https://'):
excel_url = get_excel_download_url(args.input)
excel_path = 'sheet.xlsx'
print(f'Downloading Excel file from: {excel_url}')
download_excel(excel_url, excel_path)
else:
excel_path = input_arg
excel_path = args.input
print(f'Using Excel file at path: {excel_path}')
json_path = 'distribution.json'
print('Converting Excel to JSON...')
convert_excel_to_json(excel_path, json_path)
print(f'JSON saved to {json_path}')
print(f'Converting Excel to JSON (sheet: {args.sheet})...')
convert_excel_to_json(excel_path, args.output, args.sheet)
print(f'JSON saved to {args.output}')
if __name__ == '__main__':
main()

View File

@ -2,12 +2,44 @@
set -e
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <google_sheet_url>"
# Default values
INPUT=""
OUTPUT_DIR="."
SHEET="Genesis Allocation"
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-i|--input)
INPUT="$2"
shift 2
;;
-d|--dir)
OUTPUT_DIR="$2"
shift 2
;;
-s|--sheet)
SHEET="$2"
shift 2
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 -i|--input <input_url_or_path> [-d|--dir <output_directory>] [-s|--sheet <sheet_name>]"
exit 1
;;
esac
done
# Check if input is provided
if [ -z "$INPUT" ]; then
echo "Error: Input URL or path is required"
echo "Usage: $0 -i|--input <input_url_or_path> [-d|--dir <output_directory>] [-s|--sheet <sheet_name>]"
exit 1
fi
sheet_url="$1"
# Create output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"
venv_dir="$PWD/venv-lps-lock"
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@ -21,7 +53,10 @@ fi
"$venv_dir/bin/pip" install requests pandas openpyxl
echo "Running LPS lock generation script..."
"$venv_dir/bin/python" "$script_dir/generate-lps-distribution-json.py" "$sheet_url"
"$venv_dir/bin/python" "$script_dir/generate-lps-distribution-json.py" \
--input "$INPUT" \
--output "$OUTPUT_DIR/distribution.json" \
--sheet "$SHEET"
# Clean up venv
echo "Cleaning up..."