Skip to content

Commit

Permalink
Require atom_output in blog configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Jun 18, 2015
1 parent 0b7a31d commit f315249
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 7 additions & 1 deletion handroll/extensions/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, config):
super(BlogExtension, self).__init__(config)
self.posts = []
self.atom_metadata = {}
self.atom_output = ''

def on_pre_composition(self, director):
"""Check that all the required configuration exists."""
Expand All @@ -42,6 +43,7 @@ def on_pre_composition(self, director):
_('A blog section is missing in the configuration file.'))
for metadata, option in self.required_metadata.items():
self._add_atom_metadata(metadata, option)
self.atom_output = self._get_option('atom_output')

def on_frontmatter_loaded(self, source_file, frontmatter):
"""Scan for blog posts.
Expand All @@ -60,8 +62,12 @@ def on_frontmatter_loaded(self, source_file, frontmatter):

def _add_atom_metadata(self, name, option):
"""Add atom metadata from the config parser."""
self.atom_metadata[name] = self._get_option(option)

def _get_option(self, option):
"""Get an option out of the blog section."""
try:
self.atom_metadata[name] = self._config.parser.get('blog', option)
return self._config.parser.get('blog', option)
except configparser.NoOptionError:
raise AbortError(
_('The blog extension requires the {option} option.').format(
Expand Down
22 changes: 20 additions & 2 deletions handroll/tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ def tearDown(self):

def _add_blog_section(self, parser, exclude=None):
parser.add_section('blog')
metadata = {
configuration = {
'atom_author': 'Nikola Tesla',
'atom_id': 'https://www.example.com/feed.xml',
'atom_output': 'feed.xml',
'atom_title': 'Amazing blog',
'atom_url': 'https://www.example.com/archive.html',
}
for option, value in metadata.items():
for option, value in configuration.items():
if option == exclude:
continue
parser.set('blog', option, value)
Expand Down Expand Up @@ -210,3 +211,20 @@ def test_has_atom_url_in_metadata(self):
self.assertEqual(
'https://www.example.com/archive.html',
extension.atom_metadata['url'])

def test_requires_atom_output(self):
director = self.factory.make_director()
self._add_blog_section(director.config.parser, exclude='atom_output')
extension = BlogExtension(director.config)
try:
extension.on_pre_composition(director)
self.fail()
except AbortError as ae:
self.assertTrue('atom_output' in str(ae))

def test_has_atom_output_in_metadata(self):
director = self.factory.make_director()
self._add_blog_section(director.config.parser)
extension = BlogExtension(director.config)
extension.on_pre_composition(director)
self.assertEqual('feed.xml', extension.atom_output)

0 comments on commit f315249

Please sign in to comment.