Skip to content

Commit

Permalink
Fixed AssetsPath find_asset method and initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCheese42 committed May 25, 2024
1 parent 46fc2d9 commit 2a476ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 7 additions & 5 deletions cme/resource_/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class AssetsPath(type(Path())): # type: ignore
"""
Provides the find_asset method to recursively retrive the given asset.
Provides the find_asset method to recursively retrieve the given asset.
Subclass or instantiate this to create a hierarchie of assets folders
(e.g. `ImagesPath`, `SoundsPath`, ...).
Expand All @@ -32,23 +32,25 @@ def __init__(self, *args: Any) -> None:
def find_asset(
self,
asset: str,
preferences: Optional[tuple[str]] = (".png", ".svg"),
preferences: Optional[list[str]] = None,
) -> Path:
"""
Returns first occurrence of the provided filename.
`assets` parameter may use glob syntax.
`preferences` should be a tuple containing preferred extensions. Defaults
to `.png` and `.svg`. If None, the first match will be picked.
`preferences` should be a tuple containing preferred extensions.
Defaults to `.png` and `.svg`. If None, the first match will be picked.
"""
if preferences is None:
preferences = [".png", ".svg"]

for item in self.rglob(asset):
if not preferences or item.suffix in preferences:
return Path(item)
else:
if preferences:
# Nothing found matching preferences, trying again without any
return self.find_asset(asset, preferences=None)
return self.find_asset(asset, preferences=[])

if "." not in asset:
# .find_asset("filename") without ext should also be allowed
Expand Down
8 changes: 5 additions & 3 deletions tests/test_resource_/test_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

import pytest

from cme import resource_


@pytest.fixture
def temp_assetspath(initialize_cme: None) -> Generator[Path, None, None]:
from cme import resource_
def temp_assetspath(initialize_cme: None) -> Generator[
resource_.AssetsPath, None, None
]:
with tempfile.TemporaryDirectory() as tempdir:
resource_.set_assets_path(tempdir)
yield resource_.get_assets_path() # type: ignore
Expand All @@ -16,7 +19,6 @@ def temp_assetspath(initialize_cme: None) -> Generator[Path, None, None]:
def test_set_assets_path_constant(
initialize_cme: None, temp_assetspath: Path
) -> None:
from cme import resource_
assets_path = resource_.get_assets_path()
assert isinstance(assets_path, resource_.AssetsPath)
assert str(assets_path) == str(temp_assetspath)
Expand Down

0 comments on commit 2a476ef

Please sign in to comment.