From 48c1445953b12bd247e1d74dbd436d867cea7ebd Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 30 Apr 2010 08:25:54 +0200 Subject: song: use string.Template for expanding $tags. --- nephilim/common.py | 2 +- nephilim/nephilim_app.py | 2 +- nephilim/plugins/AlbumCover.py | 10 +++++----- nephilim/plugins/Lyrics.py | 12 ++++++------ nephilim/plugins/Notify.py | 4 ++-- nephilim/plugins/Systray.py | 2 +- nephilim/song.py | 12 +++++------- 7 files changed, 21 insertions(+), 23 deletions(-) diff --git a/nephilim/common.py b/nephilim/common.py index 334ced9..3bf9ff9 100644 --- a/nephilim/common.py +++ b/nephilim/common.py @@ -70,7 +70,7 @@ def expand_tags(string, expanders): string = expander.expand_tags(string) #remove unexpanded tags - return re.sub('\$\w+', '', string) + return re.sub('\$\{.*\}', '', string) def generate_metadata_path(song, dir_tag, file_tag): """Generate dirname and (db files only) full file path for reading/writing metadata files diff --git a/nephilim/nephilim_app.py b/nephilim/nephilim_app.py index 45992d7..c56c6f0 100644 --- a/nephilim/nephilim_app.py +++ b/nephilim/nephilim_app.py @@ -101,5 +101,5 @@ class NephilimApp(QtGui.QApplication): def expand_tags(self, str): ret = str - ret = ret.replace('$musicdir', self.settings.value('MPD/music_dir').toString()) + ret = ret.replace('${musicdir}', self.settings.value('MPD/music_dir').toString()) return ret diff --git a/nephilim/plugins/AlbumCover.py b/nephilim/plugins/AlbumCover.py index 21aaa8c..c462d2f 100644 --- a/nephilim/plugins/AlbumCover.py +++ b/nephilim/plugins/AlbumCover.py @@ -103,7 +103,7 @@ class AlbumCover(Plugin): o = None # private - DEFAULTS = {'coverdir' : '$musicdir/$songdir', 'covername' : '.cover_nephilim_$artist_$album', + DEFAULTS = {'coverdir' : '${musicdir}/${songdir}', 'covername' : '.cover_%s_${artist}_${album}'%common.APPNAME, 'fetchers': ['local', 'Last.fm'], 'store' : True} "implemented fetchers" available_fetchers = None @@ -165,15 +165,15 @@ class AlbumCover(Plugin): # paths to covers self.coverdir = QtGui.QLineEdit(self.settings.value('coverdir').toString()) self.coverdir.setToolTip('Where should %s store covers.\n' - '$musicdir will be expanded to path to MPD music library (as set by user)\n' - '$songdir will be expanded to path to the song (relative to $musicdir\n' + '${musicdir} will be expanded to path to MPD music library (as set by user)\n' + '${songdir} will be expanded to path to the song (relative to ${musicdir}\n' 'other tags same as in covername' %common.APPNAME) self.covername = QtGui.QLineEdit(self.settings.value('covername').toString()) self.covername.setToolTip('Filename for %s cover files.\n' 'All tags supported by MPD will be expanded to their\n' - 'values for current song, e.g. $title, $track, $artist,\n' - '$album, $genre etc.'%common.APPNAME) + 'values for current song, e.g. ${title}, ${track}, ${artist},\n' + '${album}, ${genre} etc.'%common.APPNAME) self.store.layout().addWidget(QtGui.QLabel('Cover directory'), 0, 0) self.store.layout().addWidget(self.coverdir, 0, 1) self.store.layout().addWidget(QtGui.QLabel('Cover filename'), 1, 0) diff --git a/nephilim/plugins/Lyrics.py b/nephilim/plugins/Lyrics.py index 4dc647e..d110fd3 100644 --- a/nephilim/plugins/Lyrics.py +++ b/nephilim/plugins/Lyrics.py @@ -109,8 +109,8 @@ class Lyrics(Plugin): o = None # private - DEFAULTS = {'fetchers' : ['Lyricwiki', 'Animelyrics'], 'lyricdir' : '$musicdir/$songdir', - 'lyricname' : '.lyrics_nephilim_$artist_$album_$title', 'store' : True} + DEFAULTS = {'fetchers' : ['Lyricwiki', 'Animelyrics'], 'lyricdir' : '${musicdir}/${songdir}', + 'lyricname' : '.lyrics_%s_${artist}_${album}_${title}'%common.APPNAME, 'store' : True} "implemented fetchers" available_fetchers = None #XXX SettingsWidget currently uses it "enabled fetchers, those with higher priority first" @@ -273,15 +273,15 @@ class Lyrics(Plugin): # paths to lyrics self.lyricdir = QtGui.QLineEdit(self.settings.value('lyricdir').toString()) self.lyricdir.setToolTip('Where should %s store lyrics.\n' - '$musicdir will be expanded to path to MPD music library (as set by user)\n' - '$songdir will be expanded to path to the song (relative to $musicdir\n' + '${musicdir} will be expanded to path to MPD music library (as set by user)\n' + '${songdir} will be expanded to path to the song (relative to ${musicdir}\n' 'other tags same as in lyricname' %common.APPNAME) self.lyricname = QtGui.QLineEdit(self.settings.value('lyricname').toString()) self.lyricname.setToolTip('Filename for %s lyricsfiles.\n' 'All tags supported by MPD will be expanded to their\n' - 'values for current song, e.g. $title, $track, $artist,\n' - '$album, $genre etc.'%common.APPNAME) + 'values for current song, e.g. ${title}, ${track}, ${artist},\n' + '${album}, ${genre} etc.'%common.APPNAME) self.store.layout().addWidget(QtGui.QLabel('Lyrics directory'), 0, 0) self.store.layout().addWidget(self.lyricdir, 0, 1) self.store.layout().addWidget(QtGui.QLabel('Lyrics filename'), 1, 0) diff --git a/nephilim/plugins/Notify.py b/nephilim/plugins/Notify.py index 901e936..7906861 100644 --- a/nephilim/plugins/Notify.py +++ b/nephilim/plugins/Notify.py @@ -103,7 +103,7 @@ class Notify(Plugin): # public, read-only o = None - DEFAULTS = {'songformat' : '$track - $artist - $title ($album) [$length]', + DEFAULTS = {'songformat' : '${track} - ${artist} - ${title} (${album}) [${length}]', 'timer' : 3000} def _load(self): @@ -152,7 +152,7 @@ class Notify(Plugin): self.setLayout(QtGui.QVBoxLayout()) self._add_widget(self.format, 'Format', 'Format of notifications. All tags supported by MPD\n' 'will be expanded to their values for current song,\n' - 'e.g. $track, $title, $artist, $album, $length, $date, etc.') + 'e.g. ${track}, ${title}, ${artist}, ${album}, ${length}, ${date}, etc.') self._add_widget(self.timer, 'Duration', 'How long should notifications be displayed in ms.') self.settings.endGroup() diff --git a/nephilim/plugins/Systray.py b/nephilim/plugins/Systray.py index 084ec9d..1f28597 100644 --- a/nephilim/plugins/Systray.py +++ b/nephilim/plugins/Systray.py @@ -33,7 +33,7 @@ class Systray(Plugin): # private format = None eventObj = None - DEFAULTS = {'format': '$track - $title by $artist on $album ($length)'} + DEFAULTS = {'format': '${track} - ${title} by ${artist} on ${album} (${length})'} def _load(self): self.format = self.settings.value(self.name + '/format').toString() diff --git a/nephilim/song.py b/nephilim/song.py index 6cf0a6b..cbe22d5 100644 --- a/nephilim/song.py +++ b/nephilim/song.py @@ -17,6 +17,7 @@ from PyQt4 import QtCore import os +from string import Template from common import sec2min @@ -82,13 +83,10 @@ class Song(dict): def __nonzero__(self): return bool(self['file']) - def expand_tags(self, str): - """Expands tags in form $tag in str.""" - ret = str - ret = ret.replace('$title', self['title']) #to ensure that it is set to at least filename - for tag in self.keys() + ['tracknum', 'length', 'id']: - ret = ret.replace('$' + tag, unicode(self[tag])) - ret = ret.replace('$songdir', os.path.dirname(self['file'])) + def expand_tags(self, s): + """Expands tags in form ${tag} in string s.""" + ret = Template(s) + ret = ret.safe_substitute(self) return ret class SongRef: -- cgit v1.2.3