Skip to content

Commit

Permalink
feat: mtgjson services applies the new method() / method_async() dich…
Browse files Browse the repository at this point in the history
…otomy
  • Loading branch information
Guibod committed Mar 6, 2023
1 parent e7dfb06 commit d407077
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 60 deletions.
102 changes: 76 additions & 26 deletions src/mightstone/services/mtgjson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pydantic.error_wrappers import ValidationError

from mightstone import logger
from mightstone.ass import compressor
from mightstone.ass import compressor, synchronize
from mightstone.services import MightstoneHttpClient, ServiceError
from mightstone.services.mtgjson.models import (
Card,
Expand Down Expand Up @@ -119,7 +119,7 @@ def __init__(
compression = MtgJsonCompression.GZIP
self.compression = MtgJsonCompression(compression)

async def all_printings(self) -> AsyncGenerator[CardSet, None]:
async def all_printings_async(self) -> AsyncGenerator[CardSet, None]:
"""
all Card (Set) cards, including all printings and variations, categorized by
set.
Expand All @@ -131,7 +131,9 @@ async def all_printings(self) -> AsyncGenerator[CardSet, None]:
):
yield item

async def all_identifiers(self) -> AsyncGenerator[Card, None]:
all_printings = synchronize(all_printings_async)

async def all_identifiers_async(self) -> AsyncGenerator[Card, None]:
"""
all Card (Set) cards organized by card UUID.
Expand All @@ -140,7 +142,9 @@ async def all_identifiers(self) -> AsyncGenerator[Card, None]:
async for k, item in self._iterate_model(kind="AllIdentifiers", model=Card):
yield item

async def all_prices(self) -> AsyncGenerator[CardPrices, None]:
all_identifiers = synchronize(all_identifiers_async)

async def all_prices_async(self) -> AsyncGenerator[CardPrices, None]:
"""
all prices of cards in various formats.
Expand All @@ -149,7 +153,9 @@ async def all_prices(self) -> AsyncGenerator[CardPrices, None]:
async for k, item in self._iterate_model(kind="AllPrices"):
yield CardPrices(uuid=k, **item)

async def atomic_cards(self) -> AsyncGenerator[CardAtomic, None]:
all_prices = synchronize(all_prices_async)

async def atomic_cards_async(self) -> AsyncGenerator[CardAtomic, None]:
"""
every Card (Atomic) card.
Expand All @@ -158,23 +164,29 @@ async def atomic_cards(self) -> AsyncGenerator[CardAtomic, None]:
async for item in self._atomic(kind="AtomicCards"):
yield item

async def card_types(self) -> CardTypes:
atomic_cards = synchronize(atomic_cards_async)

async def card_types_async(self) -> CardTypes:
"""
every card type of any type of card.
:return: A ``CardTypes`` object
"""
return await self._get_item("CardTypes", model=CardTypes)

async def compiled_list(self) -> List[str]:
card_types = synchronize(card_types_async)

async def compiled_list_async(self) -> List[str]:
"""
all individual outputs from MTGJSON, such as AllPrintings, CardTypes, etc.
:return: A list of string
"""
return await self._get_item("CompiledList", model=list)

async def deck_list(self) -> AsyncGenerator[DeckList, None]:
compiled_list = synchronize(compiled_list_async)

async def deck_list_async(self) -> AsyncGenerator[DeckList, None]:
"""
all individual Deck data.
Expand All @@ -185,7 +197,9 @@ async def deck_list(self) -> AsyncGenerator[DeckList, None]:
):
yield item

async def deck(self, file_name: str) -> Deck:
deck_list = synchronize(deck_list_async)

async def deck_async(self, file_name: str) -> Deck:
"""
Recovers a deck data
Expand All @@ -194,23 +208,29 @@ async def deck(self, file_name: str) -> Deck:
"""
return await self._get_item(f"decks/{file_name}", model=Deck)

async def enum_values(self) -> dict:
deck = synchronize(deck_async)

async def enum_values_async(self) -> dict:
"""
All known property values for various Data Models.
:return: a ``dict`` object
"""
return await self._get_item("EnumValues", model=dict)

async def keywords(self) -> Keywords:
enum_values = synchronize(enum_values_async)

async def keywords_async(self) -> Keywords:
"""
a list of possible all keywords used on all cards.
:return: A ``Keywords`` object
"""
return await self._get_item("Keywords", model=Keywords)

async def legacy(self) -> AsyncGenerator[Set, None]:
keywords = synchronize(keywords_async)

async def legacy_async(self) -> AsyncGenerator[Set, None]:
"""
all Card (Set) cards organized by Set, restricted to sets legal in the
Legacy format.
Expand All @@ -220,7 +240,9 @@ async def legacy(self) -> AsyncGenerator[Set, None]:
async for k, item in self._iterate_model(kind="Legacy", model=Set):
yield item

async def legacy_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
legacy = synchronize(legacy_async)

async def legacy_atomic_async(self) -> AsyncGenerator[CardAtomicGroup, None]:
"""
all Card (Set) cards organized by Set, restricted to sets legal in the
Legacy format.
Expand All @@ -230,7 +252,9 @@ async def legacy_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
async for item in self._atomic(kind="LegacyAtomic"):
yield item

async def meta(self) -> Meta:
legacy_atomic = synchronize(legacy_atomic_async)

async def meta_async(self) -> Meta:
"""
the metadata object with ISO 8601 dates for latest build and SemVer
specifications of the MTGJSON release.
Expand All @@ -239,7 +263,9 @@ async def meta(self) -> Meta:
"""
return await self._get_item("Meta", model=Meta)

async def modern(self) -> AsyncGenerator[Set, None]:
meta = synchronize(meta_async)

async def modern_async(self) -> AsyncGenerator[Set, None]:
"""
all Card (Set) cards organized by Set, restricted to sets legal in the
Modern format.
Expand All @@ -249,7 +275,9 @@ async def modern(self) -> AsyncGenerator[Set, None]:
async for k, item in self._iterate_model(kind="Modern", model=Set):
yield item

async def modern_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
modern = synchronize(modern_async)

async def modern_atomic_async(self) -> AsyncGenerator[CardAtomicGroup, None]:
"""
all Card (Atomic) cards, restricted to cards legal in the Modern format.
Expand All @@ -258,7 +286,9 @@ async def modern_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
async for item in self._atomic(kind="ModernAtomic"):
yield item

async def pauper_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
modern_atomic = synchronize(modern_atomic_async)

async def pauper_atomic_async(self) -> AsyncGenerator[CardAtomicGroup, None]:
"""
all Card (Atomic) cards, restricted to cards legal in the Pauper format.
Expand All @@ -267,7 +297,9 @@ async def pauper_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
async for item in self._atomic(kind="PauperAtomic"):
yield item

async def pioneer(self) -> AsyncGenerator[Set, None]:
pauper_atomic = synchronize(pauper_atomic_async)

async def pioneer_async(self) -> AsyncGenerator[Set, None]:
"""
all Card (Set) cards organized by Set, restricted to cards legal in the
Pioneer format.
Expand All @@ -277,7 +309,9 @@ async def pioneer(self) -> AsyncGenerator[Set, None]:
async for k, item in self._iterate_model(kind="Pioneer", model=Set):
yield item

async def pioneer_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
pioneer = synchronize(pioneer_async)

async def pioneer_atomic_async(self) -> AsyncGenerator[CardAtomicGroup, None]:
"""
all Card (Atomic) cards, restricted to cards legal in the Pioneer format.
Expand All @@ -286,7 +320,9 @@ async def pioneer_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
async for item in self._atomic(kind="PioneerAtomic"):
yield item

async def set_list(self) -> AsyncGenerator[SetList, None]:
pioneer_atomic = synchronize(pioneer_atomic_async)

async def set_list_async(self) -> AsyncGenerator[SetList, None]:
"""
a list of meta data for all Set data.
Expand All @@ -297,7 +333,9 @@ async def set_list(self) -> AsyncGenerator[SetList, None]:
):
yield item

async def set(self, code: str) -> SetList:
set_list = synchronize(set_list_async)

async def set_async(self, code: str) -> SetList:
"""
Get a Set data
Expand All @@ -308,7 +346,9 @@ async def set(self, code: str) -> SetList:
"""
return await self._get_item(code, SetList)

async def standard(self) -> AsyncGenerator[Set, None]:
set = synchronize(set_async)

async def standard_async(self) -> AsyncGenerator[Set, None]:
"""
all Card (Set) cards organized by Set, restricted to cards legal in the
Standard format.
Expand All @@ -318,7 +358,9 @@ async def standard(self) -> AsyncGenerator[Set, None]:
async for k, item in self._iterate_model(kind="Standard", model=Set):
yield item

async def standard_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
standard = synchronize(standard_async)

async def standard_atomic_async(self) -> AsyncGenerator[CardAtomicGroup, None]:
"""
all Card (Atomic) cards, restricted to cards legal in the Standard format.
Expand All @@ -327,7 +369,9 @@ async def standard_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
async for item in self._atomic(kind="StandardAtomic"):
yield item

async def tcg_player_skus(self) -> AsyncGenerator[TcgPlayerSKUs, None]:
standard_atomic = synchronize(standard_atomic_async)

async def tcg_player_skus_async(self) -> AsyncGenerator[TcgPlayerSKUs, None]:
"""
TCGplayer SKU information based on card UUIDs.
Expand All @@ -347,7 +391,9 @@ async def tcg_player_skus(self) -> AsyncGenerator[TcgPlayerSKUs, None]:

yield group

async def vintage(self) -> AsyncGenerator[Set, None]:
tcg_player_skus = synchronize(tcg_player_skus_async)

async def vintage_async(self) -> AsyncGenerator[Set, None]:
"""
all Card (Set) cards organized by Set, restricted to sets legal in the
Vintage format.
Expand All @@ -357,7 +403,9 @@ async def vintage(self) -> AsyncGenerator[Set, None]:
async for k, item in self._iterate_model(kind="Vintage", model=Set):
yield item

async def vintage_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
vintage = synchronize(vintage_async)

async def vintage_atomic_async(self) -> AsyncGenerator[CardAtomicGroup, None]:
"""
all Card (Atomic) cards, restricted to sets legal in the Vintage format.
Expand All @@ -366,6 +414,8 @@ async def vintage_atomic(self) -> AsyncGenerator[CardAtomicGroup, None]:
async for item in self._atomic(kind="VintageAtomic"):
yield item

vintage_atomic = synchronize(vintage_atomic_async)

async def _atomic(self, kind: str) -> AsyncGenerator[CardAtomicGroup, None]:
group: Optional[CardAtomicGroup] = None
async for (k, i), item in self._iterate_model(
Expand Down
12 changes: 6 additions & 6 deletions src/mightstone/services/mtgjson/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,40 @@ def mtgjson(obj: MtgJsonObj, **kwargs):
@click.pass_obj
def meta(obj: MtgJsonObj):
"""Display the current version."""
pretty_print(asyncio.run(obj["client"].meta()))
pretty_print(asyncio.run(obj["client"].meta_async()))


@mtgjson.command()
@click.pass_obj
def card_types(obj: MtgJsonObj):
"""Display every card type of any type of card."""
pretty_print(asyncio.run(obj["client"].card_types()))
pretty_print(asyncio.run(obj["client"].card_types_async()))


@mtgjson.command()
@click.pass_obj
@click.argument("code", type=str)
def set(obj: MtgJsonObj, **kwargs):
"""Display every card type of any type of card."""
pretty_print(asyncio.run(obj["client"].set(**kwargs)))
pretty_print(asyncio.run(obj["client"].set_async(**kwargs)))


@mtgjson.command()
@click.pass_obj
def compiled_list(obj: MtgJsonObj):
"""Display every card type of any type of card."""
pretty_print(asyncio.run(obj["client"].compiled_list()))
pretty_print(asyncio.run(obj["client"].compiled_list_async()))


@mtgjson.command()
@click.pass_obj
def keywords(obj: MtgJsonObj):
"""Display every card type of any type of card."""
pretty_print(asyncio.run(obj["client"].keywords()))
pretty_print(asyncio.run(obj["client"].keywords_async()))


@mtgjson.command()
@click.pass_obj
def enum_values(obj: MtgJsonObj):
"""Display every card type of any type of card."""
pretty_print(asyncio.run(obj["client"].enum_values()))
pretty_print(asyncio.run(obj["client"].enum_values_async()))

0 comments on commit d407077

Please sign in to comment.