Prioritize existing flavor in meta format mapper

This commit is contained in:
Joakim Soderlund 2017-11-11 00:06:05 +01:00
parent 4ee0824230
commit b590c60c3b
2 changed files with 15 additions and 1 deletions

View file

@ -31,6 +31,7 @@ from arrow import api as arrow, Arrow
from fimfarchive.exceptions import InvalidStoryError
from fimfarchive.flavors import MetaFormat
from fimfarchive.stories import Story
from fimfarchive.utils import find_flavor
__all__ = (
@ -128,9 +129,13 @@ class MetaFormatMapper(Mapper[Optional[MetaFormat]]):
}
def __call__(self, story: Story) -> Optional[MetaFormat]:
flavor = find_flavor(story, MetaFormat)
if flavor:
return flavor
items = self.spec.items()
meta = set(story.meta.keys())
matches = {fmt for fmt, spec in items if spec & meta}
if len(matches) == 1:

View file

@ -334,3 +334,12 @@ class TestMetaFormatMapper:
"""
story = self.merge(story, alpha, beta)
assert mapper(story) is None
def test_existing_flavor(self, mapper, story, beta):
"""
Tests existing flavor takes precedence over meta.
"""
story = story.merge(flavors=[MetaFormat.ALPHA])
story = self.merge(story, beta, 'misc')
assert mapper(story) is MetaFormat.ALPHA