From 512614dd02bbaacebfbbdec72356ce6d6420f932 Mon Sep 17 00:00:00 2001 From: Nick Gheorghita Date: Tue, 14 May 2019 16:59:23 +0200 Subject: [PATCH 1/2] Add entrypoint for ethpm cli --- ethpm_cli/__init__.py | 4 ++++ __main__.py => ethpm_cli/main.py | 10 +++------- setup.py | 5 +++++ 3 files changed, 12 insertions(+), 7 deletions(-) rename __main__.py => ethpm_cli/main.py (97%) diff --git a/ethpm_cli/__init__.py b/ethpm_cli/__init__.py index 81ba4d3..f0b089a 100644 --- a/ethpm_cli/__init__.py +++ b/ethpm_cli/__init__.py @@ -1,3 +1,7 @@ from pathlib import Path CLI_ASSETS_DIR = Path(__file__).parent / "assets" + +from .main import ( # noqa: F401 + main, +) diff --git a/__main__.py b/ethpm_cli/main.py similarity index 97% rename from __main__.py rename to ethpm_cli/main.py index 61a0004..34e3ab1 100644 --- a/__main__.py +++ b/ethpm_cli/main.py @@ -94,7 +94,9 @@ def parse_arguments(): return parser -def main(parser, logger): +def main(): + logger = get_logger() + parser = parse_arguments() logger.info(f"EthPM CLI v{__version__}\n") args = parser.parse_args() @@ -116,9 +118,3 @@ def main(parser, logger): "%s is an invalid command. Use `ethpmcli --help` to see the list of available commands.", args.command, ) - - -if __name__ == "__main__": - logger = get_logger() - parser = parse_arguments() - main(parser, logger) diff --git a/setup.py b/setup.py index 2705cb5..5ba7823 100644 --- a/setup.py +++ b/setup.py @@ -70,4 +70,9 @@ 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', ], + entry_points={ + 'console_scripts': [ + 'ethpm=ethpm_cli:main', + ], + }, ) From 228fcfbf8ec5d9caf120e37e1215daa5bbb2d3fd Mon Sep 17 00:00:00 2001 From: Nick Gheorghita Date: Tue, 14 May 2019 17:26:01 +0200 Subject: [PATCH 2/2] Add types to main.py --- ethpm_cli/__init__.py | 6 ++---- ethpm_cli/main.py | 24 ++++++++++++------------ ethpm_cli/scraper.py | 2 +- tox.ini | 3 ++- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/ethpm_cli/__init__.py b/ethpm_cli/__init__.py index f0b089a..879eea7 100644 --- a/ethpm_cli/__init__.py +++ b/ethpm_cli/__init__.py @@ -1,7 +1,5 @@ from pathlib import Path -CLI_ASSETS_DIR = Path(__file__).parent / "assets" +CLI_ASSETS_DIR: Path = Path(__file__).parent / "assets" -from .main import ( # noqa: F401 - main, -) +from .main import main # noqa: F401 diff --git a/ethpm_cli/main.py b/ethpm_cli/main.py index 34e3ab1..16d1ed0 100644 --- a/ethpm_cli/main.py +++ b/ethpm_cli/main.py @@ -1,25 +1,24 @@ import argparse import logging from pathlib import Path -import pkg_resources import sys +import pkg_resources from web3 import Web3 from web3.middleware import local_filter_middleware from web3.providers.auto import load_provider_from_uri -from ethpm_cli.install import Config, install_package +from ethpm_cli._utils.xdg import get_xdg_ethpmcli_root from ethpm_cli.constants import INFURA_HTTP_URI +from ethpm_cli.install import Config, install_package from ethpm_cli.package import Package from ethpm_cli.scraper import scrape from ethpm_cli.validation import validate_install_cli_args -from ethpm_cli._utils.xdg import get_xdg_ethpmcli_root - __version__ = pkg_resources.require("ethpm-cli")[0].version -def get_logger(): +def setup_cli_logger() -> logging.Logger: logger = logging.getLogger() logger.setLevel(logging.INFO) handler = logging.StreamHandler(sys.stdout) @@ -28,18 +27,19 @@ def get_logger(): return logger -def setup_scraper(): +def setup_scraper() -> Web3: w3 = Web3(load_provider_from_uri(INFURA_HTTP_URI)) w3.middleware_onion.add(local_filter_middleware) return w3 -def scraper(args): +def scraper(args: argparse.Namespace) -> None: w3 = setup_scraper() ethpmcli_dir = get_xdg_ethpmcli_root() start_block = args.start_block if args.start_block else 0 last_scraped_block = scrape(w3, ethpmcli_dir, start_block) last_scraped_block_hash = w3.eth.getBlock(last_scraped_block)["hash"] + logger = setup_cli_logger() logger.info( "All blocks scraped up to # %d: %s.", last_scraped_block, @@ -47,7 +47,7 @@ def scraper(args): ) -def parse_arguments(): +def parse_arguments() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(description="ethpm-cli") subparsers = parser.add_subparsers(help="commands", dest="command") @@ -94,8 +94,8 @@ def parse_arguments(): return parser -def main(): - logger = get_logger() +def main() -> None: + logger = setup_cli_logger() parser = parse_arguments() logger.info(f"EthPM CLI v{__version__}\n") @@ -115,6 +115,6 @@ def main(): scraper(args) else: parser.error( - "%s is an invalid command. Use `ethpmcli --help` to see the list of available commands.", - args.command, + "%s is an invalid command. Use `ethpmcli --help` to " + "see the list of available commands." % args.command ) diff --git a/ethpm_cli/scraper.py b/ethpm_cli/scraper.py index cdafe23..59f0c55 100644 --- a/ethpm_cli/scraper.py +++ b/ethpm_cli/scraper.py @@ -20,7 +20,7 @@ logger = logging.getLogger("ethpm_cli.scraper.Scraper") -def scrape(w3: Web3, ethpm_dir: Path, start_block: int = 0) -> None: +def scrape(w3: Web3, ethpm_dir: Path, start_block: int = 0) -> int: """ Scrapes VersionRelease event data starting from start_block. diff --git a/tox.ini b/tox.ini index 88fab88..1abeff7 100644 --- a/tox.ini +++ b/tox.ini @@ -14,6 +14,7 @@ line_length=88 multi_line_output=3 force_grid_wrap=0 use_parentheses=True +skip=__init__.py [flake8] max-line-length= 100 @@ -42,4 +43,4 @@ commands= flake8 {toxinidir}/ethpm_cli {toxinidir}/tests mypy --follow-imports=silent --ignore-missing-imports --check-untyped-defs --disallow-incomplete-defs --disallow-untyped-defs --disallow-any-generics -p ethpm_cli black --check --diff {toxinidir}/ethpm_cli/ --check --diff {toxinidir}/tests/ - isort --recursive {toxinidir}/ethpm_cli/ {toxinidir}/tests/ + isort --check-only --recursive {toxinidir}/ethpm_cli/ {toxinidir}/tests/