Add blacklist function to utilities

This commit is contained in:
Joakim Soderlund 2019-06-05 18:13:31 +02:00
parent 54b3cd219b
commit 1e91a64a36
2 changed files with 83 additions and 3 deletions

View file

@ -31,7 +31,7 @@ from importlib_resources import read_binary, read_text
from pathlib import Path from pathlib import Path
from typing import ( from typing import (
cast, Any, Callable, Dict, Iterator, cast, Any, Callable, Dict, Iterator,
Optional, Tuple, Type, TypeVar, Union, Optional, Set, Tuple, Type, TypeVar, Union,
) )
from tqdm import tqdm 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): class EmptyMeta(type):
""" """
Meta-class for Empty. Meta-class for Empty.
@ -222,6 +236,29 @@ def get_path(source: Union[None, Path, str]) -> Optional[Path]:
return Path(source).resolve() 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: class ResourceLoader:
""" """
Loads resources from a package. Loads resources from a package.

View file

@ -29,13 +29,21 @@ from unittest.mock import call, patch
import pytest import pytest
from fimfarchive import utils
from fimfarchive.flavors import DataFormat, MetaFormat, MetaPurity from fimfarchive.flavors import DataFormat, MetaFormat, MetaPurity
from fimfarchive.utils import ( from fimfarchive.utils import (
find_flavor, get_path, find_flavor, get_path, is_blacklisted,
Empty, JayWalker, PersistedDict Empty, JayWalker, PersistedDict,
) )
BLACKLISTED_AUTHOR = 1
BLACKLISTED_STORY = 2
WHITELISTED_STORY = 3
UNLISTED_AUTHOR = 4
UNLISTED_STORY = 5
class TestEmpty: class TestEmpty:
""" """
Empty tests. Empty tests.
@ -313,3 +321,38 @@ class TestGetPath:
Tests function returns the correct value. Tests function returns the correct value.
""" """
assert target == get_path(source) 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)