From 1e91a64a362050f17fdc9c7536c79f7c81bc9f97 Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Wed, 5 Jun 2019 18:13:31 +0200 Subject: [PATCH] Add blacklist function to utilities --- fimfarchive/utils.py | 39 +++++++++++++++++++++++++++++++++++- tests/test_utils.py | 47 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/fimfarchive/utils.py b/fimfarchive/utils.py index 58d728f..6f92d6c 100644 --- a/fimfarchive/utils.py +++ b/fimfarchive/utils.py @@ -31,7 +31,7 @@ from importlib_resources import read_binary, read_text from pathlib import Path from typing import ( cast, Any, Callable, Dict, Iterator, - Optional, Tuple, Type, TypeVar, Union, + Optional, Set, Tuple, Type, TypeVar, Union, ) from tqdm import tqdm @@ -64,6 +64,20 @@ tqdm = partial( ) +# +# Authors who have opted out of being archived. +# +# Please respect their wishes. +# + +AUTHOR_BLACKLIST: Set[int] = { + 135140, +} + +STORY_BLACKLIST: Set[int] = set() +STORY_WHITELIST: Set[int] = set() + + class EmptyMeta(type): """ Meta-class for Empty. @@ -222,6 +236,29 @@ def get_path(source: Union[None, Path, str]) -> Optional[Path]: return Path(source).resolve() +def is_blacklisted(story: Story) -> bool: + """ + Checks if a story has been blacklisted. + + Args: + story: Instance to check. + + Returns: + True if a story has been blacklisted. + """ + story_id = story.key + author_id = story.meta['author']['id'] + + if story_id in STORY_WHITELIST: + return False + elif story_id in STORY_BLACKLIST: + return True + elif author_id in AUTHOR_BLACKLIST: + return True + else: + return False + + class ResourceLoader: """ Loads resources from a package. diff --git a/tests/test_utils.py b/tests/test_utils.py index 86d19b6..3be0b5c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -29,13 +29,21 @@ from unittest.mock import call, patch import pytest +from fimfarchive import utils from fimfarchive.flavors import DataFormat, MetaFormat, MetaPurity from fimfarchive.utils import ( - find_flavor, get_path, - Empty, JayWalker, PersistedDict + find_flavor, get_path, is_blacklisted, + Empty, JayWalker, PersistedDict, ) +BLACKLISTED_AUTHOR = 1 +BLACKLISTED_STORY = 2 +WHITELISTED_STORY = 3 +UNLISTED_AUTHOR = 4 +UNLISTED_STORY = 5 + + class TestEmpty: """ Empty tests. @@ -313,3 +321,38 @@ class TestGetPath: Tests function returns the correct value. """ assert target == get_path(source) + + +class TestIsBlacklisted: + """ + is_blacklisted tests. + """ + + @pytest.fixture + def utils(self): + """ + Patches the blacklists and whitelists. + """ + ab = patch.object(utils, 'AUTHOR_BLACKLIST', {BLACKLISTED_AUTHOR}) + sb = patch.object(utils, 'STORY_BLACKLIST', {BLACKLISTED_STORY}) + sw = patch.object(utils, 'STORY_WHITELIST', {WHITELISTED_STORY}) + + with ab, sb, sw: + yield utils + + @pytest.mark.parametrize('key,author,result', [ + (BLACKLISTED_STORY, BLACKLISTED_AUTHOR, True), + (BLACKLISTED_STORY, UNLISTED_AUTHOR, True), + (UNLISTED_STORY, BLACKLISTED_AUTHOR, True), + (UNLISTED_STORY, UNLISTED_AUTHOR, False), + (WHITELISTED_STORY, BLACKLISTED_AUTHOR, False), + (WHITELISTED_STORY, UNLISTED_AUTHOR, False), + ]) + def test_blacklisted(self, utils, story, key, author, result): + """ + Tests the various blacklist combinations. + """ + meta = {'id': key, 'author': {'id': author}} + story = story.merge(key=key, meta=meta) + + assert result is is_blacklisted(story)