Browse Source

Issue 18: Sorter now excludes items that do not have sorting attributes

main
Lakshmi Vyasarajan 13 years ago
parent
commit
2db79bb55c
2 changed files with 39 additions and 3 deletions
  1. +16
    -2
      hyde/ext/plugins/sorter.py
  2. +23
    -1
      hyde/tests/ext/test_sorter.py

+ 16
- 2
hyde/ext/plugins/sorter.py View File

@@ -35,6 +35,16 @@ def filter_method(item, settings=None):
break break
return all_match 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): def sort_method(node, settings=None):
""" """
Sorts the resources in the given node based on the Sorts the resources in the given node based on the
@@ -43,13 +53,17 @@ def sort_method(node, settings=None):
attr = 'name' attr = 'name'
if settings and hasattr(settings, 'attr') and settings.attr: if settings and hasattr(settings, 'attr') and settings.attr:
attr = settings.attr attr = settings.attr
filter_ = partial(filter_method, settings=settings)
resources = ifilter(filter_, node.walk_resources())
reverse = False reverse = False
if settings and hasattr(settings, 'reverse'): if settings and hasattr(settings, 'reverse'):
reverse = settings.reverse reverse = settings.reverse
if not isinstance(attr, list): if not isinstance(attr, list):
attr = [attr] 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, return sorted(resources,
key=attrgetter(*attr), key=attrgetter(*attr),
reverse=reverse) reverse=reverse)


+ 23
- 1
hyde/tests/ext/test_sorter.py View File

@@ -287,6 +287,27 @@ class TestSorterMeta(object):
def tearDown(self): def tearDown(self):
TEST_SITE.delete() 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): def test_walk_resources_sorted_by_index(self):
s = Site(TEST_SITE) s = Site(TEST_SITE)
s.load() s.load()
@@ -296,6 +317,7 @@ class TestSorterMeta(object):
} }
} }
s.config.sorter = Expando(config) s.config.sorter = Expando(config)
MetaPlugin(s).begin_site()
SorterPlugin(s).begin_site() SorterPlugin(s).begin_site()


assert hasattr(s.content, 'walk_resources_sorted_by_index') assert hasattr(s.content, 'walk_resources_sorted_by_index')
@@ -304,7 +326,7 @@ class TestSorterMeta(object):
"happy-post.html"] "happy-post.html"]


pages = [page.name for page in 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)) assert pages == sorted(expected, key=lambda f: (File(f).kind, f))




Loading…
Cancel
Save