Skip to content

Commit

Permalink
Merge pull request #15 from njgheorghita/entrypoint
Browse files Browse the repository at this point in the history
Add entrypoint for ethpm cli
  • Loading branch information
njgheorghita authored May 14, 2019
2 parents 20ce3b0 + 228fcfb commit 0f5d5dc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
4 changes: 3 additions & 1 deletion ethpm_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

CLI_ASSETS_DIR = Path(__file__).parent / "assets"
CLI_ASSETS_DIR: Path = Path(__file__).parent / "assets"

from .main import main # noqa: F401
30 changes: 13 additions & 17 deletions __main__.py → ethpm_cli/main.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -28,26 +27,27 @@ 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,
last_scraped_block_hash,
)


def parse_arguments():
def parse_arguments() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="ethpm-cli")
subparsers = parser.add_subparsers(help="commands", dest="command")

Expand Down Expand Up @@ -94,7 +94,9 @@ def parse_arguments():
return parser


def main(parser, logger):
def main() -> None:
logger = setup_cli_logger()
parser = parse_arguments()
logger.info(f"EthPM CLI v{__version__}\n")

args = parser.parse_args()
Expand All @@ -113,12 +115,6 @@ def main(parser, logger):
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
)


if __name__ == "__main__":
logger = get_logger()
parser = parse_arguments()
main(parser, logger)
2 changes: 1 addition & 1 deletion ethpm_cli/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
],
entry_points={
'console_scripts': [
'ethpm=ethpm_cli:main',
],
},
)
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/

0 comments on commit 0f5d5dc

Please sign in to comment.