diff options
| author | Alessio Ababilov <aababilov@griddynamics.com> | 2013-06-10 10:49:59 +0300 |
|---|---|---|
| committer | Alessio Ababilov <ilovegnulinux@gmail.com> | 2013-06-11 20:05:49 +0300 |
| commit | 472d8ed162ffc0b1083b761627a8ed86e7d71ae7 (patch) | |
| tree | e7c23277b4d5d44dcb4d799c6af815a9214d0345 /openstack/common | |
| parent | faddbbe318cf62791624469c4304ae4fdcc2e639 (diff) | |
| download | oslo-472d8ed162ffc0b1083b761627a8ed86e7d71ae7.tar.gz oslo-472d8ed162ffc0b1083b761627a8ed86e7d71ae7.tar.xz oslo-472d8ed162ffc0b1083b761627a8ed86e7d71ae7.zip | |
Add slugify to strutils
This function will be used in apiclient library.
Change-Id: I19f976eda896e7bede07510aafebe4931e512351
Diffstat (limited to 'openstack/common')
| -rw-r--r-- | openstack/common/strutils.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/openstack/common/strutils.py b/openstack/common/strutils.py index 8a5367b..87a9f24 100644 --- a/openstack/common/strutils.py +++ b/openstack/common/strutils.py @@ -19,7 +19,9 @@ System-level utilities and helper functions. """ +import re import sys +import unicodedata from openstack.common.gettextutils import _ @@ -38,6 +40,9 @@ BYTE_MULTIPLIERS = { TRUE_STRINGS = ('1', 't', 'true', 'on', 'y', 'yes') FALSE_STRINGS = ('0', 'f', 'false', 'off', 'n', 'no') +SLUGIFY_STRIP_RE = re.compile(r"[^\w\s-]") +SLUGIFY_HYPHENATE_RE = re.compile(r"[-\s]+") + def int_from_bool_as_string(subject): """ @@ -187,3 +192,28 @@ def to_bytes(text, default=0): raise TypeError(msg) except ValueError: return default + + +def to_slug(value, incoming=None, errors="strict"): + """Normalize string. + + Convert to lowercase, remove non-word characters, and convert spaces + to hyphens. + + Inspired by Django's `slugify` filter. + + :param value: Text to slugify + :param incoming: Text's current encoding + :param errors: Errors handling policy. See here for valid + values http://docs.python.org/2/library/codecs.html + :returns: slugified unicode representation of `value` + :raises TypeError: If text is not an instance of basestring + """ + value = safe_decode(value, incoming, errors) + # NOTE(aababilov): no need to use safe_(encode|decode) here: + # encodings are always "ascii", error handling is always "ignore" + # and types are always known (first: unicode; second: str) + value = unicodedata.normalize("NFKD", value).encode( + "ascii", "ignore").decode("ascii") + value = SLUGIFY_STRIP_RE.sub("", value).strip().lower() + return SLUGIFY_HYPHENATE_RE.sub("-", value) |
