Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring save logic into ContentSaver
- Loading branch information
Showing
6 changed files
with
121 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
import os | ||
|
||
|
||
def _ensure_directory_exists(directory_path): | ||
"""Ensures the directories in directory_path exist.""" | ||
if not os.path.exists(directory_path): | ||
os.makedirs(directory_path) | ||
|
||
|
||
def _write_to_file(filepath, content): | ||
"""writes content to a local file.""" | ||
_ensure_directory_exists(os.path.dirname(filepath)) | ||
open(filepath, 'wb').write(content) | ||
|
||
|
||
class ContentSaver(object): | ||
"""Saves recipe content to disk.""" | ||
|
||
def __init__(self, root, write_file_fn=_write_to_file): | ||
self._root = root | ||
self._write_file_fn = write_file_fn | ||
|
||
def save_metadata(self, metadata): | ||
self._write_file_fn( | ||
self._output_path('metadata.json'), | ||
json.dumps(metadata, indent=4, separators=(',', ':'))) | ||
|
||
def save_recipe_html(self, recipe_html): | ||
self._write_file_fn(self._output_path('index.html'), recipe_html) | ||
|
||
def save_main_image(self, main_image_data): | ||
self._write_file_fn(self._output_path('main.jpg'), main_image_data) | ||
|
||
def _output_path(self, filename): | ||
return os.path.join(self._root, filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import unittest | ||
|
||
import mock | ||
|
||
from ketohub import persist | ||
|
||
|
||
class PersistTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.mock_write_to_file_fn = mock.Mock() | ||
|
||
def test_save_recipe_html_saves_to_correct_file(self): | ||
saver = persist.ContentSaver('downloads/foo', | ||
self.mock_write_to_file_fn) | ||
saver.save_recipe_html('<html>Mock HTML</html>') | ||
|
||
self.mock_write_to_file_fn.assert_has_calls([ | ||
mock.call('downloads/foo/index.html', '<html>Mock HTML</html>'), | ||
]) | ||
|
||
def test_save_main_image_saves_to_correct_file(self): | ||
saver = persist.ContentSaver('downloads/foo', | ||
self.mock_write_to_file_fn) | ||
saver.save_main_image('dummy image data') | ||
|
||
self.mock_write_to_file_fn.assert_has_calls([ | ||
mock.call('downloads/foo/main.jpg', 'dummy image data'), | ||
]) | ||
|
||
def test_save_metadata_saves_to_correct_file(self): | ||
saver = persist.ContentSaver('downloads/foo', | ||
self.mock_write_to_file_fn) | ||
saver.save_metadata({'dummy_key': 'dummy value'}) | ||
|
||
self.mock_write_to_file_fn.assert_has_calls([ | ||
mock.call('downloads/foo/metadata.json', """ | ||
{ | ||
"dummy_key":"dummy value" | ||
}""".strip()), | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters