From c4ba2e8983d2a928f1900b88feeb39d2bb954b60 Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Sat, 15 Jun 2019 22:01:14 +0200 Subject: [PATCH] Add path stamper --- fimfarchive/stampers.py | 32 +++++++++++++++++-- tests/test_stampers.py | 68 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/fimfarchive/stampers.py b/fimfarchive/stampers.py index 877d1e9..f5ead18 100644 --- a/fimfarchive/stampers.py +++ b/fimfarchive/stampers.py @@ -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 diff --git a/tests/test_stampers.py b/tests/test_stampers.py index 0891491..3a77df9 100644 --- a/tests/test_stampers.py +++ b/tests/test_stampers.py @@ -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']