Skip to content

Commit

Permalink
feat: added all cli commands for static edhrec,
Browse files Browse the repository at this point in the history
  • Loading branch information
Guibod committed Feb 4, 2023
1 parent 8d6b2ed commit f522214
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 62 deletions.
163 changes: 106 additions & 57 deletions src/mightstone/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
from pydantic.json import pydantic_encoder

import mightstone
from mightstone.app import App
from mightstone.ass import asyncio_run, stream_as_list
from mightstone.services.edhrec import EdhRecStatic
from mightstone.services.edhrec import (
EdhRecCategory,
EdhRecIdentity,
EdhRecPeriod,
EdhRecStatic,
EdhRecType,
)

logger = logging.getLogger("mightstone")

Expand Down Expand Up @@ -79,69 +84,113 @@ def edhrec():
@click.pass_obj
@click.argument("name", nargs=1)
@click.argument("sub", required=False)
def commander(obj, name, sub):
logger.info(f"Searching commander {name} at edhrec")
with EdhRecStatic() as edhrec:
pretty_print(asyncio_run(edhrec.commander(name, sub)), obj.get("format"))
def commander(obj, **kwargs):
with EdhRecStatic() as client:
pretty_print(asyncio_run(client.commander(**kwargs)), obj.get("format"))


@edhrec.command()
@click.pass_obj
@click.argument("identity", required=False)
@click.option("-l", "--limit", type=int)
def tribes(obj, identity, limit):
logger.info(f"Searching tribes using color identity {identity} at edhrec")
with EdhRecStatic() as edhrec:
pretty_print(
stream_as_list(edhrec.tribes(identity, limit=limit)), obj.get("format")
)
def tribes(obj, **kwargs):
with EdhRecStatic() as client:
pretty_print(stream_as_list(client.tribes(**kwargs)), obj.get("format"))


@cli.command()
@click.option(
"-c",
"--conf",
"--config",
"config_file",
type=click.Path(exists=True),
)
def serve(config_file):
"""Start mightstone in server mode"""

settings = {}
if config_file:
try:
with open(config_file, "r") as stream:
try:
settings = yaml.safe_load(stream)
except yaml.YAMLError as exc:
click.echo(exc)
sys.exit(1)
except IOError as exc:
logger.fatal("%s: %s", exc.strerror, exc.filename)
sys.exit(1)
except Exception as exc:
logger.fatal(
"Cannot load conf file '%s'. Error message is: %s", config_file, exc
)
sys.exit(1)

# TODO: Create your application object
app = App(settings)
try:
logger.info("Starting")
# TODO: Start your application
app.start()
except KeyboardInterrupt:
pass
except Exception as exc:
logger.exception("Unexpected exception: %s", exc)
finally:
logger.info("Shutting down")
# TODO: Cleanup code
app.stop()

logger.info("All done")
@edhrec.command()
@click.pass_obj
@click.argument("identity", required=False)
@click.option("-l", "--limit", type=int)
def themes(obj, **kwargs):
with EdhRecStatic() as client:
pretty_print(stream_as_list(client.themes(**kwargs)), obj.get("format"))


@edhrec.command()
@click.pass_obj
@click.option("-l", "--limit", type=int)
def sets(obj, **kwargs):
with EdhRecStatic() as client:
pretty_print(stream_as_list(client.sets(**kwargs)), obj.get("format"))


@edhrec.command()
@click.pass_obj
@click.option("-l", "--limit", type=int)
def companions(obj, **kwargs):
with EdhRecStatic() as client:
pretty_print(stream_as_list(client.companions(**kwargs)), obj.get("format"))


@edhrec.command()
@click.pass_obj
@click.option("-i", "--identity", type=str)
@click.option("-l", "--limit", type=int)
def partners(obj, **kwargs):
with EdhRecStatic() as client:
pretty_print(stream_as_list(client.partners(**kwargs)), obj.get("format"))


@edhrec.command()
@click.pass_obj
@click.option("-i", "--identity", type=str)
@click.option("-l", "--limit", type=int, default=100)
def commanders(obj, **kwargs):
with EdhRecStatic() as client:
pretty_print(stream_as_list(client.commanders(**kwargs)), obj.get("format"))


