diff --git a/src/execo_engine/utils.py b/src/execo_engine/utils.py index 9e34df0b7e6ae0fba4b2cec2f7bcfbae964b4979..c7fa4539686cec3c8079c77f1c370d4c401a7d8d 100644 --- a/src/execo_engine/utils.py +++ b/src/execo_engine/utils.py @@ -89,9 +89,8 @@ def copy_outputs(stdout_filename, stderr_filename): # and 0 as the buffer size (unbuffered) sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1 if sys.version_info >= (3,) else 0) -def slugify(value): - """ - Normalizes string representation, converts to lowercase, removes +def slugify(value, separator='-'): + """Normalizes string representation, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens. Intended to convert any object having a relevant string @@ -99,15 +98,22 @@ def slugify(value): more or less inspired / copy pasted from django (see http://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename-in-python) + + :param value: the object to slugify + + :param separator: a character to use as separator in the string + output, instead of the default '-'. Be careful to only use one + character and only characters which don't have special meaning + in regular expressions. """ if sys.version_info >= (3,): value = str(value) value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') value = re.sub('[^\w\s-]', '', value).strip().lower() - value = re.sub('[-\s]+', '-', value) + value = re.sub('[' + separator + '\s]+', separator, value) else: value = unicode(str(value)) value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') value = unicode(re.sub('[^\w\s-]', '', value).strip().lower()) - value = unicode(re.sub('[-\s]+', '-', value)) + value = unicode(re.sub('[' + separator + '\s]+', separator, value)) return value