From 29d7cce3d1d53a7f78a48ef7a2c1a83785b6faa2 Mon Sep 17 00:00:00 2001 From: Jylpah Date: Sun, 14 Jan 2024 23:36:58 +0200 Subject: [PATCH 1/2] add Maps.load_yaml() --- src/blitzmodels/map.py | 47 +++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/blitzmodels/map.py b/src/blitzmodels/map.py index 14106a2..019592d 100644 --- a/src/blitzmodels/map.py +++ b/src/blitzmodels/map.py @@ -127,31 +127,40 @@ async def open_yaml( Read Maps from Blitz game maps.yaml file """ try: - maps: Self = cls() async with open(file=filename, mode="r", encoding="utf-8") as file: debug(f"yaml file opened: {str(filename)}") - maps_yaml = safe_load(await file.read()) - for key, map_cfg in maps_yaml["maps"].items(): - try: - map_id: int = map_cfg["id"] - modes: List[int] = map_cfg["availableModes"] - localization_code: str = map_cfg["localName"] - maps.add( - Map( - id=map_id, - key=key, - modes=modes, - localization_code=localization_code, - ) + return cls.load_yaml(await file.read()) + except OSError as err: + debug(f"Error reading file: {filename}: {err}") + return None + + @classmethod + def load_yaml(cls, yaml_doc: str) -> Self | None: + """ + Read Maps from Blitz game maps.yaml input + """ + try: + maps: Self = cls() + maps_yaml = safe_load(yaml_doc) + for key, map_cfg in maps_yaml["maps"].items(): + try: + map_id: int = map_cfg["id"] + modes: List[int] = map_cfg["availableModes"] + localization_code: str = map_cfg["localName"] + maps.add( + Map( + id=map_id, + key=key, + modes=modes, + localization_code=localization_code, ) - except KeyError as err: - error(f"could not read map config for map_key={key}: {err}") + ) + except KeyError as err: + error(f"could not read map config for map_key={key}: {err}") if len(maps) > 0: return maps except KeyError: - error(f"no YAML root key 'maps' found in {str(filename)}") - except OSError as err: - debug(f"Error reading file: {filename}: {err}") + error("no YAML root key 'maps' found in input") return None def get_by_key(self, key: str) -> Map | None: From e20f695baadbbb074176f8147c89ff08e1fb9359 Mon Sep 17 00:00:00 2001 From: Jylpah Date: Sun, 14 Jan 2024 23:37:04 +0200 Subject: [PATCH 2/2] version 1.2.4 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 82d7e5d..75843ce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "blitz-models" -version = "1.2.3" +version = "1.2.4" authors = [{ name = "Jylpah", email = "jylpah@gmail.com" }] description = "Pydantic models for Wargaming's World of Tanks Blitz game " readme = "README.md"