Skip to content

Commit

Permalink
feat: moved rules to the wotc service, introducing the first api with…
Browse files Browse the repository at this point in the history
… both sync/async functions
  • Loading branch information
Guibod committed Mar 6, 2023
1 parent 1acd5a7 commit 8ceb49d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions docs/source/reference/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ Wizards Of The Coast
RuleExplorer Client
~~~~~~~~~~~~~~~~~~~

.. autoclass:: mightstone.services.rules.RuleExplorer
.. autoclass:: mightstone.services.wotc.RuleExplorer
:members:

Models
~~~~~~

.. currentmodule:: mightstone.services.rules.models
.. currentmodule:: mightstone.services.wotc.models

.. autosummary::
:toctree: _autosummary
Expand Down
10 changes: 6 additions & 4 deletions examples/comprehensive_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

from pydantic.json import pydantic_encoder

from mightstone.services.wotc.models import RuleRef, RuleText
from mightstone.services.wotc import RuleExplorer
from mightstone.services.wotc.models import RuleRef, RuleText

explorer = RuleExplorer()
before_errata = explorer.open(
"https://media.wizards.com/2020/downloads/MagicCompRules%2020200417.txt"
)

errata_companion = asyncio.run(explorer.open_async(
"https://media.wizards.com/2020/downloads/MagicCompRules%2020200601.txt"
))
errata_companion = asyncio.run(
explorer.open_async(
"https://media.wizards.com/2020/downloads/MagicCompRules%2020200601.txt"
)
)
diff = before_errata.diff(errata_companion)
print(json.dumps(diff, default=pydantic_encoder, indent=4))

Expand Down
13 changes: 7 additions & 6 deletions src/mightstone/ass/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import asyncio
from functools import wraps

import logging
from functools import wraps
from typing import Any, AsyncGenerator, Callable, List, TypeVar

from typing import Any, AsyncGenerator, List, TypeVar, Callable
from asgiref.sync import async_to_sync
import nest_asyncio
from aiostream import pipe, stream
from asgiref.sync import async_to_sync

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -65,7 +64,9 @@ def inner(*args, **kwargs) -> R:
if docstring:
inner.__doc__ = docstring
else:
inner.__doc__ = f"Sync version of :func:`~{qname}`, same behavior but " \
f"wrapped by :func:`~asgiref.sync.async_to_sync`."
inner.__doc__ = (
f"Sync version of :func:`~{qname}`, same behavior but "
"wrapped by :func:`~asgiref.sync.async_to_sync`."
)

return inner
6 changes: 3 additions & 3 deletions src/mightstone/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __str__(self):


class MightstoneHttpClient:
_base_url = None
base_url = None
"""
Base url of the service (must be a root path such as https://example.com)
"""
Expand All @@ -42,8 +42,8 @@ def client(self):
"transport": self.transport,
"headers": {"cache-control": f"max-age={60 * 60 * 24}"},
}
if self._base_url:
options["base_url"] = self._base_url
if self.base_url:
options["base_url"] = self.base_url

return AsyncClient(**options)

Expand Down
10 changes: 6 additions & 4 deletions src/mightstone/services/wotc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

from mightstone import logger
from mightstone.ass import synchronize
from mightstone.services.wotc.models import ComprehensiveRules
from mightstone.services import MightstoneHttpClient
from mightstone.services.wotc.models import ComprehensiveRules


class RuleExplorer(MightstoneHttpClient):
_base_url = "https://media.wizards.com"
base_url = "https://media.wizards.com"

async def open_async(self, path: str = None) -> ComprehensiveRules:
"""
Expand Down Expand Up @@ -45,7 +45,7 @@ async def latest_async(self) -> str:
:return: The url of the latest ruleset to date
"""
pattern = re.compile(re.escape(self._base_url) + r"/.*/MagicComp.+\.txt")
pattern = re.compile(re.escape(self.base_url) + r"/.*/MagicComp.+\.txt")

f = await self.client.get("https://magic.wizards.com/en/rules")
f.raise_for_status()
Expand All @@ -56,7 +56,9 @@ async def latest_async(self) -> str:

latest = synchronize(latest_async)

async def explore_async(self, after: date, before: date = None, concurrency=3) -> List[str]:
async def explore_async(
self, after: date, before: date = None, concurrency=3
) -> List[str]:
"""
Explore the wizards of the coast website to find any rule between two timestamp.
Expand Down
2 changes: 0 additions & 2 deletions src/mightstone/services/wotc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,3 @@ def diff(self, cr: "ComprehensiveRules"):
}

return diff


0 comments on commit 8ceb49d

Please sign in to comment.