Skip to content

Commit

Permalink
feat: card-conjurer services applies the new method() / method_async(…
Browse files Browse the repository at this point in the history
…) dichotomy
  • Loading branch information
Guibod committed Mar 6, 2023
1 parent 6e0e165 commit e7dfb06
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
15 changes: 11 additions & 4 deletions src/mightstone/services/cardconjurer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pydantic.error_wrappers import ValidationError

from mightstone import logger
from mightstone.ass import synchronize
from mightstone.services import MightstoneHttpClient, ServiceError
from mightstone.services.cardconjurer.models import (
Card,
Expand Down Expand Up @@ -68,7 +69,7 @@ def clear(self):
self.assets_fonts = defaultdict(lambda: default_font)
self.assets_images = {}

async def template(self, url_or_path) -> Template:
async def template_async(self, url_or_path) -> Template:
"""
Open a ``Template``, local or through HTTP
Expand All @@ -79,7 +80,9 @@ async def template(self, url_or_path) -> Template:
return await self._url(Template, url_or_path)
return await self._file(Template, url_or_path)

async def card(self, url_or_path) -> Card:
template = synchronize(template_async)

async def card_async(self, url_or_path) -> Card:
"""
Open a ``Card``, local or through HTTP
Expand All @@ -90,7 +93,9 @@ async def card(self, url_or_path) -> Card:
return await self._url(Card, url_or_path)
return await self._file(Card, url_or_path)

async def render(self, card: Card, output=None) -> PIL.Image.Image:
card = synchronize(card_async)

async def render_async(self, card: Card, output=None) -> PIL.Image.Image:
"""
Render a card object into a PIL Image
Expand All @@ -106,7 +111,7 @@ async def render(self, card: Card, output=None) -> PIL.Image.Image:
if card.dependencies.template.url:
template_path = card.asset_root_url + "/" + card.dependencies.template.url
try:
template = await self.template(template_path)
template = await self.template_async(template_path)
except FileNotFoundError:
raise ServiceError(
f"Unable to find parent template for {card.name},"
Expand Down Expand Up @@ -175,6 +180,8 @@ async def render(self, card: Card, output=None) -> PIL.Image.Image:

return image

render = synchronize(render_async)

async def _file(self, model: Generic[T], path: str) -> T:
"""
Reads a Card Conjurer model from a local file using asyncio
Expand Down
4 changes: 2 additions & 2 deletions src/mightstone/services/cardconjurer/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def cardconjurer(obj: CardConjurerObj, **kwargs):
@click.pass_obj
@click.argument("url_or_path")
def card(obj: CardConjurerObj, **kwargs):
pretty_print(asyncio_run(obj["client"].card(**kwargs)), obj.get("format"))
pretty_print(obj["client"].card(**kwargs), obj.get("format"))


@cardconjurer.command()
@click.pass_obj
@click.argument("url_or_path")
def template(obj: CardConjurerObj, **kwargs):
pretty_print(asyncio_run(obj["client"].template(**kwargs)), obj.get("format"))
pretty_print(obj["client"].template(**kwargs), obj.get("format"))


@cardconjurer.command()
Expand Down
2 changes: 1 addition & 1 deletion tests/mightstone/services/cardconjurer/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TestCardConjurer(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
self.cc = CardConjurer()
path = Path(os.path.dirname(__file__)).joinpath("Dimirova Smiley.json")
self.c = await self.cc.card(str(path))
self.c = await self.cc.card_async(str(path))

def test_dimirova_smiley_is_valid(self):
self.assertEqual(self.c.name, "Dimirova Smiley")
Expand Down
10 changes: 5 additions & 5 deletions tests/mightstone/services/cardconjurer/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TestCardConjurerRemote(unittest.IsolatedAsyncioTestCase):
@pytest.mark.skip_remote_api
async def test_angular_remote_is_valid(self):
m = CardConjurer()
template = await m.template(
template = await m.template_async(
"https://card-conjurer-assets.s3.us-east-1"
".amazonaws.com/custom/11-20-22/template.json"
)
Expand All @@ -39,7 +39,7 @@ async def test_angular_remote_is_valid(self):
@pytest.mark.skip_remote_api
async def test_simple_token_remote_is_valid(self):
m = CardConjurer()
template = await m.template(
template = await m.template_async(
"https://card-conjurer-assets.s3.us-east-1"
".amazonaws.com/custom/12-8-22/template.json"
)
Expand All @@ -60,7 +60,7 @@ async def test_simple_token_remote_is_valid(self):
@pytest.mark.skip_remote_api
async def test_tall_archaic_remote_is_valid(self):
m = CardConjurer()
template = await m.template(
template = await m.template_async(
"https://card-conjurer-assets.s3.us-east-1"
".amazonaws.com/custom/12-18-22/template.json"
)
Expand All @@ -83,13 +83,13 @@ async def test_dimirova_smiley(self) -> None:

cc = CardConjurer()
path = Path(os.path.dirname(__file__)).joinpath("Dimirova Smiley.json")
card = await cc.card(str(path))
card = await cc.card_async(str(path))
card.asset_root_url = "https://card-conjurer-assets.s3.us-east-1.amazonaws.com"

original = Image.open(
Path(os.path.dirname(__file__)).joinpath("Dimirova Smiley.png")
)
image = await cc.render(card)
image = await cc.render_async(card)
diff = Image.new("RGBA", original.size)

differing_pixels = pixelmatch(
Expand Down

0 comments on commit e7dfb06

Please sign in to comment.