From c9eadd0c92166ae07e16f06afdf20b9d68a06846 Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Sat, 4 Nov 2017 14:12:26 +0100 Subject: [PATCH] Add stampers module --- fimfarchive/stampers.py | 59 +++++++++++++++++++++++++++++++++++++ tests/test_stampers.py | 64 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 fimfarchive/stampers.py create mode 100644 tests/test_stampers.py diff --git a/fimfarchive/stampers.py b/fimfarchive/stampers.py new file mode 100644 index 0000000..62bd7a6 --- /dev/null +++ b/fimfarchive/stampers.py @@ -0,0 +1,59 @@ +""" +Stampers for Fimfarchive. +""" + + +# +# Fimfarchive, preserves stories from Fimfiction. +# Copyright (C) 2015 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 +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + + +from typing import Any, Dict + +from fimfarchive.stories import Story + + +class Stamper: + """ + Adds archive-related information to stories. + """ + + def get_archive(self, story: Story) -> Dict[str, Any]: + """ + Finds or creates an archive dict. + + Args: + story: The story to stamp. + + Returns: + An archive dict for the story. + """ + meta = story.meta + + if 'archive' not in meta: + meta['archive'] = dict() + + return meta['archive'] + + def __call__(self, story: Story) -> None: + """ + Applies the stamp to the story. + + Args: + story: The story to stamp. + """ + raise NotImplementedError() diff --git a/tests/test_stampers.py b/tests/test_stampers.py new file mode 100644 index 0000000..f5d67df --- /dev/null +++ b/tests/test_stampers.py @@ -0,0 +1,64 @@ +""" +Stamper tests. +""" + + +# +# Fimfarchive, preserves stories from Fimfiction. +# Copyright (C) 2015 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 +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + + +from typing import Dict + +import pytest + +from fimfarchive.stampers import Stamper + + +class TestStamper: + """ + Stamper tests. + """ + + @pytest.fixture + def stamper(self): + """ + Returns a new stamper instance. + """ + return Stamper() + + def test_missing_archive_dict(self, stamper, story): + """ + Tests archive dict is created if none exists. + """ + meta = story.meta + assert 'archive' not in meta + + archive = stamper.get_archive(story) + assert meta['archive'] is archive + + def test_existing_archive_dict(self, stamper, story): + """ + Tests archive dict is kept if it exists. + """ + meta = story.meta + original: Dict = dict() + meta['archive'] = original + archive = stamper.get_archive(story) + + assert archive is original + assert meta['archive'] is original