Browse Source

Added support for sorting with multiple attributes

main
Lakshmi Vyasarajan 14 years ago
parent
commit
f54f0bfe8d
2 changed files with 44 additions and 2 deletions
  1. +5
    -1
      hyde/ext/plugins/sorter.py
  2. +39
    -1
      hyde/tests/ext/test_sorter.py

+ 5
- 1
hyde/ext/plugins/sorter.py View File

@@ -52,7 +52,11 @@ def sort_method(node, settings=None):
reverse = False reverse = False
if settings and hasattr(settings, 'reverse'): if settings and hasattr(settings, 'reverse'):
reverse = settings.reverse reverse = settings.reverse
return sorted(resources, key=attrgetter(attr), reverse=reverse)
if not isinstance(attr, list):
attr = [attr]
return sorted(resources,
key=attrgetter(*attr),
reverse=reverse)


def make_method(method_name, method_): def make_method(method_name, method_):
def method__(self): def method__(self):


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

@@ -11,6 +11,8 @@ from hyde.site import Site
from hyde.model import Config, Expando from hyde.model import Config, Expando
import yaml import yaml


from operator import attrgetter

TEST_SITE = File(__file__).parent.parent.child_folder('_test') TEST_SITE = File(__file__).parent.parent.child_folder('_test')




@@ -46,7 +48,6 @@ class TestMeta(object):
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_kind()]



assert pages == sorted(expected, key=lambda f: File(f).kind) assert pages == sorted(expected, key=lambda f: File(f).kind)


def test_walk_resources_sorted_reverse(self): def test_walk_resources_sorted_reverse(self):
@@ -98,6 +99,43 @@ class TestMeta(object):


assert pages == sorted(expected) assert pages == sorted(expected)


def test_walk_resources_sorted_with_multiple_attributes(self):
s = Site(TEST_SITE)
cfg = """
plugins:
- hyde.ext.sorter.SorterPlugin
sorter:
multi:
attr:
- source_file.kind
- node.name

"""
s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
s.load()
SorterPlugin(s).begin_site()

assert hasattr(s.content, 'walk_resources_sorted_by_multi')
expected = ["content/404.html",
"content/about.html",
"content/apple-touch-icon.png",
"content/blog/2010/december/merry-christmas.html",
"content/crossdomain.xml",
"content/favicon.ico",
"content/robots.txt",
"content/site.css"
]

pages = [page.name for page in s.content.walk_resources_sorted_by_multi()]

expected_sorted = [File(page).name
for page in
sorted(expected,
key=lambda p: tuple(
[File(p).kind,
File(p).parent.name]))]
assert pages == expected_sorted

def test_walk_resources_sorted_default_is_processable(self): def test_walk_resources_sorted_default_is_processable(self):
s = Site(TEST_SITE) s = Site(TEST_SITE)
cfg = """ cfg = """


Loading…
Cancel
Save