Permalink
Browse files

lyrics: rewrite the lyrics location function to make it clearer

  • Loading branch information...
1 parent c4c7e78 commit 7d3017cba54f4aa5de1420941fceee7ac0d4d122 @multani committed Jan 25, 2011
Showing with 29 additions and 21 deletions.
  1. +29 −21 sonata/info.py
View
@@ -329,10 +329,11 @@ def _update_lyrics(self, songinfo):
'not set.')))
def _check_for_local_lyrics(self, artist, title, song_dir):
- locations = [consts.LYRICS_LOCATION_HOME,
- consts.LYRICS_LOCATION_PATH,
- consts.LYRICS_LOCATION_HOME_ALT,
- consts.LYRICS_LOCATION_PATH_ALT]
+ locations = [
+ consts.LYRICS_LOCATION_HOME,
+ consts.LYRICS_LOCATION_PATH,
+ consts.LYRICS_LOCATION_HOME_ALT,
+ consts.LYRICS_LOCATION_PATH_ALT]
for location in locations:
filename = self.target_lyrics_filename(artist, title,
song_dir, location)
@@ -510,24 +511,31 @@ def resize_elements(self, notebook_allocation):
def target_lyrics_filename(self, artist, title, song_dir,
force_location=None):
+ """get the filename of the lyrics of a song"""
+
+ cfg = self.config # alias for easier access
+
# FIXME Why did we have this condition here: if self.conn:
- lyrics_loc = force_location if force_location else \
- self.config.lyrics_location
- # Note: *_ALT searching is for compatibility with other mpd clients
- # (like ncmpcpp):
+ lyrics_loc = force_location if force_location else cfg.lyrics_location
+
if song_dir is not None:
song_dir.replace('%', '%%')
+
+ music_dir = cfg.musicdir[cfg.profile_num].replace('%', '%%')
+ pattern1 = "%s-%s.txt"
+ pattern2 = "%s - %s.txt"
+
+ # Note: *_ALT searching is for compatibility with other mpd clients
+ # (like ncmpcpp):
file_map = {
- consts.LYRICS_LOCATION_HOME: ("~/.lyrics", "%s-%s.txt"),
- consts.LYRICS_LOCATION_PATH:
- (self.config.musicdir[self.config.profile_num].replace('%', '%%'),
- song_dir, "%s-%s.txt"),
- consts.LYRICS_LOCATION_HOME_ALT: ("~/.lyrics", "%s - %s.txt"),
- consts.LYRICS_LOCATION_PATH_ALT:
- (self.config.musicdir[self.config.profile_num].replace('%', '%%'),
- song_dir, "%s - %s.txt"),
- }
- return misc.file_from_utf8(misc.file_exists_insensitive(
- os.path.expanduser(
- os.path.join(*file_map[lyrics_loc]))
- % (artist, title)))
+ consts.LYRICS_LOCATION_HOME: ("~/.lyrics", pattern1),
+ consts.LYRICS_LOCATION_PATH: (music_dir, song_dir, pattern1),
+ consts.LYRICS_LOCATION_HOME_ALT: ("~/.lyrics", pattern2),
+ consts.LYRICS_LOCATION_PATH_ALT: (music_dir, song_dir, pattern2),
+ }
+
+ file_path = os.path.join(*file_map[lyrics_loc])
+ file_path = os.path.expanduser(file_path) % (artist, title)
+
+ return misc.file_from_utf8(
+ misc.file_exists_insensitive(file_path))

0 comments on commit 7d3017c

Please sign in to comment.