mirror of
https://github.com/JockeTF/fimfarchive.git
synced 2024-11-21 21:07:59 +01:00
Limit mappers to single story as parameter
This commit is contained in:
parent
ab2f2f3f88
commit
4a2956c3c3
2 changed files with 30 additions and 36 deletions
|
@ -23,10 +23,13 @@ Mappers for Fimfarchive.
|
|||
|
||||
|
||||
import os
|
||||
from abc import abstractmethod
|
||||
from typing import Generic, Optional, TypeVar
|
||||
|
||||
import arrow
|
||||
from arrow import api as arrow, Arrow
|
||||
|
||||
from fimfarchive.exceptions import InvalidStoryError
|
||||
from fimfarchive.stories import Story
|
||||
|
||||
|
||||
__all__ = (
|
||||
|
@ -37,33 +40,45 @@ __all__ = (
|
|||
)
|
||||
|
||||
|
||||
class Mapper:
|
||||
T = TypeVar('T')
|
||||
|
||||
|
||||
class Mapper(Generic[T]):
|
||||
"""
|
||||
Callable which maps something to something else.
|
||||
Callable which maps stories to something else.
|
||||
"""
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
raise NotImplementedError()
|
||||
@abstractmethod
|
||||
def __call__(self, story: Story) -> T:
|
||||
"""
|
||||
Applies the mapper.
|
||||
|
||||
Args:
|
||||
story: The story to map.
|
||||
|
||||
Returns:
|
||||
A mapped object.
|
||||
"""
|
||||
|
||||
|
||||
class StaticMapper(Mapper):
|
||||
class StaticMapper(Mapper[T]):
|
||||
"""
|
||||
Returns the supplied value for any call.
|
||||
"""
|
||||
|
||||
def __init__(self, value=None):
|
||||
def __init__(self, value: T) -> None:
|
||||
self.value = value
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
def __call__(self, story: Story) -> T:
|
||||
return self.value
|
||||
|
||||
|
||||
class StoryDateMapper(Mapper):
|
||||
class StoryDateMapper(Mapper[Optional[Arrow]]):
|
||||
"""
|
||||
Returns the latest timestamp in a story, or None.
|
||||
"""
|
||||
|
||||
def __call__(self, story):
|
||||
def __call__(self, story: Story) -> Optional[Arrow]:
|
||||
try:
|
||||
meta = getattr(story, 'meta', None)
|
||||
except InvalidStoryError:
|
||||
|
@ -87,15 +102,15 @@ class StoryDateMapper(Mapper):
|
|||
return None
|
||||
|
||||
|
||||
class StoryPathMapper(Mapper):
|
||||
class StoryPathMapper(Mapper[str]):
|
||||
"""
|
||||
Returns a key-based file path for a story.
|
||||
"""
|
||||
|
||||
def __init__(self, directory):
|
||||
def __init__(self, directory: str) -> None:
|
||||
self.directory = directory
|
||||
|
||||
def __call__(self, story):
|
||||
def __call__(self, story: Story) -> str:
|
||||
directory = str(self.directory)
|
||||
key = str(story.key)
|
||||
|
||||
|
|
|
@ -48,33 +48,12 @@ class TestStaticMapper:
|
|||
"""
|
||||
return object()
|
||||
|
||||
def test_value(self, value):
|
||||
def test_value(self, story, value):
|
||||
"""
|
||||
Tests returns the supplied value.
|
||||
"""
|
||||
mapper = StaticMapper(value)
|
||||
assert mapper() is value
|
||||
|
||||
def test_default_value(self):
|
||||
"""
|
||||
Tests `None` is returned by default.
|
||||
"""
|
||||
mapper = StaticMapper()
|
||||
assert mapper() is None
|
||||
|
||||
def test_args(self, value):
|
||||
"""
|
||||
Tests callable ignores args.
|
||||
"""
|
||||
mapper = StaticMapper(value)
|
||||
assert mapper(1, 2, 3) is value
|
||||
|
||||
def test_kwargs(self, value):
|
||||
"""
|
||||
Tests callable ignores kwargs.
|
||||
"""
|
||||
mapper = StaticMapper(value)
|
||||
assert mapper(a=1, b=2) is value
|
||||
assert mapper(story) is value
|
||||
|
||||
|
||||
class TestStoryDateMapper:
|
||||
|
|
Loading…
Reference in a new issue