From 983517c9358e3ba1df0976735d9c692bbe8ef964 Mon Sep 17 00:00:00 2001 From: KPrasch Date: Wed, 7 Aug 2024 19:39:32 +0700 Subject: [PATCH] type annotations and docstrings; cleanup for list_contracts CLI --- scripts/list_contracts.py | 42 +++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/scripts/list_contracts.py b/scripts/list_contracts.py index e7bed1ca..2628530f 100644 --- a/scripts/list_contracts.py +++ b/scripts/list_contracts.py @@ -2,48 +2,41 @@ from itertools import groupby +from typing import Optional, List, Tuple import click from ape.cli import ConnectedProviderCommand from deployment.constants import SUPPORTED_TACO_DOMAINS -from deployment.registry import read_registry +from deployment.registry import read_registry, RegistryEntry from deployment.utils import get_chain_name, registry_filepath_from_domain -def format_chain_name(chain_name): - """ - Format the chain name to capitalize each word and join with slashes. - """ +def _format_chain_name(chain_name: str) -> str: + """Format the chain name to capitalize each word and join with slashes.""" return "/".join(word.capitalize() for word in chain_name.split()) -def get_registry_entries(selected_domain): - """ - Parse the registry files for the given domain or all supported domains. - """ - registry_entries = [] - - for domain in SUPPORTED_TACO_DOMAINS: - if selected_domain and selected_domain != domain: +def _get_registry_entries(domain: Optional[str] = None) -> List[Tuple[str, List[RegistryEntry]]]: + """Parse the registry files for the given domain or all supported domains.""" + registry_entries = list() + for taco_domain in SUPPORTED_TACO_DOMAINS: + if domain and domain != taco_domain: continue - registry_filepath = registry_filepath_from_domain(domain=domain) + registry_filepath = registry_filepath_from_domain(domain=taco_domain) entries = read_registry(filepath=registry_filepath) - registry_entries.append((domain, entries)) - + registry_entries.append((taco_domain, entries)) return registry_entries -def display_registry_entries(registry_entries): - """ - Display registry entries grouped by chain ID. - """ +def _display_registry_entries(registry_entries: List[Tuple[str, List[RegistryEntry]]]) -> None: + """Display registry entries grouped by chain ID.""" for domain, entries in registry_entries: - grouped_entries = groupby(entries, key=lambda entry: entry.chain_id) + grouped_entries = groupby(entries, key=lambda e: e.chain_id) click.secho(f"\n{domain.capitalize()} Domain", fg="green") for chain_id, chain_entries in grouped_entries: - chain_name = format_chain_name(get_chain_name(chain_id)) + chain_name = _format_chain_name(get_chain_name(chain_id)) click.secho(f" {chain_name}", fg="yellow") for index, entry in enumerate(chain_entries, start=1): @@ -58,8 +51,9 @@ def display_registry_entries(registry_entries): type=click.Choice(SUPPORTED_TACO_DOMAINS), ) def cli(domain): - registry_entries = get_registry_entries(domain) - display_registry_entries(registry_entries) + """List all contracts in the registry. Optionally filter by domain.""" + registry_entries = _get_registry_entries(domain) + _display_registry_entries(registry_entries) if __name__ == "__main__":