From 2c434ebac1c64ac2da85cdeeadeaf2c3bd5307cc Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 3 Jan 2013 13:54:56 -0500 Subject: [PATCH] contents: Add rich comparisons to URLWrapper for easy sorting There have been earlier attempts to sort categories and authors [1,2,3], but they either sorted based on the object id [3], or only sorted the main author and categories list. This patch uses rich comparisons (keyed off URLWrapper.name, but easily adjustable in subclasses) to make the objects sortable without specifying a key for each sort. For example, now {% for tag, articles in tags|sort %} works as expected in a Jinja template. The functools.total_ordering decorator fills in the missing rich comparisons [4,5]. [1]: 877d454c8fa979343d4881a00977d9ac8786f178 [2]: 7f36e0ed20745c73886d96d4af303b0d858b8a53 [3]: d0ec18f4dbd623c55bdf4928ee7c363f699ef696 [4]: http://docs.python.org/2/library/functools.html#functools.total_ordering [5]: http://docs.python.org/3/library/functools.html#functools.total_ordering --- pelican/contents.py | 16 +++++++++++++++- pelican/generators.py | 3 +-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pelican/contents.py b/pelican/contents.py index 4655d4cc..bd257ad8 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -243,7 +243,9 @@ class Article(Page): class Quote(Page): base_properties = ('author', 'date') + @python_2_unicode_compatible +@functools.total_ordering class URLWrapper(object): def __init__(self, name, settings): self.name = name @@ -256,8 +258,20 @@ class URLWrapper(object): def __hash__(self): return hash(self.name) + def _key(self): + return self.name + + def _normalize_key(self, key): + return six.text_type(key) + def __eq__(self, other): - return self.name == other + return self._key() == self._normalize_key(other) + + def __ne__(self, other): + return self._key() != self._normalize_key(other) + + def __lt__(self, other): + return self._key() < self._normalize_key(other) def __str__(self): return self.name diff --git a/pelican/generators.py b/pelican/generators.py index ce102a31..49b6bc1d 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -416,11 +416,10 @@ class ArticlesGenerator(Generator): # order the categories per name self.categories = list(self.categories.items()) self.categories.sort( - key=lambda item: item[0].name, reverse=self.settings['REVERSE_CATEGORY_ORDER']) self.authors = list(self.authors.items()) - self.authors.sort(key=lambda item: item[0].name) + self.authors.sort() self._update_context(('articles', 'dates', 'tags', 'categories', 'tag_cloud', 'authors', 'related_posts'))