From 62ece58a96c3b458b8318a03233f62543dc9be9f Mon Sep 17 00:00:00 2001 From: Nabarun Date: Tue, 13 Aug 2024 12:50:34 +0530 Subject: [PATCH] Create python CLI to generate csv for subscribed and onboarded laconicd accounts --- .gitignore | 6 ++++++ cli/README.md | 32 +++++++++++++++++++++++++++++++ cli/cli.py | 15 +++++++++++++++ cli/core.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + setup.py | 15 +++++++++++++++ 6 files changed, 119 insertions(+) create mode 100644 .gitignore create mode 100644 cli/README.md create mode 100644 cli/cli.py create mode 100644 cli/core.py create mode 100644 requirements.txt create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..422c006 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +venv +laconic_testnet.egg-info +__pycache__ + +*-deployment +*-spec.yml diff --git a/cli/README.md b/cli/README.md new file mode 100644 index 0000000..6bd81fe --- /dev/null +++ b/cli/README.md @@ -0,0 +1,32 @@ +# cli + +## Setup + +* Run commands in repo root directory + ```bash + python3 -m venv venv + source ./venv/bin/activate + ``` + +* Install CLI in editable mode + ```bash + pip install --editable . + ``` + +* Verify installation + ```bash + laconic-testnet --help + ``` + +## Usage + +```bash +laconic-testnet --onboarded-json path/to/onboarded-accounts.json --csv-input path/to/subscribers.csv --output path/to/subscribed-onboarded-accounts.csv +``` + +## Cleanup + +Deactivate virtual environment +```bash +deactivate +``` diff --git a/cli/cli.py b/cli/cli.py new file mode 100644 index 0000000..7b2a237 --- /dev/null +++ b/cli/cli.py @@ -0,0 +1,15 @@ +import click +from .core import process_subscribers + +@click.command() +@click.option('--onboarded-json', required=True, type=click.Path(exists=True), help='Path to onboarded accounts JSON file.') +@click.option('--subscribers-csv', required=True, type=click.Path(exists=True), help='Path to the subscribers CSV file.') +@click.option('--output', required=True, type=click.Path(), help='Path to the output CSV file.') +def main(onboarded_json, subscribers_csv, output): + """ + CLI tool to match subscriber data with participant data and generate a CSV. + """ + process_subscribers(onboarded_json, subscribers_csv, output) + +if __name__ == "__main__": + main() diff --git a/cli/core.py b/cli/core.py new file mode 100644 index 0000000..5bd3647 --- /dev/null +++ b/cli/core.py @@ -0,0 +1,50 @@ +import csv +import hashlib +import json + +def hash_subscriber_id(subscriber_id): + return hashlib.sha256(subscriber_id.encode()).hexdigest() + +def process_subscribers(onboarded_json, subscribers_csv, output): + # Load JSON data from the file + with open(onboarded_json, 'r') as json_file: + json_data = json.load(json_file) + + # Create a dictionary mapping kyc_id to participant data + kyc_map = {participant['kyc_id']: participant for participant in json_data['participants']} + + # Load subscribers data from the CSV file and process it using map + with open(subscribers_csv, 'r') as csv_file: + csv_reader = csv.DictReader(csv_file) + subscribers = list(csv_reader) + + # Use map to process subscribers + def process_subscriber(subscriber): + hashed_subscriber_id = hash_subscriber_id(subscriber['subscriber_id']) + participant = kyc_map.get(hashed_subscriber_id) + + if participant: + return { + 'subscriber_id': subscriber['subscriber_id'], + 'email': subscriber['email'], + 'cosmos_address': participant['cosmos_address'], + 'nitro_address': participant['nitro_address'], + 'role': participant['role'], + 'kyc_id': participant['kyc_id'], + 'status': subscriber['status'], + 'premium': subscriber['premium?'], + 'created_at': subscriber['created_at'] + } + return None + + # Apply the map function and filter out None values + output_data = list(filter(None, map(process_subscriber, subscribers))) + + # Write the matched data to a new CSV file + with open(output, 'w', newline='') as csv_file: + fieldnames = ['subscriber_id', 'email', 'cosmos_address', 'nitro_address', 'role', 'kyc_id', 'status', 'premium', 'created_at'] + csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames) + csv_writer.writeheader() + csv_writer.writerows(output_data) + + print(f'Data has been written to {output}') \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d77b06d --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Click diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..9087930 --- /dev/null +++ b/setup.py @@ -0,0 +1,15 @@ +from setuptools import setup, find_packages + +setup( + name="laconic-testnet", + version="0.1.0", + packages=find_packages(), + install_requires=[ + "Click", + ], + entry_points={ + 'console_scripts': [ + 'laconic-testnet = cli.cli:main', + ], + }, +)