improve URLWrapper comparison

* speed up via reduced slugify calls (only call when needed)
* fix __repr__ to not contain str, should call repr on name
* add test_urlwrappers and move URLWrappers tests there
  * add new equality test
* cleanup header

additionally:
* Content is now decorated with python_2_unicode_compatible
  instead of treating __str__ differently
* better formatting for test_article_metadata_key_lowercase
  to actually output the conflict instead of a non descriptive
  error
This commit is contained in:
derwinlu 2015-06-19 11:19:21 +02:00
commit 5389543a39
5 changed files with 80 additions and 48 deletions

View file

@ -1,7 +1,9 @@
import os
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import functools
import logging
import os
import six
from pelican.utils import (slugify, python_2_unicode_compatible)
@ -52,27 +54,36 @@ class URLWrapper(object):
def __hash__(self):
return hash(self.slug)
def _key(self):
return self.slug
def _normalize_key(self, key):
subs = self.settings.get('SLUG_SUBSTITUTIONS', ())
return six.text_type(slugify(key, subs))
def __eq__(self, other):
return self._key() == self._normalize_key(other)
if isinstance(other, self.__class__):
return self.slug == other.slug
if isinstance(other, six.text_type):
return self.slug == self._normalize_key(other)
return False
def __ne__(self, other):
return self._key() != self._normalize_key(other)
if isinstance(other, self.__class__):
return self.slug != other.slug
if isinstance(other, six.text_type):
return self.slug != self._normalize_key(other)
return True
def __lt__(self, other):
return self._key() < self._normalize_key(other)
if isinstance(other, self.__class__):
return self.slug < other.slug
if isinstance(other, six.text_type):
return self.slug < self._normalize_key(other)
return False
def __str__(self):
return self.name
def __repr__(self):
return '<{} {}>'.format(type(self).__name__, str(self))
return '<{} {}>'.format(type(self).__name__, repr(self._name))
def _from_settings(self, key, get_page_name=False):
"""Returns URL information as defined in settings.