Skip to content

Commit

Permalink
feat: add cli (#12)
Browse files Browse the repository at this point in the history
* feat: add default token list getter
* feat: add tokenlists cli
  • Loading branch information
fubuloubu authored Apr 13, 2021
1 parent 40a7723 commit 5f78bc9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
long_description_content_type="text/markdown",
url="https://github.com/ApeWorX/py-tokenlists",
packages=find_packages(exclude=["tests", "tests.*"]),
install_requires=["semantic-version>=2.8.5,<3"],
install_requires=["semantic-version>=2.8.5,<3", "pyyaml>=5.4.1,<6"],
entry_points={"console_scripts": ["tokenlists=tokenlists._cli:cli"]},
extras_require=extras_require,
classifiers=[
"Development Status :: 3 - Alpha",
Expand Down
2 changes: 2 additions & 0 deletions tokenlists/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .manager import (
available_token_lists,
default_token_list,
get_token_info,
get_token_list,
install_token_list,
Expand All @@ -13,6 +14,7 @@
"install_token_list",
"set_default_token_list",
"available_token_lists",
"default_token_list",
"get_token_info",
"get_token_list",
]
48 changes: 48 additions & 0 deletions tokenlists/_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import click
import yaml

import tokenlists


@click.group()
def cli():
"""
Utility for working with the `py-tokenlists` installed token lists
"""


@cli.command(short_help="Display the names and versions of all installed tokenlists")
def show_lists():
click.echo("Installed Token Lists:")
for token_list in map(tokenlists.get_token_list, tokenlists.available_token_lists()):
click.echo(f"- {token_list.name} (v{token_list.version})")


@cli.command(short_help="Display the info for a particular token")
@click.argument("symbol", type=tokenlists.typing.TokenSymbol)
@click.option(
"--token-list",
type=click.Choice(tokenlists.available_token_lists()),
default=tokenlists.default_token_list(),
)
def token_info(symbol, token_list):
token_info = tokenlists.get_token_info(symbol, token_list)
click.echo(yaml.dump(token_info.to_dict()))


@cli.command(short_help="Install a new token list")
@click.argument("uri")
def install(uri):
tokenlists.install_token_list(uri)


@cli.command(short_help="Remove an existing token list")
@click.argument("name", type=click.Choice(tokenlists.available_token_lists()))
def remove(name):
tokenlists.uninstall_token_list(name)


@cli.command(short_help="Set the default tokenlist")
@click.argument("name", type=click.Choice(tokenlists.available_token_lists()))
def set_default(name):
tokenlists.set_default_token_list(name)
10 changes: 10 additions & 0 deletions tokenlists/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ def uninstall_token_list(token_list_name: str = "") -> None:
del INSTALLED_TOKEN_LISTS[token_list.name]


def default_token_list() -> str:
_load_token_lists()
if config.DEFAULT_TOKEN_LIST:
return config.DEFAULT_TOKEN_LIST
elif len(INSTALLED_TOKEN_LISTS) == 0:
raise ValueError("No tokenlists installed!")
else:
return list(INSTALLED_TOKEN_LISTS)[0] # just return the first


def set_default_token_list(name: str) -> None:
_load_token_lists()
if name not in INSTALLED_TOKEN_LISTS:
Expand Down

0 comments on commit 5f78bc9

Please sign in to comment.