Skip to content

Commit

Permalink
feat: allow specifying the URL for CardXML bootstrapping from web
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Apr 17, 2024
1 parent 007da86 commit 6436cd4
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions hearthstone/cardxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ def is_functional_duplicate_of(self, other):
XML_URL = "https://api.hearthstonejson.com/v1/latest/CardDefs.xml"


def _bootstrap_from_web(parse: Callable[[Iterator[Tuple[str, Any]]], None]):
def _bootstrap_from_web(url: str, parse: Callable[[Iterator[Tuple[str, Any]]], None]):
with tempfile.TemporaryFile(mode="rb+") as fp:
if download_to_tempfile_retry(XML_URL, fp):
if download_to_tempfile_retry(url, fp):
fp.flush()
fp.seek(0)

Expand All @@ -416,7 +416,7 @@ def _bootstrap_from_library(parse: Callable[[Iterator[Tuple[str, Any]]], None],
parse(ElementTree.iterparse(f, events=("start", "end",)))


def _load(path, locale, cache, attr):
def _load(path, locale, cache, attr, url=None):
cache_key = (path, locale)
if cache_key not in cache:
db = {}
Expand Down Expand Up @@ -446,7 +446,7 @@ def parse(context: Iterator[Tuple[str, Any]]):
has_lib = False

if not has_lib:
_bootstrap_from_web(parse)
_bootstrap_from_web(url or XML_URL, parse)

if not db:
_bootstrap_from_library(parse, path=path)
Expand All @@ -456,9 +456,9 @@ def parse(context: Iterator[Tuple[str, Any]]):
return cache[cache_key]


def load(path=None, locale="enUS"):
return _load(path, locale, cardid_cache, "id")
def load(path=None, locale="enUS", url=None):
return _load(path, locale, cardid_cache, "id", url)


def load_dbf(path=None, locale="enUS"):
return _load(path, locale, dbf_cache, "dbf_id")
def load_dbf(path=None, locale="enUS", url=None):
return _load(path, locale, dbf_cache, "dbf_id", url)

0 comments on commit 6436cd4

Please sign in to comment.