예제 #1
0
def pattern_substitute(pattern, dic, normalize=False):
    """
    Execute path rendering/substitution based on replacement dictionary
    e.g. pattern = $Artist/$Album
         dic = {Artist: 'My artist', Album: 'My album'}
         returns My artist/My album
    """
    from headphones import pathrender
    if not pattern:
        return ''

    if normalize:
        new_dic = {}
        for i, j in dic.iteritems():
            if j is not None:
                try:
                    if sys.platform == 'darwin':
                        j = unicodedata.normalize('NFD', j)
                    else:
                        j = unicodedata.normalize('NFC', j)
                except TypeError:
                    j = unicodedata.normalize('NFC',
                        j.decode(headphones.SYS_ENCODING, 'replace'))
            new_dic[i] = j
        dic = new_dic
    return pathrender.render(pattern, dic)[0]
예제 #2
0
def replace_all(text, dic, normalize=False):
    from headphones import pathrender
    if not text:
        return ''

    if normalize:
        new_dic = {}
        for i, j in dic.iteritems():
            try:
                if sys.platform == 'darwin':
                    j = unicodedata.normalize('NFD', j)
                else:
                    j = unicodedata.normalize('NFC', j)
            except TypeError:
                j = unicodedata.normalize('NFC', j.decode(headphones.SYS_ENCODING, 'replace'))
            new_dic[i] = j
        dic = new_dic
    return pathrender.render(text, dic)[0]