@edhrec.command()
@click.pass_obj
@click.argument("identity", type=click.Choice([t.value for t in EdhRecIdentity]))
@click.option("-l", "--limit", type=int, default=100)
def combos(obj, **kwargs):
with EdhRecStatic() as client:
pretty_print(stream_as_list(client.combos(**kwargs)), obj.get("format"))


@edhrec.command()
@click.pass_obj
@click.argument("identity", type=click.Choice([t.value for t in EdhRecIdentity]))
@click.argument("identifier", type=str)
@click.option("-l", "--limit", type=int, default=100)
def combo(obj, **kwargs):
with EdhRecStatic() as client:
pretty_print(stream_as_list(client.combo(**kwargs)), obj.get("format"))


@edhrec.command()
@click.pass_obj
@click.argument("year", required=False, type=int)
@click.option("-l", "--limit", type=int)
def salt(obj, **kwargs):
with EdhRecStatic() as client:
pretty_print(stream_as_list(client.salt(**kwargs)), obj.get("format"))


@edhrec.command()
@click.pass_obj
@click.option("-t", "--type", type=click.Choice([t.value for t in EdhRecType]))
@click.option("-p", "--period", type=click.Choice([t.value for t in EdhRecPeriod]))
@click.option("-l", "--limit", type=int)
def top_cards(obj, **kwargs):
with EdhRecStatic() as client:
pretty_print(stream_as_list(client.top_cards(**kwargs)), obj.get("format"))


@edhrec.command()
@click.pass_obj
@click.option("-c", "--category", type=click.Choice([t.value for t in EdhRecCategory]))
@click.option("-t", "--theme", type=str)
@click.option("--commander", type=str)
@click.option("-i", "--identity", type=str)
@click.option("-s", "--set", type=str)
@click.option("-l", "--limit", type=int)
def cards(obj, **kwargs):
logger.info(f"Searching top cards using for type {kwargs}")
with EdhRecStatic() as client:
pretty_print(stream_as_list(client.cards(**kwargs)), obj.get("format"))


if __name__ == "__main__":
Expand Down
8 changes: 6 additions & 2 deletions src/mightstone/services/edhrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,12 @@ async def salt(
async def top_cards(
self,
type: EdhRecType = None,
period: Union[str, EdhRecPeriod] = EdhRecPeriod.PAST_WEEK,
period: EdhRecPeriod = EdhRecPeriod.PAST_WEEK,
limit: int = None,
) -> AsyncGenerator[EdhRecCardItem, None]:
period = EdhRecPeriod(period)
if type:
type = EdhRecType(type)
async for item in self._page_item_generator(
f"top/{type.value}.json", period, limit=limit
):
Expand Down Expand Up @@ -468,6 +469,9 @@ async def cards(
if category:
category = EdhRecCategory(category)

if not theme and not commander and not set:
raise ValueError("You must either provide a theme, commander or set")

if commander:
if theme:
raise ValueError("commander and theme options are mutually exclusive")
Expand Down Expand Up @@ -499,7 +503,7 @@ async def cards(
return

if identity and not theme:
raise ValueError("youn must specify a theme to search by color identity")
raise ValueError("you must specify a theme to search by color identity")

path = f"themes/{slugify(theme)}.json"
if identity:
Expand Down
24 changes: 21 additions & 3 deletions tests/mightstone/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,28 @@ def test_true():
assert 1 == 1


def test_click_cli():
def test_click_cli_help():
runner = CliRunner(mix_stderr=False)
result = runner.invoke(cli.cli, ["--help"])
assert result.exit_code == 0
assert "Start mightstone in server mode" in result.output
assert "Start mightstone in server mode" in result.stdout
assert "edhrec" in result.output
assert "version" in result.stdout
assert "" == result.stderr


def test_click_edhrec_help():
runner = CliRunner(mix_stderr=False)
result = runner.invoke(cli.edhrec, ["--help"])
assert result.exit_code == 0
assert "cards" in result.output
assert "combo" in result.output
assert "combos" in result.output
assert "commander" in result.output
assert "commanders" in result.output
assert "partners" in result.output
assert "salt" in result.output
assert "sets" in result.output
assert "themes" in result.output
assert "top-cards" in result.stdout
assert "tribes" in result.stdout
assert "" == result.stderr

0 comments on commit f522214

Please sign in to comment.