Rename pk attribute to key in fetchers

This commit is contained in:
Joakim Soderlund 2016-12-17 22:23:45 +01:00
parent f2853103a2
commit 0c0cdf8c3f
2 changed files with 41 additions and 41 deletions

View file

@ -55,12 +55,12 @@ class Fetcher:
""" """
raise NotImplementedError() raise NotImplementedError()
def fetch(self, pk): def fetch(self, key):
""" """
Fetches story information. Fetches story information.
Args: Args:
pk: Primary key of the story. key: Primary key of the story.
Returns: Returns:
Story: A new `Story` object. Story: A new `Story` object.
@ -71,12 +71,12 @@ class Fetcher:
""" """
raise NotImplementedError() raise NotImplementedError()
def fetch_data(self, pk): def fetch_data(self, key):
""" """
Fetches story content data. Fetches story content data.
Args: Args:
pk: Primary key of the story. key: Primary key of the story.
Returns: Returns:
bytes: Story content data. bytes: Story content data.
@ -87,12 +87,12 @@ class Fetcher:
""" """
raise NotImplementedError() raise NotImplementedError()
def fetch_meta(self, pk): def fetch_meta(self, key):
""" """
Fetches story meta information. Fetches story meta information.
Args: Args:
pk: Primary key of the story. key: Primary key of the story.
Returns: Returns:
dict: Story meta information. dict: Story meta information.
@ -141,8 +141,8 @@ class FimfictionFetcher(Fetcher):
return response return response
def fetch_data(self, pk): def fetch_data(self, key):
response = self.get(self.data_path, story=pk, html=True) response = self.get(self.data_path, story=key, html=True)
data = response.content data = response.content
if len(data) == 0: if len(data) == 0:
@ -156,8 +156,8 @@ class FimfictionFetcher(Fetcher):
return data return data
def fetch_meta(self, pk): def fetch_meta(self, key):
response = self.get(self.meta_path, story=pk) response = self.get(self.meta_path, story=key)
try: try:
meta = response.json() meta = response.json()
@ -249,12 +249,12 @@ class FimfarchiveFetcher(Fetcher):
gc.collect() gc.collect()
def lookup(self, pk): def lookup(self, key):
""" """
Finds meta for a story in the index. Finds meta for a story in the index.
Args: Args:
pk: Primary key of the story. key: Primary key of the story.
Returns: Returns:
dict: A reference to the story's meta. dict: A reference to the story's meta.
@ -266,15 +266,15 @@ class FimfarchiveFetcher(Fetcher):
if not self.is_open: if not self.is_open:
raise StorySourceError("Fetcher is closed.") raise StorySourceError("Fetcher is closed.")
pk = str(pk) key = str(key)
if pk not in self.index: if key not in self.index:
raise InvalidStoryError("Story does not exist.") raise InvalidStoryError("Story does not exist.")
return self.index[pk] return self.index[key]
def fetch_data(self, pk): def fetch_data(self, key):
meta = self.lookup(pk) meta = self.lookup(key)
if 'path' not in meta: if 'path' not in meta:
raise StorySourceError("Index is missing a path value.") raise StorySourceError("Index is missing a path value.")
@ -295,6 +295,6 @@ class FimfarchiveFetcher(Fetcher):
return data return data
def fetch_meta(self, pk): def fetch_meta(self, key):
meta = self.lookup(pk) meta = self.lookup(key)
return deepcopy(meta) return deepcopy(meta)

View file

