diff --git a/setup.py b/setup.py index 48dd17d..0606750 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/tokenlists/__init__.py b/tokenlists/__init__.py index e1482a3..99c2365 100644 --- a/tokenlists/__init__.py +++ b/tokenlists/__init__.py @@ -1,5 +1,6 @@ from .manager import ( available_token_lists, + default_token_list, get_token_info, get_token_list, install_token_list, @@ -13,6 +14,7 @@ "install_token_list", "set_default_token_list", "available_token_lists", + "default_token_list", "get_token_info", "get_token_list", ] diff --git a/tokenlists/_cli.py b/tokenlists/_cli.py new file mode 100644 index 0000000..b074c39 --- /dev/null +++ b/tokenlists/_cli.py @@ -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) diff --git a/tokenlists/manager.py b/tokenlists/manager.py index 25edd1e..f46791b 100644 --- a/tokenlists/manager.py +++ b/tokenlists/manager.py @@ -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: