Add abstract base class for story fetchers

This commit is contained in:
Joakim Soderlund 2015-10-01 19:42:50 +02:00
parent d01960d639
commit 341b70d1c5
2 changed files with 115 additions and 0 deletions

View file

@ -26,3 +26,21 @@ class FimfarchiveError(Exception):
""" """
Base class for Fimfarchive exceptions. Base class for Fimfarchive exceptions.
""" """
class InvalidStoryError(FimfarchiveError):
"""
Story does not exist.
"""
class StoryPermissionError(FimfarchiveError):
"""
Story is password protected.
"""
class StorySourceError(FimfarchiveError):
"""
Story source is unusable.
"""

97
fimfarchive/fetchers.py Normal file
View file

@ -0,0 +1,97 @@
"""
Fetchers 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 <http://www.gnu.org/licenses/>.
#
class Fetcher:
"""
Abstract base class for story fetchers.
"""
def __enter__(self):
"""
Returns self for use in with statements.
"""
return self
def __exit__(self, exc_type, exc_value, traceback):
"""
Closes the fetcher on exit in with statements.
"""
self.close()
def close(self):
"""
Closes file descriptors and frees memory.
"""
raise NotImplementedError()
def fetch(self, pk):
"""
Fetches story information.
Args:
pk: Primary key of the story.
Returns:
Story: A new `Story` object.
Raises:
InvalidStoryError: If a valid story is not found.
StoryPermissionError: If access to the story is denied.
StorySourceError: If source does not return any data.
"""
raise NotImplementedError()
def fetch_data(self, pk):
"""
Fetches story content data.
Args:
pk: Primary key of the story.
Returns:
bytes: Story content data.
Raises:
InvalidStoryError: If a valid story is not found.
StoryPermissionError: If access to the story is denied.
StorySourceError: If source does not return any data.
"""
raise NotImplementedError()
def fetch_meta(self, pk):
"""
Fetches story meta information.
Args:
pk: Primary key of the story.
Returns:
dict: Story meta information.
Raises:
InvalidStoryError: If a valid story is not found.
StoryPermissionError: If access to the story is denied.
StorySourceError: If source does not return any data.
"""
raise NotImplementedError()