@ -28,10 +28,10 @@ from fimfarchive.exceptions import InvalidStoryError, StorySourceError
from fimfarchive.fetchers import FimfarchiveFetcher, FimfictionFetcher from fimfarchive.fetchers import FimfarchiveFetcher, FimfictionFetcher
VALID_STORY_PK = 9 VALID_STORY_KEY = 9
INVALID_STORY_PK = 7 INVALID_STORY_KEY = 7
EMPTY_STORY_PK = 8 EMPTY_STORY_KEY = 8
PROTECTED_STORY_PK = 208799 PROTECTED_STORY_KEY = 208799
FIMFARCHIVE_PATH = 'fimfarchive-20160525.zip' FIMFARCHIVE_PATH = 'fimfarchive-20160525.zip'
@ -78,8 +78,8 @@ class TestFimfictionFetcher:
""" """
Tests meta is returned if story is valid Tests meta is returned if story is valid
""" """
meta = self.fetcher.fetch_meta(VALID_STORY_PK) meta = self.fetcher.fetch_meta(VALID_STORY_KEY)
assert meta['id'] == VALID_STORY_PK assert meta['id'] == VALID_STORY_KEY
assert meta['words'] != 0 assert meta['words'] != 0
def test_fetch_meta_for_invalid_story(self): def test_fetch_meta_for_invalid_story(self):
@ -87,7 +87,7 @@ class TestFimfictionFetcher:
Tests `InvalidStoryError` is raised if story is invalid. Tests `InvalidStoryError` is raised if story is invalid.
""" """
with pytest.raises(InvalidStoryError): with pytest.raises(InvalidStoryError):
self.fetcher.fetch_meta(INVALID_STORY_PK) self.fetcher.fetch_meta(INVALID_STORY_KEY)
def test_fetch_meta_for_empty_story(self): def test_fetch_meta_for_empty_story(self):
""" """
@ -97,8 +97,8 @@ class TestFimfictionFetcher:
since the story data is useless. But because the meta of such stories since the story data is useless. But because the meta of such stories
may still be of interest, it was decided that meta should be returned. may still be of interest, it was decided that meta should be returned.
""" """
meta = self.fetcher.fetch_meta(EMPTY_STORY_PK) meta = self.fetcher.fetch_meta(EMPTY_STORY_KEY)
assert meta['id'] == EMPTY_STORY_PK assert meta['id'] == EMPTY_STORY_KEY
assert meta['words'] == 0 assert meta['words'] == 0
def test_fetch_meta_for_protected_story(self): def test_fetch_meta_for_protected_story(self):
@ -109,15 +109,15 @@ class TestFimfictionFetcher:
since the story data is inaccessible. However, there is no way to since the story data is inaccessible. However, there is no way to
determine that a story is password-protected from its meta data. determine that a story is password-protected from its meta data.
""" """
meta = self.fetcher.fetch_meta(PROTECTED_STORY_PK) meta = self.fetcher.fetch_meta(PROTECTED_STORY_KEY)
assert meta['id'] == PROTECTED_STORY_PK assert meta['id'] == PROTECTED_STORY_KEY
assert meta['words'] != 0 assert meta['words'] != 0
def test_fetch_data_for_valid_story(self): def test_fetch_data_for_valid_story(self):
""" """
Tests data is returned if story is valid. Tests data is returned if story is valid.
""" """
data = self.fetcher.fetch_data(VALID_STORY_PK) data = self.fetcher.fetch_data(VALID_STORY_KEY)
assert len(data) != 0 assert len(data) != 0
def test_fetch_data_for_invalid_story(self): def test_fetch_data_for_invalid_story(self):
@ -125,21 +125,21 @@ class TestFimfictionFetcher:
Tests `InvalidStoryError` is raised if story is invalid. Tests `InvalidStoryError` is raised if story is invalid.
""" """
with pytest.raises(InvalidStoryError): with pytest.raises(InvalidStoryError):
self.fetcher.fetch_data(INVALID_STORY_PK) self.fetcher.fetch_data(INVALID_STORY_KEY)
def test_fetch_data_for_empty_story(self): def test_fetch_data_for_empty_story(self):
""" """
Tests `InvalidStoryError` is raised if story is empty. Tests `InvalidStoryError` is raised if story is empty.
""" """
with pytest.raises(InvalidStoryError): with pytest.raises(InvalidStoryError):
self.fetcher.fetch_data(EMPTY_STORY_PK) self.fetcher.fetch_data(EMPTY_STORY_KEY)
def test_fetch_data_for_protected_story(self): def test_fetch_data_for_protected_story(self):
""" """
Tests `InvalidStoryError` is raised if story is protected. Tests `InvalidStoryError` is raised if story is protected.
""" """
with pytest.raises(InvalidStoryError): with pytest.raises(InvalidStoryError):
self.fetcher.fetch_data(PROTECTED_STORY_PK) self.fetcher.fetch_data(PROTECTED_STORY_KEY)
class TestFimfarchiveFetcher: class TestFimfarchiveFetcher:
@ -162,17 +162,17 @@ class TestFimfarchiveFetcher:
Tests fetcher can be used in with statements. Tests fetcher can be used in with statements.
""" """
with FimfarchiveFetcher(FIMFARCHIVE_PATH) as fetcher: with FimfarchiveFetcher(FIMFARCHIVE_PATH) as fetcher:
fetcher.lookup(VALID_STORY_PK) fetcher.lookup(VALID_STORY_KEY)
with pytest.raises(StorySourceError): with pytest.raises(StorySourceError):
fetcher.lookup(VALID_STORY_PK) fetcher.lookup(VALID_STORY_KEY)
def test_fetch_meta_for_valid_story(self): def test_fetch_meta_for_valid_story(self):
""" """
Tests meta is returned if story is valid Tests meta is returned if story is valid
""" """
meta = self.fetcher.fetch_meta(VALID_STORY_PK) meta = self.fetcher.fetch_meta(VALID_STORY_KEY)
assert meta['id'] == VALID_STORY_PK assert meta['id'] == VALID_STORY_KEY
assert meta['words'] != 0 assert meta['words'] != 0
def test_fetch_meta_for_invalid_story(self): def test_fetch_meta_for_invalid_story(self):
@ -180,13 +180,13 @@ class TestFimfarchiveFetcher:
Tests `InvalidStoryError` is raised if story is invalid. Tests `InvalidStoryError` is raised if story is invalid.
""" """
with pytest.raises(InvalidStoryError): with pytest.raises(InvalidStoryError):
self.fetcher.fetch_meta(INVALID_STORY_PK) self.fetcher.fetch_meta(INVALID_STORY_KEY)
def test_fetch_data_for_valid_story(self): def test_fetch_data_for_valid_story(self):
""" """
Tests data is returned if story is valid. Tests data is returned if story is valid.
""" """
data = self.fetcher.fetch_data(VALID_STORY_PK) data = self.fetcher.fetch_data(VALID_STORY_KEY)
assert len(data) != 0 assert len(data) != 0
def test_fetch_data_for_invalid_story(self): def test_fetch_data_for_invalid_story(self):
@ -194,4 +194,4 @@ class TestFimfarchiveFetcher:
Tests `InvalidStoryError` is raised if story is invalid. Tests `InvalidStoryError` is raised if story is invalid.
""" """
with pytest.raises(InvalidStoryError): with pytest.raises(InvalidStoryError):
self.fetcher.fetch_data(INVALID_STORY_PK) self.fetcher.fetch_data(INVALID_STORY_KEY)