]> kaliko git repositories - mpd-sima.git/blobdiff - sima/lib/simadb.py
Cleanup PlayerError exception wrapper
[mpd-sima.git] / sima / lib / simadb.py
index 75119aafbd94c0d473531db32f936b26be6cff05..dddf4eb430564019db2d8c41195d658554417c8b 100644 (file)
@@ -33,9 +33,10 @@ from datetime import timezone
 
 from sima.lib.meta import Artist, Album
 from sima.lib.track import Track
+from sima.utils.utils import MPDSimaException
 
 
-class SimaDBError(Exception):
+class SimaDBError(MPDSimaException):
     """
     Exceptions.
     """
@@ -195,8 +196,8 @@ class SimaDB:
         connection = self.get_database_connection()
         rows = connection.execute(
                 "SELECT name FROM sqlite_master WHERE type='table'")
-        for r in rows.fetchall():
-            connection.execute(f'DROP TABLE IF EXISTS {r[0]}')
+        for row in rows.fetchall():
+            connection.execute(f'DROP TABLE IF EXISTS {row[0]}')
         connection.close()
 
     def _remove_blocklist_id(self, blid, with_connection=None):
@@ -217,10 +218,9 @@ class SimaDB:
             return connection.execute(
                 "SELECT id FROM albums WHERE mbid = ?",
                 (album.mbid,))
-        else:
-            return connection.execute(
-                "SELECT id FROM albums WHERE name = ? AND mbid IS NULL",
-                (album.name,))
+        return connection.execute(
+            "SELECT id FROM albums WHERE name = ? AND mbid IS NULL",
+            (album.name,))
 
     def get_album(self, album, with_connection=None, add=True):
         """get album information from the database.
@@ -260,10 +260,9 @@ class SimaDB:
             return connection.execute(
                 "SELECT id FROM albumartists WHERE mbid = ?",
                 (artist.mbid,))
-        else:
-            return connection.execute(
-                "SELECT id FROM albumartists WHERE name = ? AND mbid IS NULL",
-                (artist.name,))
+        return connection.execute(
+            "SELECT id FROM albumartists WHERE name = ? AND mbid IS NULL",
+            (artist.name,))
 
     def get_albumartist(self, artist, with_connection=None, add=True):
         """get albumartist information from the database.
@@ -302,9 +301,8 @@ class SimaDB:
             return connection.execute(
                 "SELECT id FROM artists WHERE mbid = ?",
                 (artist.mbid,))
-        else:
-            return connection.execute(
-                "SELECT id FROM artists WHERE name = ? AND mbid IS NULL", (artist.name,))
+        return connection.execute(
+            "SELECT id FROM artists WHERE name = ? AND mbid IS NULL", (artist.name,))
 
     def get_artist(self, artist, with_connection=None, add=True):
         """get artist information from the database.
@@ -375,7 +373,7 @@ class SimaDB:
         :param sima.lib.track.Track track: track to use
         :param bool add: add non existing track to database"""
         if not track.file:
-            raise SimaDBError('Got a track with no file attribute: %r' % track)
+            raise SimaDBError(f'Got a track with no file attribute: {track}')
         if with_connection:
             connection = with_connection
         else:
@@ -429,7 +427,7 @@ class SimaDB:
 
     def _add_tracks_genres(self, track, connection):
         if not track.genres:
-            return None
+            return
         rows = connection.execute(
             "SELECT id FROM tracks WHERE file = ?", (track.file,))
         trk_id = rows.fetchone()[0]
@@ -491,7 +489,7 @@ class SimaDB:
                 LEFT OUTER JOIN albumartists ON tracks.albumartist = albumartists.id
                 WHERE history.last_play > ? AND albums.name NOT NULL AND artists.name NOT NULL
                 ORDER BY history.last_play DESC""", (date.isoformat(' '),))
-        hist = list()
+        hist = []
         for row in rows:
             vals = dict(row)
             if needle:  # Here use artist instead of albumartist
@@ -532,7 +530,7 @@ class SimaDB:
                 WHERE history.last_play > ? AND artists.name NOT NULL
                 ORDER BY history.last_play DESC""", (date.isoformat(' '),))
         last = deque(maxlen=1)
-        hist = list()
+        hist = []
         for row in rows:
             artist = Artist(**row)
             if last and last[0] == artist:  # remove consecutive dupes
@@ -543,7 +541,7 @@ class SimaDB:
                     hist.append(artist)  # No need to go further
                     break
                 continue
-            elif needle and getattr(needle, '__contains__'):
+            if needle and getattr(needle, '__contains__'):
                 if artist in needle:
                     hist.append(artist)  # No need to go further
                 continue
@@ -569,7 +567,7 @@ class SimaDB:
                 WHERE history.last_play > ? AND genres.name NOT NULL
                 ORDER BY history.last_play DESC
                 """, (date.isoformat(' '),))
-        genres = list()
+        genres = []
         for row in rows:
             genres.append(row)
             if len({g[0] for g in genres}) >= limit:
@@ -614,7 +612,7 @@ class SimaDB:
         else:
             rows = connection.execute(sql+'ORDER BY history.last_play DESC',
                                       (date.isoformat(' '),))
-        hist = list()
+        hist = []
         for row in rows:
             hist.append(Track(**row))
         connection.close()
@@ -643,10 +641,10 @@ class SimaDB:
             connection.commit()
         rows = connection.execute(
             "SELECT id FROM blocklist WHERE track = ?", (track_id,))
-        bl = rows.fetchone()[0]
+        blt = rows.fetchone()[0]
         if not with_connection:
             connection.close()
-        return bl
+        return blt
 
     def get_bl_album(self, album, with_connection=None, add=True):
         """Add an album to blocklist
@@ -671,10 +669,10 @@ class SimaDB:
             connection.commit()
         rows = connection.execute(
             "SELECT id FROM blocklist WHERE album = ?", (album_id,))
-        bl = rows.fetchone()[0]
+        blitem = rows.fetchone()[0]
         if not with_connection:
             connection.close()
-        return bl
+        return blitem
 
     def get_bl_artist(self, artist, with_connection=None, add=True):
         """Add an artist to blocklist
@@ -697,10 +695,10 @@ class SimaDB:
             connection.commit()
         rows = connection.execute(
             "SELECT id FROM blocklist WHERE artist = ?", (artist_id,))
-        bl = rows.fetchone()[0]
+        blitem = rows.fetchone()[0]
         if not with_connection:
             connection.close()
-        return bl
+        return blitem
 
     def view_bl(self):
         connection = self.get_database_connection()