From a43acbda0b3d0980d277c87139d0f6950553b224 Mon Sep 17 00:00:00 2001 From: Hiroyuki Date: Tue, 24 Sep 2024 00:51:07 +0200 Subject: [PATCH] fix: added configparser for loading configurations --- .env.example | 2 ++ pyproject.toml | 1 + src/bt_auto_dumper/_v2/__main__.py | 40 +++++++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..85f58fd --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +BT_AUTO_DUMPER_APIVER=v1 +CONFIG_DIR=~/.config/autovalidator diff --git a/pyproject.toml b/pyproject.toml index 82ccd04..b058e9c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ dynamic = [ "version", ] dependencies = [ + "python-dotenv", "requests", "bittensor>=7.4.0", ] diff --git a/src/bt_auto_dumper/_v2/__main__.py b/src/bt_auto_dumper/_v2/__main__.py index 318eda9..2bd2db4 100644 --- a/src/bt_auto_dumper/_v2/__main__.py +++ b/src/bt_auto_dumper/_v2/__main__.py @@ -1,5 +1,7 @@ import argparse +import configparser import json +import os import pathlib import re import subprocess @@ -9,18 +11,48 @@ import bittensor as bt # type: ignore import requests +from dotenv import load_dotenv + +load_dotenv() def main(apiver: str | None = None): apiver = apiver or pathlib.Path(__file__).parent.name parser = argparse.ArgumentParser(description=f"BT Auto Dumper CLI {apiver}") parser.add_argument("--note", help="Comment or note for the operation", type=str, default="") - parser.add_argument("subnet_identifier", help="Subnet Identifier", type=str) - parser.add_argument("autovalidator_address", help="AutoValidator Address", type=str) args = parser.parse_args() - dump_and_upload(args.subnet_identifier, args.autovalidator_address, args.note) + # Get configuration directory from env variable. + config_base_dir = os.getenv("CONFIG_DIR") + + # Check if the CONFIG_DIR environment variable is set + if not config_base_dir: + raise RuntimeError("CONFIG_DIR environment variable is not set.") + + config_expanded_dir = os.path.expanduser(config_base_dir) + + # Define the full path for the configuration file + config_path = os.path.join(config_expanded_dir, "config.ini") + + if not os.path.exists(config_path): + raise FileNotFoundError(f"{config_path} does not exist.") + + # Read the configuration file + config = configparser.ConfigParser() + try: + config.read(config_path) + except Exception as e: + raise RuntimeError(f"Error reading configuration file: {config_path} \n Error:{e}") + print(config_path) + # Extract required values from the configuration + try: + autovalidator_address = config.get("subnet", "autovalidator_address") + subnet_identifier = config.get("subnet", "codename") + except Exception as e: + raise KeyError(f"Configuration error: Missing in the config file. \n Error:{e}") + + dump_and_upload(subnet_identifier, autovalidator_address, args.note) def dump_and_upload(subnet_identifier: str, autovalidator_address: str, note: str): @@ -92,7 +124,7 @@ def make_signed_request(method: str, url: str, headers: dict, file_path: str, wa file_content = file.read() file.seek(0) headers_str = json.dumps(headers, sort_keys=True) - data_to_sign = f"{method}{url}{headers_str}{file_content.decode(errors="ignore")}".encode() + data_to_sign = f"{method}{url}{headers_str}{file_content.decode(errors='ignore')}".encode() signature = wallet.hotkey.sign( data_to_sign, ).hex()