Skip to content

Commit

Permalink
Require an atom_title in the blog configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Jun 11, 2015
1 parent e3ff42f commit d984834
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions handroll/extensions/blog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Copyright (c) 2015, Matt Layman

try:
import ConfigParser as configparser
except ImportError: # pragma: no cover
import configparser

from handroll.exceptions import AbortError
from handroll.extensions.base import Extension
from handroll.i18n import _
Expand All @@ -21,12 +26,14 @@ class BlogExtension(Extension):
def __init__(self, config):
super(BlogExtension, self).__init__(config)
self.posts = []
self.atom_metadata = {}

def on_pre_composition(self, director):
"""Check that all the required configuration exists."""
if not self._config.parser.has_section('blog'):
raise AbortError(
_('A blog section is missing in the configuration file.'))
self._add_atom_metadata('title', 'atom_title')

def on_frontmatter_loaded(self, source_file, frontmatter):
"""Scan for blog posts.
Expand All @@ -42,3 +49,12 @@ def on_frontmatter_loaded(self, source_file, frontmatter):
self.posts.append(BlogPost(
source_file=source_file,
))

def _add_atom_metadata(self, name, option):
"""Add atom metadata from the config parser."""
try:
self.atom_metadata[name] = self._config.parser.get('blog', option)
except configparser.NoOptionError:
raise AbortError(
_('The blog extension requires the {option} option.').format(
option=option))
27 changes: 27 additions & 0 deletions handroll/tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ def tearDown(self):
signals.pre_composition.receivers.clear()
signals.post_composition.receivers.clear()

def _add_blog_section(self, parser, exclude=None):
parser.add_section('blog')
metadata = {
'atom_title': 'Amazing blog',
}
for option, value in metadata.items():
if option == exclude:
continue
parser.set('blog', option, value)

def test_handles_frontmatter_loaded(self):
extension = BlogExtension(None)
self.assertTrue(extension.handle_frontmatter_loaded)
Expand Down Expand Up @@ -126,3 +136,20 @@ def test_requires_blog_section(self):
director = self.factory.make_director()
extension = BlogExtension(director.config)
self.assertRaises(AbortError, extension.on_pre_composition, director)

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

def test_has_atom_title_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('Amazing blog', extension.atom_metadata['title'])

0 comments on commit d984834

Please sign in to comment.