Add path stamper

This commit is contained in:
Joakim Soderlund 2019-06-15 22:01:14 +02:00
parent 1a34c8112b
commit c4ba2e8983
2 changed files with 96 additions and 4 deletions

View file

@ -5,7 +5,7 @@ Stampers for Fimfarchive.
#
# Fimfarchive, preserves stories from Fimfiction.
# Copyright (C) 2015 Joakim Soderlund
# Copyright (C) 2019 Joakim Soderlund
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -22,7 +22,7 @@ Stampers for Fimfarchive.
#
from typing import Any, Dict, Set
from typing import Any, Callable, Dict, Optional, Set
import arrow
@ -106,3 +106,31 @@ class UpdateStamper(Stamper):
archive[key] = timestamp
elif key not in archive:
archive[key] = None
class PathStamper(Stamper):
"""
Adds archive paths to stories.
"""
def __init__(self, mapper: Callable[[Story], Optional[str]]) -> None:
"""
Constructor.
Args:
mapper: Callable returning the path to stamp.
"""
self.map = mapper
def __call__(self, story: Story) -> None:
archive = self.get_archive(story)
path = self.map(story)
if 'path' in archive:
del archive['path']
if 'path' in story.meta:
del story.meta['path']
if path:
archive['path'] = path

View file

@ -5,7 +5,7 @@ Stamper tests.
#
# Fimfarchive, preserves stories from Fimfiction.
# Copyright (C) 2015 Joakim Soderlund
# Copyright (C) 2019 Joakim Soderlund
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -29,7 +29,8 @@ import arrow
import pytest
from fimfarchive.flavors import UpdateStatus
from fimfarchive.stampers import Stamper, UpdateStamper
from fimfarchive.mappers import StaticMapper
from fimfarchive.stampers import Stamper, PathStamper, UpdateStamper
class TestStamper:
@ -187,3 +188,66 @@ class TestUpdateStamper:
assert archive['date_created'] == prev
assert archive['date_fetched'] == prev
assert archive['date_updated'] == prev
class TestPathStamper:
"""
PathStamper tests.
"""
@pytest.mark.parametrize('value', (None, 'Some'))
def test_cleared_alpha_path(self, story, value):
"""
Tests removal of old alpha format values.
"""
story = story.merge(meta={
**story.meta,
'path': 'path',
})
meta = story.meta
assert 'path' in meta
stamp = PathStamper(StaticMapper(value))
stamp(story)
assert 'path' not in meta
def test_cleared_beta_path(self, story):
"""
Tests removal of old beta format values.
"""
story = story.merge(meta={
**story.meta,
'archive': {
'path': 'path',
},
})
archive = story.meta['archive']
assert 'path' in archive
stamp = PathStamper(StaticMapper(None))
stamp(story)
assert 'path' not in archive
@pytest.mark.parametrize('value', (None, ''))
def test_ignored_blank_value(self, story, value):
"""
Tests blank values are ignored.
"""
stamp = PathStamper(StaticMapper(value))
stamp(story)
assert 'path' not in story.meta['archive']
@pytest.mark.parametrize('value', ('one', 'two'))
def test_stamped_value(self, story, value):
"""
Tests values are stamped to story meta.
"""
stamp = PathStamper(StaticMapper(value))
stamp(story)
assert value == story.meta['archive']['path']