Aiden Nibali  committed b9aeab1

Performed some janitorial work

  • Participants
  • Parent commits 829b5a0
  • Branches default

Comments (0)

Files changed (1)

 from __future__ import with_statement
 import os, re, urllib, webbrowser
-from gi.repository import Gtk, Gio, GObject, Peas
+from gi.repository import Gtk, GObject, Peas
 from gi.repository import RB
-import rb
 import LyricsParse
 from cachedProperty import cachedProperty
-#LYRIC_TITLE_STRIP=["\(live[^\)]*\)", "\(acoustic[^\)]*\)", "\([^\)]*mix\)", "\([^\)]*version\)", "\([^\)]*edit\)", "\(feat[^\)]*\)"]
 LYRIC_TITLE_REPLACE=[("/", "-"), (" & ", " and ")]
 LYRIC_ARTIST_REPLACE=[("/", "-"), (" & ", " and ")]
-#gconf_keys = {'folder': '/apps/rhythmbox/plugins/lyrcl/folder'}
 class CurrentSong(object):
     def __init__(self, db):
         self.db = db
         if not self.artist or not self.title:
             return None
-        # convert to lowercase
+        # Convert to lowercase
         artist = self.artist.lower()
         title = self.title.lower()
-        # replace ampersands and the like
+        # Replace ampersands and the like
         for exp in LYRIC_ARTIST_REPLACE:
             artist = re.sub(exp[0], exp[1], artist)
         for exp in LYRIC_TITLE_REPLACE:
             title = re.sub(exp[0], exp[1], title)
-        # compress spaces
+        # Compress spaces
         title = title.strip()
         artist = artist.strip()
         cache_path = os.path.join(artist_folder, title[:128] + '.lyric')
         return cache_path
-    def __lyric_callback_file(self, text): # came from cache file
-        if text != None:
-            # I'm forced to trust a file request (via rb.Loader().get_url()) to be fast
-            # enough not to back-up when using the Next and Previous buttons on the    
-            # Properties dialog, because the request won't pass extra stuff back AFAIK.
-            # I have yet to see a request slow enough to cause problems.
-            self.lyrics = text
-            self.callback(self.lyrics)
-        else:   # shouldn't happen; only when cache file not found (BAD!)
-            # this means that a request was made after a refresh() call but before the cache
-            # path was generated, which is stupidly unlikely.
-            print "Couldn't find cache file. THIS SHOULD NOT HAPPEN."
+    # Lyrics came from cache file
+    def __lyric_callback_file(self, text):
+        self.lyrics = text
+        self.callback(self.lyrics)
-    def __lyric_callback_web(self, text): # came from web; format is (lyrics, entry)
+    # Lyrics came from web; 'text' parameter is the tuple (lyrics, entry)
+    def __lyric_callback_web(self, text):
         if text[1] == self.entry: # request is for current entry
             if text[0]:
                 self.lyrics = text[0]
                 with open(self.cache_path, 'w') as f:
-            else: # if lyrics is None, Parse couldn't find the lyrics from the web
+            else: # lyrics are blank (ie not found)
                 self.lyrics = None
             print "Got from web. Calling back..."
     def det_lyrics(self, callback, force_refresh=False):
         print "Obtaining lyrics for %s by %s..." % (self.title, self.artist)
         self.callback = callback
+        cache_path = self.cache_path
         if not self.artist or not self.title:
             self.lyrics = None
             print "Calling back with error..."
-        elif not force_refresh and os.path.exists(self.cache_path):
-            f = file(self.cache_path, 'r')
-            self.__lyric_callback_file(
-            f.close()
+        elif not force_refresh and os.path.isfile(cache_path):
+            with open(cache_path, 'r') as f:
+                self.__lyric_callback_file(
             parser = LyricsParse.Parser(self.artist, self.title, self.entry)
-class LyricsShowyThing(object):
+class AbstractLyricPanel(object):
     Template. Subclass and define self.c_song and self.buffer
     Make sure prep_buttons is called.
         return hbox
-class LyricPane(LyricsShowyThing):
+class LyricPane(AbstractLyricPanel):
     def __init__(self, db, song_info):
         self.song_info_property = song_info.get_property
         self.c_song = CurrentSong(db)
-class LyricWindow(Gtk.Window, LyricsShowyThing):
+class LyricWindow(Gtk.Window, AbstractLyricPanel):
     def __init__(self, db):    
         super(self.__class__, self).__init__()
         self.props.border_width = 12
     def create_song_info(self, shell, song_info, is_multiple):
         if is_multiple is False:
             LyricPane(self.db, song_info)