Fix helpers.replace_all

- Renamed post processing replace_all to pattern_substitute
- Resurrected replace_all
This commit is contained in:
Ade
2018-08-17 18:43:43 +12:00
parent 693e5b1624
commit 0b2600b783
4 changed files with 24 additions and 9 deletions

View File

@@ -197,9 +197,15 @@ def piratesize(size):
return size
def replace_all(text, dic, normalize=False):
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 text:
if not pattern:
return ''
if normalize:
@@ -216,7 +222,16 @@ def replace_all(text, dic, normalize=False):
j.decode(headphones.SYS_ENCODING, 'replace'))
new_dic[i] = j
dic = new_dic
return pathrender.render(text, dic)[0]
return pathrender.render(pattern, dic)[0]
def replace_all(text, dic):
if not text:
return ''
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
def replace_illegal_chars(string, type="file"):

View File

@@ -168,8 +168,8 @@ class MetadataTest(TestCase):
if not _h.SYS_ENCODING:
_h.SYS_ENCODING = 'UTF-8'
res = _hp.replace_all(
res = _hp.pattern_substitute(
"/music/$First/$Artist/$Artist - $Album{ [$Year]}", md, True)
self.assertEqual(res, u"/music/A/artist/artist - Album",
"check correct rendering of None via replace_all()")
"check correct rendering of None via pattern_substitute()")

View File

@@ -660,7 +660,7 @@ def addAlbumArt(artwork, albumpath, release, metadata_dict):
if artwork[:4] == '\x89PNG':
ext = ".png"
album_art_name = helpers.replace_all(
album_art_name = helpers.pattern_substitute(
headphones.CONFIG.ALBUM_ART_FORMAT.strip(), md) + ext
album_art_name = helpers.replace_illegal_chars(album_art_name).encode(
@@ -716,7 +716,7 @@ def moveFiles(albumpath, release, metadata_dict):
logger.info("Moving files: %s" % albumpath)
md = metadata.album_metadata(albumpath, release, metadata_dict)
folder = helpers.replace_all(
folder = helpers.pattern_substitute(
headphones.CONFIG.FOLDER_FORMAT.strip(), md, normalize=True)
if headphones.CONFIG.FILE_UNDERSCORES:
@@ -1095,7 +1095,7 @@ def renameFiles(albumpath, downloaded_track_list, release):
title = md[metadata.Vars.TITLE]
new_file_name = helpers.cleanTitle(title) + ext
else:
new_file_name = helpers.replace_all(
new_file_name = helpers.pattern_substitute(
headphones.CONFIG.FILE_FORMAT.strip(), md
).replace('/', '_') + ext

View File

@@ -326,7 +326,7 @@ class WebInterface(object):
'$first': firstchar.lower(),
}
folder = helpers.replace_all(folder_format.strip(), values, normalize=True)
folder = helpers.pattern_substitute(folder_format.strip(), values, normalize=True)
folder = helpers.replace_illegal_chars(folder, type="folder")
folder = folder.replace('./', '_/').replace('/.', '/_')