summaryrefslogtreecommitdiffstats
path: root/openstack
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2013-06-12 11:42:22 +0000
committerGerrit Code Review <review@openstack.org>2013-06-12 11:42:22 +0000
commit02cd9b5f44faed64528c6fbe4c06f734a4c11208 (patch)
tree12fcbe3e0822da1fbd11f309cd929a2c6bf85eac /openstack
parentda554770974706ffa6f69f12d9051062cd0d0b80 (diff)
parent472d8ed162ffc0b1083b761627a8ed86e7d71ae7 (diff)
downloadoslo-02cd9b5f44faed64528c6fbe4c06f734a4c11208.tar.gz
oslo-02cd9b5f44faed64528c6fbe4c06f734a4c11208.tar.xz
oslo-02cd9b5f44faed64528c6fbe4c06f734a4c11208.zip
Merge "Add slugify to strutils"
Diffstat (limited to 'openstack')
-rw-r--r--openstack/common/strutils.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/openstack/common/strutils.py b/openstack/common/strutils.py
index bbe2c92..05c178c 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):
"""Interpret a string as a boolean and return either 1 or 0.
@@ -182,3 +187,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)