diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3603a86..34d4c52 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,11 @@ +Version 0.8.3c10 +=============== + +* Bug Fix: `hyde create` only fails when `content`, `layout` or `site.yaml` + is present in the target directory. (Issue #21) +* Bug Fix: Exceptions are now handled with `ArgumentParser.error`. + (Issue #21) + Version 0.8.3c9 =============== diff --git a/hyde/engine.py b/hyde/engine.py index b79f391..7f0c2a4 100644 --- a/hyde/engine.py +++ b/hyde/engine.py @@ -4,7 +4,7 @@ Implements the hyde entry point commands """ from commando import * from hyde.exceptions import HydeException -from hyde.fs import File, Folder +from hyde.fs import FS, File, Folder from hyde.layout import Layout, HYDE_DATA from hyde.model import Config from hyde.site import Site @@ -23,6 +23,28 @@ class Engine(Application): """ The Hyde Application """ + def __init__(self, raise_exceptions=False): + self.raise_exceptions = raise_exceptions + super(Engine, self).__init__() + + def run(self, args=None): + """ + The engine entry point. + """ + + # Catch any errors thrown and log the message. + + try: + super(Engine, self).run(args) + except HydeException, he: + if self.raise_exceptions: + raise + elif self.__parser__: + self.__parser__.error(he.message) + else: + logger.error(he.message) + return -1 + @command(description='hyde - a python static website generator', epilog='Use %(prog)s {command} -h to get help on individual commands') @@ -39,7 +61,7 @@ class Engine(Application): import logging logger.setLevel(logging.DEBUG) - @subcommand('create', help='Create a new hyde site') + @subcommand('create', help='Create a new hyde site.') @store('-l', '--layout', default='basic', help='Layout for the new site') @true('-f', '--force', default=False, dest='overwrite', help='Overwrite the current site if it exists') @@ -50,9 +72,12 @@ class Engine(Application): """ self.main(args) sitepath = Folder(Folder(args.sitepath).fully_expanded_path) - if sitepath.exists and not args.overwrite: + markers = ['content', 'layout', 'site.yaml'] + exists = any((FS(sitepath.child(item)).exists for item in markers)) + + if exists and not args.overwrite: raise HydeException( - "The given site path [%s] already exists." + "The given site path [%s] already contains a hyde site." " Use -f to overwrite." % sitepath) layout = Layout.find_layout(args.layout) logger.info( diff --git a/hyde/tests/ext/test_sorter.py b/hyde/tests/ext/test_sorter.py index a2ee7ae..fa2b46d 100644 --- a/hyde/tests/ext/test_sorter.py +++ b/hyde/tests/ext/test_sorter.py @@ -329,6 +329,3 @@ class TestSorterMeta(object): s.content.walk_resources_sorted_by_index()] assert pages == sorted(expected, key=lambda f: (File(f).kind, f)) - - - diff --git a/hyde/tests/test_initialize.py b/hyde/tests/test_initialize.py index c099a19..71c7a94 100644 --- a/hyde/tests/test_initialize.py +++ b/hyde/tests/test_initialize.py @@ -33,26 +33,57 @@ def delete_test_site_at_user(): @raises(HydeException) @with_setup(create_test_site, delete_test_site) -def test_ensure_exception_when_sitepath_exists(): - e = Engine() +def test_ensure_exception_when_site_yaml_exists(): + e = Engine(raise_exceptions=True) + File(TEST_SITE.child('site.yaml')).write("Hey") e.run(e.parse(['-s', str(TEST_SITE), 'create'])) +@raises(HydeException) +@with_setup(create_test_site, delete_test_site) +def test_ensure_exception_when_content_folder_exists(): + e = Engine(raise_exceptions=True) + TEST_SITE.child_folder('content').make() + e.run(e.parse(['-s', str(TEST_SITE), 'create'])) + +@raises(HydeException) +@with_setup(create_test_site, delete_test_site) +def test_ensure_exception_when_layout_folder_exists(): + e = Engine(raise_exceptions=True) + TEST_SITE.child_folder('layout').make() + e.run(e.parse(['-s', str(TEST_SITE), 'create'])) + +@with_setup(create_test_site, delete_test_site) +def test_ensure_no_exception_when_empty_site_exists(): + e = Engine(raise_exceptions=True) + e.run(e.parse(['-s', str(TEST_SITE), 'create'])) + verify_site_contents(TEST_SITE, Layout.find_layout()) + @with_setup(create_test_site, delete_test_site) -def test_ensure_no_exception_when_sitepath_exists_when_forced(): - e = Engine() +def test_ensure_no_exception_when_forced(): + e = Engine(raise_exceptions=True) + TEST_SITE.child_folder('layout').make() + e.run(e.parse(['-s', str(TEST_SITE), 'create', '-f'])) + verify_site_contents(TEST_SITE, Layout.find_layout()) + TEST_SITE.delete() + TEST_SITE.child_folder('content').make() e.run(e.parse(['-s', str(TEST_SITE), 'create', '-f'])) - assert True #No Exception + verify_site_contents(TEST_SITE, Layout.find_layout()) + TEST_SITE.delete() + TEST_SITE.make() + File(TEST_SITE.child('site.yaml')).write("Hey") + e.run(e.parse(['-s', str(TEST_SITE), 'create', '-f'])) + verify_site_contents(TEST_SITE, Layout.find_layout()) @with_setup(create_test_site, delete_test_site) def test_ensure_no_exception_when_sitepath_does_not_exist(): - e = Engine() + e = Engine(raise_exceptions=True) TEST_SITE.delete() e.run(e.parse(['-s', str(TEST_SITE), 'create', '-f'])) verify_site_contents(TEST_SITE, Layout.find_layout()) @with_setup(create_test_site_at_user, delete_test_site_at_user) def test_ensure_can_create_site_at_user(): - e = Engine() + e = Engine(raise_exceptions=True) TEST_SITE_AT_USER.delete() e.run(e.parse(['-s', str(TEST_SITE_AT_USER), 'create', '-f'])) verify_site_contents(TEST_SITE_AT_USER, Layout.find_layout()) @@ -75,6 +106,6 @@ def verify_site_contents(site, layout): @raises(HydeException) @with_setup(create_test_site, delete_test_site) def test_ensure_exception_when_layout_is_invalid(): - e = Engine() + e = Engine(raise_exceptions=True) e.run(e.parse(['-s', str(TEST_SITE), 'create', '-l', 'junk']))