Skip to content

Commit

Permalink
Require a blog config for the blog extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Jun 10, 2015
1 parent b719c14 commit e3ff42f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
14 changes: 8 additions & 6 deletions handroll/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def __init__(self):
self.outdir = None
self.timing = None

# Keep the parser to allow extensions to get configuration file data.
self.parser = ConfigParser()

def load_from_arguments(self, args):
"""Load any configuration attributes from the provided command line
arguments. Arguments have the highest precedent so overwrite any other
Expand All @@ -33,13 +36,12 @@ def load_from_arguments(self, args):
def load_from_file(self, config_file):
"""Load any configuration attributes from the provided config file."""
with open(config_file, 'r') as f:
parser = ConfigParser()
parser.readfp(f)
if parser.has_option('site', 'outdir'):
self.parser.readfp(f)
if self.parser.has_option('site', 'outdir'):
self.outdir = os.path.abspath(os.path.expanduser(
parser.get('site', 'outdir')))
if parser.has_section('site'):
self._find_extensions(parser)
self.parser.get('site', 'outdir')))
if self.parser.has_section('site'):
self._find_extensions(self.parser)

def _find_extensions(self, parser):
"""Check if the site options have extensions to enable."""
Expand Down
7 changes: 7 additions & 0 deletions handroll/extensions/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ class BlogExtension(Extension):
"""Track files marked as blog entries and generate a feed."""

handle_frontmatter_loaded = True
handle_pre_composition = True
handle_post_composition = True

def __init__(self, config):
super(BlogExtension, self).__init__(config)
self.posts = []

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.'))

def on_frontmatter_loaded(self, source_file, frontmatter):
"""Scan for blog posts.
Expand Down
7 changes: 7 additions & 0 deletions handroll/tests/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
import os
import tempfile

from handroll.configuration import Configuration
from handroll.director import Director
from handroll.site import Site


class Factory(object):
"""A factory to produce commonly needed objects"""

def make_director(self):
config = Configuration()
site = self.make_site()
return Director(config, site, [])

def make_site(self):
"""Make a valid site instance."""
site = tempfile.mkdtemp()
Expand Down
12 changes: 12 additions & 0 deletions handroll/tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TestExtensionLoader(TestCase):
def tearDown(self):
# Clean up any attached extension instance.
signals.frontmatter_loaded.receivers.clear()
signals.pre_composition.receivers.clear()
signals.post_composition.receivers.clear()

def test_loads_available_extensions(self):
Expand Down Expand Up @@ -88,6 +89,7 @@ class TestBlogExtension(TestCase):
def tearDown(self):
# Clean up any attached extension instance.
signals.frontmatter_loaded.receivers.clear()
signals.pre_composition.receivers.clear()
signals.post_composition.receivers.clear()

def test_handles_frontmatter_loaded(self):
Expand All @@ -114,3 +116,13 @@ def test_blog_must_be_boolean(self):
self.assertRaises(
AbortError, extension.on_frontmatter_loaded, 'animaniacs.md',
{'blog': 'crazy'})

def test_handles_pre_composition(self):
extension = BlogExtension(None)
self.assertTrue(extension.handle_pre_composition)

def test_requires_blog_section(self):
"""A config with no blog section aborts."""
director = self.factory.make_director()
extension = BlogExtension(director.config)
self.assertRaises(AbortError, extension.on_pre_composition, director)

0 comments on commit e3ff42f

Please sign in to comment.