mirror of
https://github.com/JockeTF/fimfarchive.git
synced 2024-11-25 14:37:59 +01:00
Change find flavors method into utility function
This commit is contained in:
parent
c604c4b2ad
commit
478b2e7080
4 changed files with 63 additions and 26 deletions
|
@ -22,12 +22,13 @@ Stampers for Fimfarchive.
|
|||
#
|
||||
|
||||
|
||||
from typing import Any, Dict, Optional, Set, Type, TypeVar
|
||||
from typing import Any, Dict, Set
|
||||
|
||||
import arrow
|
||||
|
||||
from fimfarchive.flavors import Flavor, UpdateStatus
|
||||
from fimfarchive.flavors import UpdateStatus
|
||||
from fimfarchive.stories import Story
|
||||
from fimfarchive.utils import find_flavor
|
||||
|
||||
|
||||
__all__ = (
|
||||
|
@ -36,9 +37,6 @@ __all__ = (
|
|||
)
|
||||
|
||||
|
||||
F = TypeVar('F', bound=Flavor)
|
||||
|
||||
|
||||
class Stamper:
|
||||
"""
|
||||
Adds archive-related information to stories.
|
||||
|
@ -90,23 +88,6 @@ class UpdateStamper(Stamper):
|
|||
},
|
||||
}
|
||||
|
||||
def find_flavor(self, story: Story, flavor: Type[F]) -> Optional[F]:
|
||||
"""
|
||||
Searches for a flavor of a specific type.
|
||||
|
||||
Args:
|
||||
story: The story to search in.
|
||||
flavor: The flavor type to find.
|
||||
|
||||
Returns:
|
||||
A flavor of the desired type, or None.
|
||||
"""
|
||||
for current in story.flavors:
|
||||
if isinstance(current, flavor):
|
||||
return current
|
||||
|
||||
return None
|
||||
|
||||
def __call__(self, story: Story) -> None:
|
||||
"""
|
||||
Applies modification dates to a story.
|
||||
|
@ -115,7 +96,7 @@ class UpdateStamper(Stamper):
|
|||
story: The story to stamp.
|
||||
"""
|
||||
timestamp = arrow.utcnow().isoformat()
|
||||
flavor = self.find_flavor(story, UpdateStatus)
|
||||
flavor = find_flavor(story, UpdateStatus)
|
||||
archive = self.get_archive(story)
|
||||
|
||||
archive['date_checked'] = timestamp
|
||||
|
|
|
@ -25,15 +25,22 @@ Various utilities.
|
|||
import json
|
||||
import os
|
||||
import shutil
|
||||
from typing import Dict, Any
|
||||
from typing import Any, Dict, Optional, Type, TypeVar
|
||||
|
||||
from fimfarchive.flavors import Flavor
|
||||
from fimfarchive.stories import Story
|
||||
|
||||
|
||||
__all__ = (
|
||||
'Empty',
|
||||
'PersistedDict',
|
||||
'find_flavor',
|
||||
)
|
||||
|
||||
|
||||
F = TypeVar('F', bound=Flavor)
|
||||
|
||||
|
||||
class EmptyMeta(type):
|
||||
"""
|
||||
Meta-class for Empty.
|
||||
|
@ -101,3 +108,21 @@ class PersistedDict(Dict[str, Any]):
|
|||
|
||||
if os.path.exists(self.temp):
|
||||
os.remove(self.temp)
|
||||
|
||||
|
||||
def find_flavor(story: Story, flavor: Type[F]) -> Optional[F]:
|
||||
"""
|
||||
Searches for a flavor of a specific type.
|
||||
|
||||
Args:
|
||||
story: The story to search in.
|
||||
flavor: The flavor type to find.
|
||||
|
||||
Returns:
|
||||
A flavor of the desired type, or None.
|
||||
"""
|
||||
for current in story.flavors:
|
||||
if isinstance(current, flavor):
|
||||
return current
|
||||
|
||||
return None
|
||||
|
|
|
@ -63,8 +63,8 @@ class TestStamper:
|
|||
meta['archive'] = original
|
||||
archive = stamper.get_archive(story)
|
||||
|
||||
assert archive is original
|
||||
assert meta['archive'] is original
|
||||
assert archive is original
|
||||
|
||||
|
||||
class TestUpdateStamper:
|
||||
|
|
|
@ -27,7 +27,8 @@ import os
|
|||
|
||||
import pytest
|
||||
|
||||
from fimfarchive.utils import Empty, PersistedDict
|
||||
from fimfarchive.flavors import DataFormat, MetaFormat, MetaPurity
|
||||
from fimfarchive.utils import find_flavor, Empty, PersistedDict
|
||||
|
||||
|
||||
class TestEmpty:
|
||||
|
@ -189,3 +190,33 @@ class TestPersistedDict:
|
|||
data = PersistedDict(tmpfile, default=extra)
|
||||
|
||||
assert dict(data) == sample
|
||||
|
||||
|
||||
class TestFindFlavor:
|
||||
"""
|
||||
find_flavor tests.
|
||||
"""
|
||||
|
||||
@pytest.fixture
|
||||
def story(self, story):
|
||||
"""
|
||||
Returns a meta-flavored story.
|
||||
"""
|
||||
return story.merge(flavors=[
|
||||
MetaFormat.BETA,
|
||||
MetaPurity.CLEAN,
|
||||
])
|
||||
|
||||
def test_present_flavor(self, story):
|
||||
"""
|
||||
Tests flavor is returned when present.
|
||||
"""
|
||||
found = find_flavor(story, MetaFormat)
|
||||
assert found == MetaFormat.BETA
|
||||
|
||||
def test_missing_flavor(self, story):
|
||||
"""
|
||||
Tests None is returned when flavor is missing.
|
||||
"""
|
||||
found = find_flavor(story, DataFormat)
|
||||
assert found is None
|
||||
|
|
Loading…
Reference in a new issue