Add optional property for locations

This commit is contained in:
kramo
2023-12-20 17:33:49 +01:00
parent f8bc23991a
commit ec69361826
3 changed files with 23 additions and 7 deletions

View File

@@ -36,8 +36,9 @@ class FlatpakSourceIterable(SourceIterable):
"""Generator method producing games"""
icon_theme = Gtk.IconTheme.new()
if user_data := self.source.locations.user_data["icons"]:
icon_theme.add_search_path(str(user_data))
icon_theme.add_search_path(str(self.source.locations.system_data["icons"]))
icon_theme.add_search_path(str(self.source.locations.user_data["icons"]))
blacklist = (
{"hu.kramo.Cartridges", "hu.kramo.Cartridges.Devel"}
@@ -154,5 +155,6 @@ class FlatpakSource(ExecutableFormatSource):
"icons": LocationSubPath("exports/share/icons", True),
},
invalid_subtitle=Location.DATA_INVALID_SUBTITLE,
optional=True,
),
)

View File

@@ -16,7 +16,8 @@ class LocationSubPath(NamedTuple):
class UnresolvableLocationError(Exception):
pass
def __init__(self, optional):
self.optional = optional
class Location:
@@ -49,12 +50,14 @@ class Location:
candidates: Iterable[Candidate],
paths: Mapping[str, LocationSubPath],
invalid_subtitle: str,
optional: Optional[bool] = False,
) -> None:
super().__init__()
self.schema_key = schema_key
self.candidates = candidates
self.paths = paths
self.invalid_subtitle = invalid_subtitle
self.optional = optional
def check_candidate(self, candidate: Path) -> bool:
"""Check if a candidate root has the necessary files and directories"""
@@ -87,7 +90,7 @@ class Location:
break
else:
# No good candidate found
raise UnresolvableLocationError()
raise UnresolvableLocationError(self.optional)
# Update the schema with the found candidate
value = str(candidate)
@@ -96,7 +99,13 @@ class Location:
def __getitem__(self, key: str) -> Optional[Path]:
"""Get the computed path from its key for the location"""
self.resolve()
try:
self.resolve()
except UnresolvableLocationError as error:
if error.optional:
return None
raise UnresolvableLocationError from error
if self.root:
return self.root / self.paths[key].segment
return None

View File

@@ -23,7 +23,7 @@ from collections.abc import Iterable
from typing import Any, Collection, Generator, Optional
from cartridges.game import Game
from cartridges.importer.location import Location
from cartridges.importer.location import Location, UnresolvableLocationError
# Type of the data returned by iterating on a Source
SourceIterationResult = Optional[Game | tuple[Game, tuple[Any]]]
@@ -87,10 +87,15 @@ class Source(Iterable):
def __iter__(self) -> Generator[SourceIterationResult, None, None]:
"""
Get an iterator for the source
:raises UnresolvableLocationError: Not iterable if any of the locations are unresolvable
:raises UnresolvableLocationError: Not iterable
if any of the mandatory locations are unresolvable
"""
for location in self.locations:
location.resolve()
try:
location.resolve()
except UnresolvableLocationError as error:
if not error.optional:
raise UnresolvableLocationError from error
return iter(self.iterable_class(self))