Make FimfarchiveFetcher iterable

This commit is contained in:
Joakim Soderlund 2018-02-14 09:47:53 +01:00
parent cb58cfaa53
commit b916699127

View file

@ -31,6 +31,7 @@ from jmespath import compile as jmes
from fimfarchive.exceptions import InvalidStoryError, StorySourceError from fimfarchive.exceptions import InvalidStoryError, StorySourceError
from fimfarchive.flavors import StorySource, DataFormat, MetaPurity from fimfarchive.flavors import StorySource, DataFormat, MetaPurity
from fimfarchive.stories import Story
from fimfarchive.utils import Empty from fimfarchive.utils import Empty
from .base import Fetcher from .base import Fetcher
@ -140,6 +141,19 @@ class FimfarchiveFetcher(Fetcher):
except ValueError as e: except ValueError as e:
raise StorySourceError(f"Malformed index key: {key}") from e raise StorySourceError(f"Malformed index key: {key}") from e
def __len__(self) -> int:
"""
Returns the total number of stories in the archive.
"""
return len(self.index)
def __iter__(self) -> Iterable[Story]:
"""
Yields all stories in the archive, ordered by ID.
"""
for key in sorted(self.index.keys()):
yield self.fetch(key)
def validate(self, key: int) -> int: def validate(self, key: int) -> int:
""" """
Ensures that the key matches a valid story Ensures that the key matches a valid story