diff --git a/hyde/ext/plugins/sorter.py b/hyde/ext/plugins/sorter.py index c923283..894b540 100644 --- a/hyde/ext/plugins/sorter.py +++ b/hyde/ext/plugins/sorter.py @@ -35,6 +35,16 @@ def filter_method(item, settings=None): break return all_match +def attributes_checker(item, attributes=None): + """ + Checks if the given list of attributes exist. + """ + try: + x = attrgetter(*attributes)(item) + return True + except AttributeError: + return False + def sort_method(node, settings=None): """ Sorts the resources in the given node based on the @@ -43,13 +53,17 @@ def sort_method(node, settings=None): attr = 'name' if settings and hasattr(settings, 'attr') and settings.attr: attr = settings.attr - filter_ = partial(filter_method, settings=settings) - resources = ifilter(filter_, node.walk_resources()) reverse = False if settings and hasattr(settings, 'reverse'): reverse = settings.reverse if not isinstance(attr, list): attr = [attr] + filter_ = partial(filter_method, settings=settings) + + excluder_ = partial(attributes_checker, attributes=attr) + + resources = ifilter(lambda x: excluder_(x) and filter_(x), + node.walk_resources()) return sorted(resources, key=attrgetter(*attr), reverse=reverse) diff --git a/hyde/tests/ext/test_sorter.py b/hyde/tests/ext/test_sorter.py index ba09e28..a2ee7ae 100644 --- a/hyde/tests/ext/test_sorter.py +++ b/hyde/tests/ext/test_sorter.py @@ -287,6 +287,27 @@ class TestSorterMeta(object): def tearDown(self): TEST_SITE.delete() + def test_attribute_checker_no_meta(self): + s = Site(TEST_SITE) + s.load() + from hyde.ext.plugins.sorter import attributes_checker + for r in s.content.walk_resources(): + assert not attributes_checker(r, ['meta.index']) + + def test_attribute_checker_with_meta(self): + s = Site(TEST_SITE) + s.load() + MetaPlugin(s).begin_site() + from hyde.ext.plugins.sorter import attributes_checker + have_index = ["angry-post.html", + "another-sad-post.html", + "happy-post.html"] + for r in s.content.walk_resources(): + print r.meta.to_dict() + expected = r.name in have_index + assert attributes_checker(r, ['meta.index']) == expected + + def test_walk_resources_sorted_by_index(self): s = Site(TEST_SITE) s.load() @@ -296,6 +317,7 @@ class TestSorterMeta(object): } } s.config.sorter = Expando(config) + MetaPlugin(s).begin_site() SorterPlugin(s).begin_site() assert hasattr(s.content, 'walk_resources_sorted_by_index') @@ -304,7 +326,7 @@ class TestSorterMeta(object): "happy-post.html"] pages = [page.name for page in - s.content.walk_resources_sorted_by_kind()] + s.content.walk_resources_sorted_by_index()] assert pages == sorted(expected, key=lambda f: (File(f).kind, f))