From 40dccb76c0c4e005dbaf233b0038c2a0001c288b Mon Sep 17 00:00:00 2001 From: Jylpah Date: Sun, 21 Jan 2024 17:47:10 +0200 Subject: [PATCH 1/2] make open_default() non-async --- src/blitzmodels/map.py | 11 ++++++----- src/blitzmodels/wg_api.py | 5 +++-- tests/test_map.py | 7 ++----- tests/test_tank.py | 5 ++--- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/blitzmodels/map.py b/src/blitzmodels/map.py index f78e9a0..eb0fe58 100644 --- a/src/blitzmodels/map.py +++ b/src/blitzmodels/map.py @@ -10,7 +10,7 @@ Field, # ValidationError, ) -from aiofiles import open +import aiofiles from pathlib import Path from re import Pattern, compile, Match from yaml import safe_load # type: ignore @@ -130,7 +130,7 @@ async def open_yaml( Read Maps from Blitz game maps.yaml file """ try: - async with open(file=filename, mode="r", encoding="utf-8") as file: + async with aiofiles.open(file=filename, mode="r", encoding="utf-8") as file: debug(f"yaml file opened: {str(filename)}") return cls.load_yaml(await file.read()) except OSError as err: @@ -178,11 +178,12 @@ def default_path(cls) -> Path: return maps_fn @classmethod - async def open_default(cls) -> Optional[Self]: + def open_default(cls) -> Optional[Self]: """ Open maps shipped with the package """ - return await cls.open_json(cls.default_path()) + with open(cls.default_path(), "r", encoding="utf-8") as file: + return cls.parse_str(file.read()) def get_by_key(self, key: str) -> Map | None: """A brute-force map search by key""" @@ -226,7 +227,7 @@ def add_names(self, localization_strs: Dict[str, str]) -> int: # warn("legacy JSON format is depreciated", category=DeprecationWarning) # try: # res = cls() - # async with open(filename, "r", encoding="utf8") as f: + # async with aiofiles.open(filename, "r", encoding="utf8") as f: # objs: dict[str, Any] = json.loads(await f.read()) # for key, obj in objs.items(): # try: diff --git a/src/blitzmodels/wg_api.py b/src/blitzmodels/wg_api.py index ede4e3e..97fa358 100644 --- a/src/blitzmodels/wg_api.py +++ b/src/blitzmodels/wg_api.py @@ -722,11 +722,12 @@ def default_path(cls) -> Path: return tankopedia_fn @classmethod - async def open_default(cls) -> Optional[Self]: + def open_default(cls) -> Optional[Self]: """ Open Tankopedia shipped with the package """ - return await cls.open_json(cls.default_path()) + with open(cls.default_path(), "r", encoding="utf-8") as file: + return cls.parse_str(file.read()) def __len__(self) -> int: return len(self.data) diff --git a/tests/test_map.py b/tests/test_map.py index 7abc088..58de724 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -252,10 +252,7 @@ async def test_4_open_yaml( ), f"incorrect number of names updated: {updated} != {maps_names_updated}" -@pytest.mark.asyncio -async def test_5_Maps_default() -> None: - assert ( - maps := await Maps.open_default() - ) is not None, "could not open packaged maps" +def test_5_Maps_default() -> None: + assert (maps := Maps.open_default()) is not None, "could not open packaged maps" assert len(maps) > 0, "packaged maps file is empty" assert len(maps) > 30, "packaged maps file does not have enough maps" diff --git a/tests/test_tank.py b/tests/test_tank.py index 6329bea..292dbf7 100644 --- a/tests/test_tank.py +++ b/tests/test_tank.py @@ -378,10 +378,9 @@ async def test_12_WGApiTankopedia_sorted( ), f"tankopedia does not keep tanks sorted: {type(tankopedia.data)}, {type(tankopedia_shuffled.data)}" -@pytest.mark.asyncio -async def test_13_WGApiTankopedia_default() -> None: +def test_13_WGApiTankopedia_default() -> None: assert ( - tankopedia := await WGApiWoTBlitzTankopedia.open_default() + tankopedia := WGApiWoTBlitzTankopedia.open_default() ) is not None, "could not open packaged tankopedia" assert len(tankopedia) > 0, "packaged tankopedia is empty" assert len(tankopedia) > 500, "packaged tankopedia does not have enough tanks" From ca91e97fdd9abdb4d5232ff085c1bc78b2d159c8 Mon Sep 17 00:00:00 2001 From: Jylpah Date: Sun, 21 Jan 2024 17:47:26 +0200 Subject: [PATCH 2/2] version 1.2.12 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 433aaca..f2a0fd1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "blitz-models" -version = "1.2.11" +version = "1.2.12" authors = [{ name = "Jylpah", email = "jylpah@gmail.com" }] description = "Pydantic models for Wargaming's World of Tanks Blitz game " readme = "README.md"