Skip to content

Commit

Permalink
Merge pull request #40 from Jylpah/dev
Browse files Browse the repository at this point in the history
version 1.2.11: default_path() method for Maps and WGApiWoTBlitzTankopedia to access the packaged maps/tankopedia
  • Loading branch information
Jylpah authored Jan 21, 2024
2 parents 6053b1e + 2ffa087 commit a77d45f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 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.10"
version = "1.2.11"
authors = [{ name = "Jylpah", email = "[email protected]" }]
description = "Pydantic models for Wargaming's World of Tanks Blitz game "
readme = "README.md"
Expand Down
13 changes: 10 additions & 3 deletions src/blitzmodels/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,22 @@ def load_yaml(cls, yaml_doc: str) -> Self | None:
return None

@classmethod
async def open_default(cls) -> Optional[Self]:
def default_path(cls) -> Path:
"""
Open maps shipped with the package
Return Path of the Tankopedia shipped with the package
"""
packaged_maps: Traversable = importlib.resources.files("blitzmodels").joinpath(
"maps.json"
) # REFACTOR in Python 3.12
with as_file(packaged_maps) as maps_fn:
return await cls.open_json(maps_fn)
return maps_fn

@classmethod
async def open_default(cls) -> Optional[Self]:
"""
Open maps shipped with the package
"""
return await cls.open_json(cls.default_path())

def get_by_key(self, key: str) -> Map | None:
"""A brute-force map search by key"""
Expand Down
14 changes: 11 additions & 3 deletions src/blitzmodels/wg_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
field_serializer,
Field,
)
from pathlib import Path
from importlib.resources.abc import Traversable
from importlib.resources import as_file
import importlib
Expand Down Expand Up @@ -710,15 +711,22 @@ def _validate_code(self) -> Self:
return self

@classmethod
async def open_default(cls) -> Optional[Self]:
def default_path(cls) -> Path:
"""
Open Tankopedia shipped with the package
Return Path of the Tankopedia shipped with the package
"""
packaged_tankopedia: Traversable = importlib.resources.files(
"blitzmodels"
).joinpath("tanks.json") # REFACTOR in Python 3.12
with as_file(packaged_tankopedia) as tankopedia_fn:
return await cls.open_json(tankopedia_fn)
return tankopedia_fn

@classmethod
async def open_default(cls) -> Optional[Self]:
"""
Open Tankopedia shipped with the package
"""
return await cls.open_json(cls.default_path())

def __len__(self) -> int:
return len(self.data)
Expand Down

0 comments on commit a77d45f

Please sign in to comment.