diff options
author | Michaël Ball <michael.ball@gmail.com> | 2019-06-30 11:12:05 +0100 |
---|---|---|
committer | Michaël Ball <michael.ball@gmail.com> | 2019-06-30 11:12:05 +0100 |
commit | a87517a0b385f1a68dd80e7dabf7843955c95ab9 (patch) | |
tree | 696f1e2a45ab7c0db57ce562517790cb73247b53 | |
parent | 1acbc1e85d6a0af47c236c48b8c5a1b504871d96 (diff) |
Style fixesfeature/pluggable-viewsdevelop
-rw-r--r-- | app.py | 1 | ||||
-rw-r--r-- | common/security.py | 2 | ||||
-rw-r--r-- | common/utils.py | 8 | ||||
-rw-r--r-- | db/db_manager.py | 17 | ||||
-rwxr-xr-x | library.py | 3 | ||||
-rw-r--r-- | mach2.py | 58 | ||||
-rw-r--r-- | models/album.py | 66 | ||||
-rw-r--r-- | models/artist.py | 63 | ||||
-rw-r--r-- | models/base.py | 1 | ||||
-rw-r--r-- | models/track.py | 337 | ||||
-rw-r--r-- | watcher.py | 12 |
11 files changed, 328 insertions, 240 deletions
@@ -11,7 +11,6 @@ from watcher import LibraryWatcher # NOQA : E402 APP = create_app() - if __name__ == "__main__": config = configparser.ConfigParser() config.read("mach2.ini") diff --git a/common/security.py b/common/security.py index e3ab5de..d021fcf 100644 --- a/common/security.py +++ b/common/security.py @@ -17,4 +17,4 @@ pwd_context = CryptContext( # (appropriate values may vary for different schemes, # and the amount of time you wish it to take) pbkdf2_sha256__default_rounds=8000, - ) +) diff --git a/common/utils.py b/common/utils.py index efac527..f1134a7 100644 --- a/common/utils.py +++ b/common/utils.py @@ -15,11 +15,11 @@ def make_where_clause(params, join_operator="AND"): condition_subphrase = "" if operator == "BETWEEN": - condition_subphrase = " ".join(("%s", operator, - ":%s1 AND :%s2")) + condition_subphrase = " ".join( + ("%s", operator, ":%s1 AND :%s2")) - where_items.append(condition_subphrase % (column, column, - column)) + where_items.append(condition_subphrase % + (column, column, column)) else: condition_subphrase = " ".join(("%s", operator, ":%s")) diff --git a/db/db_manager.py b/db/db_manager.py index 9c2dd53..2684fb3 100644 --- a/db/db_manager.py +++ b/db/db_manager.py @@ -8,9 +8,9 @@ import sqlite3 import six - _LOGGER = logging.getLogger(__name__) + class DbManager(object): """DBManager makes interacting with sqlite databases easier.""" @@ -72,16 +72,17 @@ class DbManager(object): yield six.u("{0};").format(sql) table_name_ident = table_name.replace("\"", "\"\"") - res = cursor.execute("PRAGMA table_info(\"{0}\")".format( - table_name_ident)) + res = cursor.execute( + "PRAGMA table_info(\"{0}\")".format(table_name_ident)) column_names = [ - str(table_info[1]) for table_info in res.fetchall()] + str(table_info[1]) for table_info in res.fetchall() + ] query = """ SELECT 'INSERT INTO "{0}" VALUES({1})' FROM "{0}"; - """.format(table_name_ident, ",".join( - """'||quote("{0}")||'""".format( - col.replace( - "\"", "\"\"")) for col in column_names)) + """.format( + table_name_ident, ",".join( + """'||quote("{0}")||'""".format(col.replace("\"", "\"\"")) + for col in column_names)) query_res = cursor.execute(query) for row in query_res: yield six.u("{0};").format(row[0]) @@ -12,14 +12,15 @@ from six.moves import configparser, range from db.db_manager import DbManager from models.track import Track - logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG) _LOGGER = logging.getLogger(__name__) if six.PY2: + def _u(string): return unicode(string, encoding="utf_8") else: + def _u(string): return string @@ -20,7 +20,6 @@ from models.artist import Artist from models.track import Track from models.user import User - _CONFIG = configparser.ConfigParser() _CONFIG.read("mach2.ini") @@ -81,8 +80,8 @@ def load_user_from_request(req): if api_key: user = None - result = query_db("SELECT * FROM user WHERE api_key = ?", - [api_key], one=True) + result = query_db("SELECT * FROM user WHERE api_key = ?", [api_key], + one=True) if result: user = User(id=result[0], @@ -195,8 +194,10 @@ def album_search(album_name): result_albums = [] for returned_album in Album.search(current_app.config["LIBRARY"], - name={"data": album_name, - "operator": "LIKE"}): + name={ + "data": album_name, + "operator": "LIKE" + }): result_albums.append(returned_album.as_dict()) return jsonify(result_albums) @@ -227,10 +228,12 @@ def artists(): if order_by: returned_artists = Artist.all(current_app.config["LIBRARY"], order=order_by, - direction=order_direction, limit=lim, + direction=order_direction, + limit=lim, offset=off) else: - returned_artists = Artist.all(current_app.config["LIBRARY"], limit=lim, + returned_artists = Artist.all(current_app.config["LIBRARY"], + limit=lim, offset=off) for returned_artist in returned_artists: @@ -279,7 +282,7 @@ def artist_search(artist_name): name={ "data": artist_name, "operator": "LIKE" - }): + }): result_artists.append(artist.as_dict()) return jsonify(artists) @@ -309,11 +312,14 @@ def tracks(): if order_by: returned_tracks = Track.all(current_app.config["LIBRARY"], - order=order_by, direction=order_direction, - limit=lim, offset=off) + order=order_by, + direction=order_direction, + limit=lim, + offset=off) else: returned_tracks = Track.all(current_app.config["LIBRARY"], - limit=lim, offset=off) + limit=lim, + offset=off) for returned_track in returned_tracks: result_tracks.append(returned_track.as_dict()) @@ -381,14 +387,17 @@ def track(track_id): def track_search(track_name): result_tracks = [] for returned_track in Track.search(current_app.config["LIBRARY"], - name={"data": track_name, - "operator": "LIKE"}): + name={ + "data": track_name, + "operator": "LIKE" + }): result_tracks.append(returned_track.as_dict()) return jsonify(result_tracks) -@MACH2.route("/user", defaults={"user_id": None}, +@MACH2.route("/user", + defaults={"user_id": None}, methods=["GET", "POST", "PUT"]) @MACH2.route("/user/<int:user_id>", methods=["DELETE", "GET", "PUT"]) @login_required @@ -401,6 +410,7 @@ def users(user_id): on the currently logged in user. """ + def update_user(user, user_data): """Update the user with the supplied data. @@ -411,8 +421,7 @@ def users(user_id): """ db_conn = get_db() if "password" in user_data: - password_hash, api_key = user.new_password( - user_data["password"]) + password_hash, api_key = user.new_password(user_data["password"]) update_query = ("UPDATE user SET password_hash = ?, " "api_key = ? WHERE id = ?") @@ -441,8 +450,7 @@ def users(user_id): update_query, (user_data["transcode_command"], user.id)) if rows_updated > 0: - user.transcode_command = user_data[ - "transcode_command"] + user.transcode_command = user_data["transcode_command"] else: error = dict(message="Unable to update user") return jsonify(error), 500 @@ -499,10 +507,13 @@ def load_user(user_id): result = query_db("SELECT * FROM user WHERE id = ?", [user_id], one=True) if result: - local_user = User( - id=result[0], username=result[1],password_hash=result[2], - authenticated=1, active=result[4], anonymous=0, - transcode_command=result[7]) + local_user = User(id=result[0], + username=result[1], + password_hash=result[2], + authenticated=1, + active=result[4], + anonymous=0, + transcode_command=result[7]) return local_user @@ -512,7 +523,8 @@ def login(): if request.method == "POST": user = None result = query_db("SELECT * FROM user WHERE username = ?", - [request.form["username"]], one=True) + [request.form["username"]], + one=True) if result: user = User(id=result[0], username=result[1], diff --git a/models/album.py b/models/album.py index d55fb88..b7c3548 100644 --- a/models/album.py +++ b/models/album.py @@ -10,7 +10,7 @@ class Album(BaseModel): if id is not None: for row in self._db.execute("SELECT * FROM album WHERE id = ?", - (id,)): + (id, )): setattr(self, "id", id) setattr(self, "name", row[1]) setattr(self, "date", row[2]) @@ -24,13 +24,13 @@ class Album(BaseModel): with self._db.conn: delete_album = "DELETE FROM album WHERE id = ?" - self._db.execute(delete_album, (self.id,)) + self._db.execute(delete_album, (self.id, )) delete_track_rel = "DELETE FROM album_track WHERE album_id = ?" - self._db.execute(delete_track_rel, (self.id,)) + self._db.execute(delete_track_rel, (self.id, )) delete_artist_rel = "DELETE FROM album_artist WHERE album_id = ?" - self._db.execute(delete_artist_rel, (self.id,)) + self._db.execute(delete_artist_rel, (self.id, )) return True @@ -49,13 +49,16 @@ class Album(BaseModel): if not hasattr(self, "_artists"): setattr(self, "_artists", []) - for row in self._db.execute("SELECT artist.* FROM artist INNER " - "JOIN album_artist ON artist.id = " - "album_artist.artist_id WHERE " - "album_id = ? ORDER BY name ASC", - (self.id,)): - artist = Artist(id=row[0], db=self._db, name=row[1], - sortname=row[2], musicbrainz_artistid=row[3]) + for row in self._db.execute( + "SELECT artist.* FROM artist INNER " + "JOIN album_artist ON artist.id = " + "album_artist.artist_id WHERE " + "album_id = ? ORDER BY name ASC", (self.id, )): + artist = Artist(id=row[0], + db=self._db, + name=row[1], + sortname=row[2], + musicbrainz_artistid=row[3]) self._artists.append(artist) return self._artists @@ -67,15 +70,18 @@ class Album(BaseModel): if not hasattr(self, "_tracks"): setattr(self, "_tracks", []) - for row in self._db.execute("SELECT track.* FROM track INNER " - "JOIN album_track ON track.id = " - "album_track.track_id WHERE " - "album_id = ? ORDER BY tracknumber " - "ASC", (self.id,)): + for row in self._db.execute( + "SELECT track.* FROM track INNER " + "JOIN album_track ON track.id = " + "album_track.track_id WHERE " + "album_id = ? ORDER BY tracknumber " + "ASC", (self.id, )): - track = Track(id=row["id"], db=self._db, + track = Track(id=row["id"], + db=self._db, tracknumber=row["tracknumber"], - name=row["name"], grouping=row["grouping"], + name=row["name"], + grouping=row["grouping"], filename=row["filename"]) self._tracks.append(track) @@ -135,14 +141,19 @@ class Album(BaseModel): for row in result: albums.append( - Album(id=row["id"], db=database, name=row["name"], - date=row["date"]) - ) + Album(id=row["id"], + db=database, + name=row["name"], + date=row["date"])) return albums @classmethod - def all(cls, database, order="album.id", direction="ASC", limit=None, + def all(cls, + database, + order="album.id", + direction="ASC", + limit=None, offset=None): albums = [] @@ -153,15 +164,16 @@ class Album(BaseModel): direction) if limit is not None and offset is not None: - select_string = " ".join((select_string, - "LIMIT %s OFFSET %s" % (limit, offset))) + select_string = " ".join( + (select_string, "LIMIT %s OFFSET %s" % (limit, offset))) result = database.execute(select_string) for row in result: albums.append( - Album(id=row["id"], db=database, name=row["name"], - date=row["date"]) - ) + Album(id=row["id"], + db=database, + name=row["name"], + date=row["date"])) return albums diff --git a/models/artist.py b/models/artist.py index dada665..e91e66e 100644 --- a/models/artist.py +++ b/models/artist.py @@ -3,13 +3,12 @@ from models.base import BaseModel class Artist(BaseModel): - def __init__(self, db, id=None, **kwargs): self._db = db if id is not None: for row in self._db.execute("SELECT * FROM artist WHERE id = ?", - (id,)): + (id, )): for key in ["id", "name", "sortname", "musicbrainz_artistid"]: setattr(self, key, row[key]) else: @@ -22,13 +21,13 @@ class Artist(BaseModel): with self._db.conn: delete_artist = "DELETE FROM artist WHERE id = ?" - self._db.execute(delete_artist, (self.id,)) + self._db.execute(delete_artist, (self.id, )) delete_track_rel = "DELETE FROM artist_track WHERE artist_id = ?" - self._db.execute(delete_track_rel, (self.id,)) + self._db.execute(delete_track_rel, (self.id, )) delete_album_rel = "DELETE FROM album_artist WHERE artist_id = ?" - self._db.execute(delete_album_rel, (self.id,)) + self._db.execute(delete_album_rel, (self.id, )) return True @@ -47,14 +46,16 @@ class Artist(BaseModel): if not hasattr(self, "_tracks"): setattr(self, "_tracks", []) - for row in self._db.execute("SELECT track.* FROM track INNER " - "JOIN artist_track ON track.id = " - "artist_track.track_id WHERE " - "artist_id = ? ORDER BY name ASC", - (self.id,)): + for row in self._db.execute( + "SELECT track.* FROM track INNER " + "JOIN artist_track ON track.id = " + "artist_track.track_id WHERE " + "artist_id = ? ORDER BY name ASC", (self.id, )): - track = Track(id=row["id"], db=self._db, - tracknumber=row["tracknumber"], name=row["name"], + track = Track(id=row["id"], + db=self._db, + tracknumber=row["tracknumber"], + name=row["name"], grouping=row["grouping"], filename=row["filename"]) self._tracks.append(track) @@ -68,12 +69,14 @@ class Artist(BaseModel): if not hasattr(self, "_albums"): setattr(self, "_albums", []) - for row in self._db.execute("SELECT album.* FROM album INNER " - "JOIN album_artist ON album.id = " - "album_artist.album_id WHERE " - "artist_id = ? ORDER BY date ASC", - (self.id,)): - album = Album(id=row["id"], db=self._db, name=row["name"], + for row in self._db.execute( + "SELECT album.* FROM album INNER " + "JOIN album_artist ON album.id = " + "album_artist.album_id WHERE " + "artist_id = ? ORDER BY date ASC", (self.id, )): + album = Album(id=row["id"], + db=self._db, + name=row["name"], date=row["date"]) self._albums.append(album) @@ -126,15 +129,20 @@ class Artist(BaseModel): for row in result: artists.append( - Artist(id=row["id"], db=database, name=row["name"], + Artist(id=row["id"], + db=database, + name=row["name"], sortname=row["sortname"], - musicbrainz_artistid=row["musicbrainz_artistid"]) - ) + musicbrainz_artistid=row["musicbrainz_artistid"])) return artists @classmethod - def all(cls, database, order="sortname", direction="ASC", limit=None, + def all(cls, + database, + order="sortname", + direction="ASC", + limit=None, offset=None): artists = [] @@ -143,16 +151,17 @@ class Artist(BaseModel): direction) if limit is not None and offset is not None: - select_string = " ".join((select_string, - "LIMIT %s OFFSET %s" % (limit, offset))) + select_string = " ".join( + (select_string, "LIMIT %s OFFSET %s" % (limit, offset))) result = database.execute(select_string) for row in result: artists.append( - Artist(id=row["id"], db=database, name=row["name"], + Artist(id=row["id"], + db=database, + name=row["name"], sortname=row["sortname"], - musicbrainz_artistid=row["musicbrainz_artistid"]) - ) + musicbrainz_artistid=row["musicbrainz_artistid"])) return artists diff --git a/models/base.py b/models/base.py index 8a6fc47..9ccec08 100644 --- a/models/base.py +++ b/models/base.py @@ -1,6 +1,7 @@ """Implements a base model for other models to inherit.""" from six import iteritems + class BaseModel(object): """BaseModel is meant to be inherited by other models.""" diff --git a/models/track.py b/models/track.py index 2a92558..81c60fc 100644 --- a/models/track.py +++ b/models/track.py @@ -6,12 +6,10 @@ from models.artist import Artist from models.album import Album from models.base import BaseModel - logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG) class Track(BaseModel): - def __init__(self, db, id=None, **kwargs): self._db = db @@ -19,9 +17,10 @@ class Track(BaseModel): if id is not None: for row in self._db.execute("SELECT * FROM track WHERE id = ?", - (id,)): - for key in ["id", "tracknumber", "name", "grouping", - "filename"]: + (id, )): + for key in [ + "id", "tracknumber", "name", "grouping", "filename" + ]: setattr(self, key, row[key]) self.__data[key] = row[key] else: @@ -33,7 +32,7 @@ class Track(BaseModel): delete_sql = "DELETE FROM track WHERE id = ?" with self._db.conn: - self._db.execute(delete_sql, (self.id,)) + self._db.execute(delete_sql, (self.id, )) # If there is an old album, remove it if it no longer has any # tracks @@ -46,7 +45,7 @@ class Track(BaseModel): if old_album: self._db.execute("DELETE FROM album_track WHERE track_id = ?", - (self.id,)) + (self.id, )) if not old_album.tracks: old_album.delete() @@ -60,8 +59,9 @@ class Track(BaseModel): old_artists = self.artists for old_artist in old_artists: - self._db.execute("DELETE FROM artist_track WHERE track_id = " - "?", (self.id,)) + self._db.execute( + "DELETE FROM artist_track WHERE track_id = " + "?", (self.id, )) if not old_artist.tracks: old_artist.delete() @@ -72,7 +72,6 @@ class Track(BaseModel): def db(self): return self._db - @db.setter def db(self, db): self._db = db @@ -82,13 +81,17 @@ class Track(BaseModel): if not hasattr(self, "_album"): setattr(self, "_album", None) - for row in self._db.execute("SELECT album.* FROM album INNER " - "JOIN album_track ON album.id = " - "album_track.album_id WHERE " - "track_id = ? LIMIT 1", (self.id,)): - setattr(self, "_album", Album(id=row["id"], db=self._db, - name=row["name"], - date=row["date"])) + for row in self._db.execute( + "SELECT album.* FROM album INNER " + "JOIN album_track ON album.id = " + "album_track.album_id WHERE " + "track_id = ? LIMIT 1", (self.id, )): + setattr( + self, "_album", + Album(id=row["id"], + db=self._db, + name=row["name"], + date=row["date"])) return self._album @@ -98,16 +101,17 @@ class Track(BaseModel): cursor = self._db.cursor() setattr(self, "_artists", []) - for row in cursor.execute("SELECT artist.* FROM artist INNER JOIN " - "artist_track ON artist.id = " - "artist_track.artist_id WHERE " - "artist_track.track_id = ?", - (self.id,)): - self._artists.append(Artist(id=row["id"], db=self._db, - name=row["name"], - sortname=row["sortname"], - musicbrainz_artistid=row[ - "musicbrainz_artistid"])) + for row in cursor.execute( + "SELECT artist.* FROM artist INNER JOIN " + "artist_track ON artist.id = " + "artist_track.artist_id WHERE " + "artist_track.track_id = ?", (self.id, )): + self._artists.append( + Artist(id=row["id"], + db=self._db, + name=row["name"], + sortname=row["sortname"], + musicbrainz_artistid=row["musicbrainz_artistid"])) return self._artists @@ -149,20 +153,22 @@ class Track(BaseModel): rows = None if musicbrainz_artistid: - rows = c.execute("SELECT * FROM artist WHERE " - "musicbrainz_artistid = ?", - (musicbrainz_artistid,)) + rows = c.execute( + "SELECT * FROM artist WHERE " + "musicbrainz_artistid = ?", (musicbrainz_artistid, )) else: rows = c.execute("SELECT * FROM artist WHERE name = ?", - (artist_name,)) + (artist_name, )) row = rows.fetchone() if row: - artist = Artist(id=row["id"], db=self._db, name=row["name"], - sortname=row["sortname"], - musicbrainz_artistid=row[ - "musicbrainz_artistid"]) + artist = Artist( + id=row["id"], + db=self._db, + name=row["name"], + sortname=row["sortname"], + musicbrainz_artistid=row["musicbrainz_artistid"]) if artist.name != artist_name: c.execute("UPDATE artist SET name = ? WHERE id = ?", @@ -175,15 +181,16 @@ class Track(BaseModel): artist.sortname = artistsort else: - c.execute("INSERT INTO artist (name, sortname, " - "musicbrainz_artistid) VALUES(?, ?, ?)", - (artist_name, artistsort, musicbrainz_artistid)) + c.execute( + "INSERT INTO artist (name, sortname, " + "musicbrainz_artistid) VALUES(?, ?, ?)", + (artist_name, artistsort, musicbrainz_artistid)) - artist = Artist( - id=c.lastrowid, db=self._db, name=artist_name, - sortname=artistsort, - musicbrainz_artistid=musicbrainz_artistid - ) + artist = Artist(id=c.lastrowid, + db=self._db, + name=artist_name, + sortname=artistsort, + musicbrainz_artistid=musicbrainz_artistid) i += 1 @@ -211,40 +218,48 @@ class Track(BaseModel): if mb_albumid: rows = c.execute( "SELECT * FROM album WHERE musicbrainz_albumid = ?", - (mb_albumid,) - ) + (mb_albumid, )) row = rows.fetchone() if row: - album = Album(id=row["id"], db=self._db, name=row["name"], + album = Album(id=row["id"], + db=self._db, + name=row["name"], date=row["date"], musicbrainz_albumid=row["musicbrainz_albumid"]) else: - c.execute("INSERT INTO album (name, `date`, " - "musicbrainz_albumid) VALUES (?, ?, ?)", - (album_name, album_date, mb_albumid)) + c.execute( + "INSERT INTO album (name, `date`, " + "musicbrainz_albumid) VALUES (?, ?, ?)", + (album_name, album_date, mb_albumid)) - album = Album(id=c.lastrowid, db=self._db, name=album_name, - date=album_date, musicbrainz_albumid=mb_albumid) + album = Album(id=c.lastrowid, + db=self._db, + name=album_name, + date=album_date, + musicbrainz_albumid=mb_albumid) elif album_name: rows = c.execute( "SELECT album.* FROM album INNER JOIN album_artist ON " "album_artist.album_id = album.id WHERE album.name = ? " - "AND artist_id = ?", (album_name, artist.id) - ) + "AND artist_id = ?", (album_name, artist.id)) row = rows.fetchone() if row: - album = Album(id=row["id"], db=self._db, name=row["name"], + album = Album(id=row["id"], + db=self._db, + name=row["name"], date=row["date"], musicbrainz_albumid=row["musicbrainz_albumid"]) else: c.execute("INSERT INTO album (name, `date`) VALUES (?, ?)", (album_name, album_date)) - album = Album(id=c.lastrowid, db=self._db, name=album_name, + album = Album(id=c.lastrowid, + db=self._db, + name=album_name, date=album_date) if album: @@ -277,9 +292,10 @@ class Track(BaseModel): except KeyError: pass - c.execute("UPDATE track SET tracknumber = ?, name = ?, grouping = ? " - "WHERE id = ?", (track_number, track_name, track_grouping, - self.id)) + c.execute( + "UPDATE track SET tracknumber = ?, name = ?, grouping = ? " + "WHERE id = ?", + (track_number, track_name, track_grouping, self.id)) # If there is an old album, remove it if it no longer has any tracks try: @@ -290,7 +306,7 @@ class Track(BaseModel): old_album = self.album if old_album: c.execute("DELETE FROM album_track WHERE track_id = ?", - (self.id,)) + (self.id, )) # If there are old artists, remove them if they no longer have # any tracks @@ -302,12 +318,13 @@ class Track(BaseModel): for old_artist in old_artists: c.execute("DELETE FROM artist_track WHERE track_id = ?", - (self.id,)) + (self.id, )) if album: try: - c.execute("INSERT INTO album_track (album_id, track_id) " - "VALUES(?, ?)", (album.id, self.id)) + c.execute( + "INSERT INTO album_track (album_id, track_id) " + "VALUES(?, ?)", (album.id, self.id)) except sqlite3.IntegrityError: pass @@ -318,8 +335,9 @@ class Track(BaseModel): for artist in artists: try: - c.execute("INSERT INTO artist_track (artist_id, track_id) " - "VALUES(?, ?)", (artist.id, self.id)) + c.execute( + "INSERT INTO artist_track (artist_id, track_id) " + "VALUES(?, ?)", (artist.id, self.id)) except sqlite3.IntegrityError: pass @@ -395,10 +413,12 @@ class Track(BaseModel): for row in result: tracks.append( - Track(id=row["id"], db=database, tracknumber=row["tracknumber"], - name=row["name"], grouping=row["grouping"], - filename=row["filename"]) - ) + Track(id=row["id"], + db=database, + tracknumber=row["tracknumber"], + name=row["name"], + grouping=row["grouping"], + filename=row["filename"])) return tracks @@ -406,11 +426,15 @@ class Track(BaseModel): def find_by_path(cls, path, database): track = None - for row in database.execute("SELECT * FROM track WHERE filename = ? " - "LIMIT 1", (path,)): - track = Track(id=row["id"], db=database, - tracknumber=row["tracknumber"], name=row["name"], - grouping=row["grouping"], filename=row["filename"]) + for row in database.execute( + "SELECT * FROM track WHERE filename = ? " + "LIMIT 1", (path, )): + track = Track(id=row["id"], + db=database, + tracknumber=row["tracknumber"], + name=row["name"], + grouping=row["grouping"], + filename=row["filename"]) return track @@ -451,50 +475,52 @@ class Track(BaseModel): rows = None row = None if musicbrainz_artistid: - rows = c.execute("SELECT * FROM artist WHERE " - "musicbrainz_artistid = ?", - (musicbrainz_artistid,)) + rows = c.execute( + "SELECT * FROM artist WHERE " + "musicbrainz_artistid = ?", (musicbrainz_artistid, )) row = rows.fetchone() if not row: - rows = c.execute("SELECT * FROM artist WHERE name = ? " - "AND musicbrainz_artistid IS NULL", - (artist_name,)) + rows = c.execute( + "SELECT * FROM artist WHERE name = ? " + "AND musicbrainz_artistid IS NULL", (artist_name, )) row = rows.fetchone() if not row: rows = c.execute("SELECT * FROM artist WHERE name = ?", - (artist_name,)) + (artist_name, )) row = rows.fetchone() if row: - artist = Artist(id=row["id"], db=database, name=row["name"], - sortname=row["sortname"], - musicbrainz_artistid=row[ - "musicbrainz_artistid"]) - - if (musicbrainz_artistid and - (not hasattr(artist, "musicbrainz_artistid") or - not artist.musicbrainz_artistid)): - c.execute("UPDATE artist SET musicbrainz_artistid = ? " - "WHERE id = ?", - (musicbrainz_artistid, artist.id)) + artist = Artist( + id=row["id"], + db=database, + name=row["name"], + sortname=row["sortname"], + musicbrainz_artistid=row["musicbrainz_artistid"]) + + if (musicbrainz_artistid + and (not hasattr(artist, "musicbrainz_artistid") + or not artist.musicbrainz_artistid)): + c.execute( + "UPDATE artist SET musicbrainz_artistid = ? " + "WHERE id = ?", (musicbrainz_artistid, artist.id)) if (artistsort and - (not hasattr(artist, "sortname") or - not artist.sortname)): + (not hasattr(artist, "sortname") or not artist.sortname)): c.execute("UPDATE artist SET sortname = ? WHERE id = ?", (artistsort, artist.id)) else: - c.execute("INSERT INTO artist (name, sortname, " - "musicbrainz_artistid) VALUES(?, ?, ?)", - (artist_name, artistsort, musicbrainz_artistid)) + c.execute( + "INSERT INTO artist (name, sortname, " + "musicbrainz_artistid) VALUES(?, ?, ?)", + (artist_name, artistsort, musicbrainz_artistid)) - artist = Artist( - id=c.lastrowid, db=database, name=artist_name, - sortname=artistsort, - musicbrainz_artistid=musicbrainz_artistid - ) + artist = Artist(id=c.lastrowid, + db=database, + name=artist_name, + sortname=artistsort, + musicbrainz_artistid=musicbrainz_artistid) i += 1 @@ -520,46 +546,59 @@ class Track(BaseModel): pass if mb_albumid: - rows = c.execute("SELECT * FROM album WHERE " - "musicbrainz_albumid = ?", (mb_albumid,)) + rows = c.execute( + "SELECT * FROM album WHERE " + "musicbrainz_albumid = ?", (mb_albumid, )) row = rows.fetchone() if row: - album = Album(id=row["id"], db=database, name=row["name"], - date=row["date"], musicbrainz_albumid=row[ - "musicbrainz_albumid"]) + album = Album(id=row["id"], + db=database, + name=row["name"], + date=row["date"], + musicbrainz_albumid=row["musicbrainz_albumid"]) else: - c.execute("INSERT INTO album (name, `date`, " - "musicbrainz_albumid) VALUES (?, ?, ?)", - (album_name, album_date, mb_albumid)) + c.execute( + "INSERT INTO album (name, `date`, " + "musicbrainz_albumid) VALUES (?, ?, ?)", + (album_name, album_date, mb_albumid)) - album = Album(id=c.lastrowid, db=database, name=album_name, - date=album_date, musicbrainz_albumid=mb_albumid) + album = Album(id=c.lastrowid, + db=database, + name=album_name, + date=album_date, + musicbrainz_albumid=mb_albumid) elif album_name: for artist in artists: - rows = c.execute("SELECT album.* FROM album INNER JOIN " - "album_artist ON album_artist.album_id = " - "album.id WHERE album.name = ? AND " - "artist_id = ?", (album_name, artist.id)) + rows = c.execute( + "SELECT album.* FROM album INNER JOIN " + "album_artist ON album_artist.album_id = " + "album.id WHERE album.name = ? AND " + "artist_id = ?", (album_name, artist.id)) row = rows.fetchone() if row: - album = Album(id=row["id"], db=database, name=row["name"], + album = Album(id=row["id"], + db=database, + name=row["name"], date=row["date"]) else: c.execute("INSERT INTO album (name, `date`) VALUES(?, ?)", (album_name, album_date)) - album = Album(id=c.lastrowid, db=database, name=album_name, + album = Album(id=c.lastrowid, + db=database, + name=album_name, date=album_date) for artist in artists: if album: try: - c.execute("INSERT INTO album_artist (artist_id, album_id) " - "VALUES(?, ?)", (artist.id, album.id)) + c.execute( + "INSERT INTO album_artist (artist_id, album_id) " + "VALUES(?, ?)", (artist.id, album.id)) except sqlite3.IntegrityError: pass @@ -580,33 +619,42 @@ class Track(BaseModel): pass track = None - rows = c.execute("SELECT * FROM track WHERE filename = ?", (filename,)) + rows = c.execute("SELECT * FROM track WHERE filename = ?", + (filename, )) row = rows.fetchone() if row: - track = Track(id=row["id"], db=database, - tracknumber=row["tracknumber"], name=row["name"], - grouping=row["grouping"], filename=row["filename"]) + track = Track(id=row["id"], + db=database, + tracknumber=row["tracknumber"], + name=row["name"], + grouping=row["grouping"], + filename=row["filename"]) else: - c.execute("INSERT INTO track (tracknumber, name, grouping, " - "filename) VALUES(?, ?, ?, ?)", - (track_number, track_name, track_grouping, - filename)) - - track = Track(id=c.lastrowid, db=database, tracknumber=track_number, - name=track_name, grouping=track_grouping, + c.execute( + "INSERT INTO track (tracknumber, name, grouping, " + "filename) VALUES(?, ?, ?, ?)", + (track_number, track_name, track_grouping, filename)) + + track = Track(id=c.lastrowid, + db=database, + tracknumber=track_number, + name=track_name, + grouping=track_grouping, filename=filename) if album: try: - c.execute("INSERT INTO album_track (album_id, track_id) " - "VALUES(?,?)", (album.id, track.id)) + c.execute( + "INSERT INTO album_track (album_id, track_id) " + "VALUES(?,?)", (album.id, track.id)) except sqlite3.IntegrityError: pass for artist in artists: try: - c.execute("INSERT INTO artist_track (artist_id, track_id) " - "VALUES(?, ?)", (artist.id, track.id)) + c.execute( + "INSERT INTO artist_track (artist_id, track_id) " + "VALUES(?, ?)", (artist.id, track.id)) except sqlite3.IntegrityError: pass @@ -616,7 +664,11 @@ class Track(BaseModel): return track @classmethod - def all(cls, database, order="track.id", direction="ASC", limit=None, + def all(cls, + database, + order="track.id", + direction="ASC", + limit=None, offset=None): tracks = [] @@ -628,15 +680,18 @@ class Track(BaseModel): direction) if limit and offset: - select_string = " ".join((select_string, - "LIMIT %s OFFSET %s" % (limit, offset))) + select_string = " ".join( + (select_string, "LIMIT %s OFFSET %s" % (limit, offset))) result = database.execute(select_string) for row in result: - tracks.append(Track(id=row["id"], db=database, - tracknumber=row["tracknumber"], - name=row["name"], grouping=row["name"], - filename=row["filename"])) + tracks.append( + Track(id=row["id"], + db=database, + tracknumber=row["tracknumber"], + name=row["name"], + grouping=row["name"], + filename=row["filename"])) return tracks @@ -13,7 +13,6 @@ import pyinotify from db.db_manager import DbManager from library import MediaLibrary - _LOGGER = logging.getLogger(__name__) @@ -111,14 +110,13 @@ class LibraryWatcher(object): pyinotify.IN_MODIFY if not hasattr(self, "notifier"): - setattr(self, - "notifier", - pyinotify.Notifier(self.wm, EventHandler(library), - timeout=10)) + setattr( + self, "notifier", + pyinotify.Notifier(self.wm, EventHandler(library), timeout=10)) if not hasattr(self, "wdd"): - setattr(self, "wdd", self.wm.add_watch(path, mask, rec=True, - auto_add=True)) + setattr(self, "wdd", + self.wm.add_watch(path, mask, rec=True, auto_add=True)) self.notifier.coalesce_events() |