From f70b1b3495fb121b57d3463fba21eaedca5d1d79 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Fri, 7 Jan 2011 11:14:16 +0530 Subject: [PATCH] Ensured the is_processable is always accounted for when sorting --- hyde/ext/plugins/sorter.py | 9 ++++--- hyde/tests/ext/test_sorter.py | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/hyde/ext/plugins/sorter.py b/hyde/ext/plugins/sorter.py index d993cc8..6ba36ad 100644 --- a/hyde/ext/plugins/sorter.py +++ b/hyde/ext/plugins/sorter.py @@ -26,8 +26,8 @@ def filter_method(item, settings=None): given settings evaluate to True. """ - all_match = True - if settings and hasattr(settings, 'filters'): + all_match = item.is_processable + if all_match and settings and hasattr(settings, 'filters'): filters = settings.filters for field, value in filters.__dict__.items(): try: @@ -49,7 +49,10 @@ def sort_method(node, settings=None): attr = settings.attr filter_ = partial(filter_method, settings=settings) resources = ifilter(filter_, node.walk_resources()) - return sorted(resources, key=attrgetter(attr)) + reverse = False + if settings and hasattr(settings, 'reverse'): + reverse = settings.reverse + return sorted(resources, key=attrgetter(attr), reverse=reverse) def make_method(method_name, method_): def method__(self): diff --git a/hyde/tests/ext/test_sorter.py b/hyde/tests/ext/test_sorter.py index 6b83857..201498a 100644 --- a/hyde/tests/ext/test_sorter.py +++ b/hyde/tests/ext/test_sorter.py @@ -49,6 +49,31 @@ class TestMeta(object): assert pages == sorted(expected, key=lambda f: File(f).kind) + def test_walk_resources_sorted_reverse(self): + s = Site(TEST_SITE) + s.load() + s.config.plugins = ['hyde.ext.sorter.SorterPlugin'] + s.config.sorter = Expando(dict(kind=dict(attr='source_file.kind', reverse=True))) + + SorterPlugin(s).begin_site() + + assert hasattr(s.content, 'walk_resources_sorted_by_kind') + expected = ["404.html", + "about.html", + "apple-touch-icon.png", + "merry-christmas.html", + "crossdomain.xml", + "favicon.ico", + "robots.txt", + "site.css" + ] + + pages = [page.name for page in + s.content.walk_resources_sorted_by_kind()] + + + assert pages == sorted(expected, key=lambda f: File(f).kind, reverse=True) + def test_walk_resources_sorted_with_filters(self): s = Site(TEST_SITE) cfg = """ @@ -73,6 +98,29 @@ class TestMeta(object): assert pages == sorted(expected) + def test_walk_resources_sorted_default_is_processable(self): + s = Site(TEST_SITE) + cfg = """ + plugins: + - hyde.ext.sorter.SorterPlugin + sorter: + kind2: + filters: + source_file.kind: html + """ + s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) + s.load() + p_404 = s.content.resource_from_relative_path('404.html') + p_404.is_processable = False + SorterPlugin(s).begin_site() + + assert hasattr(s.content, 'walk_resources_sorted_by_kind2') + expected = ["about.html", "merry-christmas.html"] + + pages = [page.name for page in s.content.walk_resources_sorted_by_kind2()] + + assert pages == sorted(expected) + def test_prev_next(self): s = Site(TEST_SITE) cfg = """