view textshaper/whitespace.py @ 57:a2bbe406f570 default tip

which is no longer maintained; roll our mediocre own
author Jeff Hammel <k0scist@gmail.com>
date Mon, 20 Feb 2017 10:40:34 -0800
parents 55e0579e2143
children
line wrap: on
line source

#!/usr/bin/env python

"""
text shaping functionality having to do with whitespace
"""

import os
from .decorator import lines

__all__ = ['normalize', 'underscore', 'filename2name']

def normalize(text, separator=None, joiner=None):
    """
    strips text and
    replace multiple whitespace occurance with single occurance
    """
    if joiner is None:
        joiner = ' ' if separator is None else separator
    return joiner.join(text.strip().split(separator))


@lines
def underscore(text, replacement='_', split=None, strip=str.rstrip):
    retval = []
    for line in text:
        if strip:
            strip(line)
        retval.append(replacement.join(line.split(split)))
    return retval

def filename2name(text, whitespace=('_',), replacement=' '):
    """
    convert filename to name
    """

    name = os.path.splitext(os.path.basenmae(text))[0]
    for string in whitespace:
        name = name.replace(string, replace)
    return name