From 601198884d58c0f3825e7108a9adb4dc4353ff5c Mon Sep 17 00:00:00 2001 From: Michaƫl Ball Date: Wed, 24 Dec 2014 17:52:04 +0000 Subject: Better searching methods --- common/utils.py | 32 ++++++++++++++++---------------- models/album.py | 20 +++++++++++++++++--- models/artist.py | 20 +++++++++++++++++--- models/track.py | 22 +++++++++++++++++++--- watcher.py | 9 +++++++-- 5 files changed, 76 insertions(+), 27 deletions(-) diff --git a/common/utils.py b/common/utils.py index 484179a..288673e 100644 --- a/common/utils.py +++ b/common/utils.py @@ -1,23 +1,25 @@ -def make_where_clause(params): - """Create a where clause for each key-value pair in a dict, joined - by AND. - - Parameters - ---------- - params : dict - A dict of keys and values +def make_where_clause(params, join_operator="AND"): + """Create a where clause from the param. + + Args: + params : A dict where key is the column and the value a comparison + operator + join_operator: string to join comparisons. Should be "AND" or "OR" """ where_items = [] where_clause = None try: - for key in params.keys(): - where_items.append("%s=:%s" % (key, key)) + for (column, operator) in params.items(): + condition_subphrase = " ".join(("%s", operator, ":%s")) + where_items.append(condition_subphrase % (column, column)) where_statement = None if len(where_items) > 1: - where_statement = " AND ".join(where_items) + # surround join operator with spaces + join_string = "".join((" ", join_operator, " ")) + where_statement = join_string.join(where_items) else: where_statement = where_items[0] @@ -29,12 +31,10 @@ def make_where_clause(params): def update_clause_from_dict(data): - """Create an update clause from a dictionary + """Create an update clause - Parameters - __________ - data: dict - A dict of the new value and the column name as key + Args: + data: A dict of the new value and the column name as key """ update_items = [] diff --git a/models/album.py b/models/album.py index 727156f..24af0b5 100644 --- a/models/album.py +++ b/models/album.py @@ -87,22 +87,36 @@ class Album(): set_clause = utils.update_clause_from_dict(dirty_attributes) dirty_attributes[id] = self.id - + sql = " ".join(("UPDATE album"), set_clause, "WHERE id = :id") db.execute(sql, dirty_attributes) db.commit() def search(**search_params): + """Find an album with the given params + + Args: + name: dict, with 'data' and 'operator' keys + date: dict, with 'data' and 'operator' keys + musicbrainz_albumid: dict, with 'data' and 'operator' keys + """ albums = [] db = DbManager() - where_clause = utils.make_where_clause(search_params) + # unpack search params + where_params = {} + value_params = {} + for (attr, value) in search_params.items(): + where_params[attr] = value["operator"] + value_params[attr] = value["data"] + + where_clause = utils.make_where_clause(where_params) result = None if where_clause: statement = " ".join(("SELECT * FROM album", where_clause)) - result = db.execute(statement, search_params) + result = db.execute(statement, value_params) else: result = db.execute("SELECT * FROM album") diff --git a/models/artist.py b/models/artist.py index eaae27e..7d36457 100644 --- a/models/artist.py +++ b/models/artist.py @@ -87,22 +87,36 @@ class Artist: set_clause = utils.update_clause_from_dict(dirty_attributes) dirty_attributes[id] = self.id - + sql = " ".join(("UPDATE artist"), set_clause, "WHERE id = :id") db.execute(sql, dirty_attributes) db.commit() def search(**search_params): + """Find an artist with the given params + + Args: + name: dict, with 'data' and 'operator' keys + sortname: dict, with 'data' and 'operator' keys + musicbrainz_artist_id: dict, with 'data' and 'operator' keys + """ artists = [] db = DbManager() - where_clause = utils.make_where_clause(search_params) + # unpack search params + where_params = {} + value_params = {} + for (attr, value) in search_params.items(): + where_params[attr] = value["operator"] + value_params[attr] = value["data"] + + where_clause = utils.make_where_clause(where_params) result = [] if where_clause: statement = " ".join(("SELECT * FROM artist", where_clause)) - result = db.execute(statement, search_params) + result = db.execute(statement, value_params) else: result = db.execute("SELECT * FROM artist") diff --git a/models/track.py b/models/track.py index 264db81..90eba9f 100644 --- a/models/track.py +++ b/models/track.py @@ -271,21 +271,37 @@ class Track: set_clause = utils.update_clause_from_dict(dirty_attributes) dirty_attributes[id] = self.id - + sql = " ".join(("UPDATE track"), set_clause, "WHERE id = :id") db.execute(sql, dirty_attributes) db.commit() def search(**search_params): + """Find a track with the given params + + Args: + tracknumber: dict, with 'data' and 'operator' keys + name: dict, with 'data' and 'operator' keys + grouping: dict, with 'data' and 'operator' keys + filename: dict, with 'data' and 'operator' keys + """ + db = DbManager() tracks = [] - where_clause = utils.make_where_clause(search_params) + # unpack search params + where_params = {} + value_params = {} + for (attr, value) in search_params.items(): + where_params[attr] = value["operator"] + value_params[attr] = value["data"] + + where_clause = utils.make_where_clause(where_params) result = None if where_clause: statement = " ".join(("SELECT * FROM track", where_clause)) - result = db.execute(statement, search_params) + result = db.execute(statement, value_params) else: result = db.execute("SELECT * FROM track") diff --git a/watcher.py b/watcher.py index d1e4ab6..9af4a6b 100644 --- a/watcher.py +++ b/watcher.py @@ -1,5 +1,6 @@ import atexit import configparser +import os import pyinotify @@ -13,7 +14,9 @@ class EventHandler(pyinotify.ProcessEvent): def process_IN_DELETE(self, event): print("Removing:", event.pathname) - library.delete_file(event.pathname) + + if not os.path.isdir(event.pathname): + library.delete_file(event.pathname) def process_IN_MOVED_TO(self, event): print("Moved to:", event.pathname) @@ -24,7 +27,9 @@ class EventHandler(pyinotify.ProcessEvent): def process_IN_MODIFY(self, event): print("Modified:", event.pathname) - library.update_file(event.pathname) + + if not os.path.isdir(event.pathname): + library.update_file(event.pathname) class LibraryWatcher: -- cgit v1.2.3