Skip to content

Commit

Permalink
Merge pull request #32 from Jylpah/dev
Browse files Browse the repository at this point in the history
version version 1.2.4: Maps.open_yaml()
  • Loading branch information
Jylpah authored Jan 14, 2024
2 parents 60c4631 + e20f695 commit c3246ab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "blitz-models"
version = "1.2.3"
version = "1.2.4"
authors = [{ name = "Jylpah", email = "[email protected]" }]
description = "Pydantic models for Wargaming's World of Tanks Blitz game "
readme = "README.md"
Expand Down
47 changes: 28 additions & 19 deletions src/blitzmodels/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c3246ab

Please sign in to comment.