pathrender.py: Add module for extended path formatting

The pattern matching is loosely based on foobar2000 pattern syntax, i.e. the
notion of escaping characters with ' and optional elements enclosed in square
brackets [] is taken from there while the substitution variable names are
Perl-ish or sh-ish. The following syntax elements are supported:
 * escaped literal strings, that is everything that is enclosed within single
   quotes (like 'this');
 * substitution variables, which start with dollar sign ($) and extend until
   next non-alphanumeric+underscore character (like $This and $5_that).
 * optional elements enclosed in square brackets, which render nonempty value
   only if any variable or optional inside returned nonempty value, ignoring
   literals (like ['['$That']' ]).
This commit is contained in:
Andrzej Ciarkowski
2016-02-05 01:55:13 +01:00
parent ffd72ed6f7
commit 7ff27a42ca
3 changed files with 236 additions and 4 deletions
+7 -4
View File
@@ -191,11 +191,13 @@ def piratesize(size):
def replace_all(text, dic, normalize=False):
from headphones import pathrender
if not text:
return ''
for i, j in dic.iteritems():
if normalize:
if normalize:
new_dic = {}
for i, j in dic.iteritems():
try:
if sys.platform == 'darwin':
j = unicodedata.normalize('NFD', j)
@@ -203,8 +205,9 @@ def replace_all(text, dic, normalize=False):
j = unicodedata.normalize('NFC', j)
except TypeError:
j = unicodedata.normalize('NFC', j.decode(headphones.SYS_ENCODING, 'replace'))
text = text.replace(i, j)
return text
new_dic[i] = j
dic = new_dic
return pathrender.render(text, dic)
def replace_illegal_chars(string, type="file"):