From 0538bfa32e37303539a6148ba096126a646ed4f3 Mon Sep 17 00:00:00 2001 From: BasioMeusPuga Date: Wed, 17 Jan 2018 01:47:21 +0530 Subject: [PATCH] Directory tagging support --- __main__.py | 2 ++ database.py | 6 +---- library.py | 65 ++++++++++++++++++++++++++++++++++++++++++++--- models.py | 5 ++++ settingsdialog.py | 5 ++-- threaded.py | 6 +---- 6 files changed, 72 insertions(+), 17 deletions(-) diff --git a/__main__.py b/__main__.py index 78f8bb6..b071c99 100755 --- a/__main__.py +++ b/__main__.py @@ -184,6 +184,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.lib_ref.generate_model('build') self.lib_ref.create_table_model() self.lib_ref.create_proxymodel() + self.lib_ref.generate_library_tags() # ListView self.listView.setGridSize(QtCore.QSize(175, 240)) @@ -409,6 +410,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.lib_ref.create_table_model() self.lib_ref.create_proxymodel() + self.lib_ref.generate_library_tags() def switch_library_view(self): if self.libraryToolBar.coverViewButton.isChecked(): diff --git a/database.py b/database.py index a366da0..454c723 100644 --- a/database.py +++ b/database.py @@ -19,7 +19,6 @@ import os import pickle import sqlite3 -import datetime from PyQt5 import QtCore @@ -43,7 +42,7 @@ class DatabaseInit: self.database.execute( "CREATE TABLE books \ (id INTEGER PRIMARY KEY, Title TEXT, Author TEXT, Year INTEGER, DateAdded TEXT, \ - Path TEXT, Position BLOB, ISBN TEXT, Tags TEXT, DirTags TEXT, Hash TEXT, \ + Path TEXT, Position BLOB, ISBN TEXT, Tags TEXT, Hash TEXT, \ Bookmarks BLOB, CoverImage BLOB)") # CheckState is the standard QtCore.Qt.Checked / Unchecked @@ -92,9 +91,6 @@ class DatabaseFunctions: current_datetime = QtCore.QDateTime().currentDateTime() current_datetime_bin = sqlite3.Binary(pickle.dumps(current_datetime)) - # TODO - # Account for directory tags - for i in data.items(): book_hash = i[0] title = i[1]['title'] diff --git a/library.py b/library.py index 36ce2d0..14a496b 100644 --- a/library.py +++ b/library.py @@ -22,6 +22,7 @@ import os import pickle +import pathlib from PyQt5 import QtGui, QtCore import database @@ -112,9 +113,9 @@ class Library: # This remarkably ugly hack is because the QSortFilterProxyModel # doesn't easily allow searching through multiple item roles - search_workaround = title + ' ' + author + search_workaround_base = title + ' ' + author if tags: - search_workaround += tags + search_workaround_base += tags # Additional data can be set using an incrementing # QtCore.Qt.UserRole @@ -134,19 +135,20 @@ class Library: item.setData(author, QtCore.Qt.UserRole + 1) item.setData(year, QtCore.Qt.UserRole + 2) item.setData(all_metadata, QtCore.Qt.UserRole + 3) - item.setData(search_workaround, QtCore.Qt.UserRole + 4) + item.setData(search_workaround_base, QtCore.Qt.UserRole + 4) item.setData(file_exists, QtCore.Qt.UserRole + 5) item.setData(i[8], QtCore.Qt.UserRole + 6) # File hash item.setData(position, QtCore.Qt.UserRole + 7) item.setData(False, QtCore.Qt.UserRole + 8) # Is the cover being displayed? item.setData(date_added, QtCore.Qt.UserRole + 9) + item.setData(search_workaround_base, QtCore.Qt.UserRole + 10) item.setIcon(QtGui.QIcon(img_pixmap)) self.view_model.appendRow(item) # all_metadata is just being sent. It is not being displayed # It will be correlated to the current row as its first userrole self.table_rows.append( - [title, author, None, year, tags, all_metadata, i[7]]) + [title, author, None, year, tags, all_metadata, i[8]]) def create_table_model(self): table_header = ['Title', 'Author', 'Status', 'Year', 'Tags'] @@ -216,6 +218,61 @@ class Library: self.proxy_model.sort(0, sort_order) self.parent.start_culling_timer() + def generate_library_tags(self): + db_library_directories = database.DatabaseFunctions( + self.parent.database_path).fetch_data( + ('Path', 'Name', 'Tags'), + 'directories', + {'Path': ''}, + 'LIKE') + + library_directories = { + i[0]: (i[1], i[2]) for i in db_library_directories} + + # Both the models will have to be done separately + # Item Model + for i in range(self.view_model.rowCount()): + this_item = self.view_model.item(i, 0) + + all_metadata = this_item.data(QtCore.Qt.UserRole + 3) + search_workaround_base = this_item.data(QtCore.Qt.UserRole + 10) + + path = os.path.dirname(all_metadata['path']) + path_ref = pathlib.Path(path) + + for j in library_directories: + if j == path or pathlib.Path(j) in path_ref.parents: + directory_name = library_directories[j][0].lower() + directory_tags = library_directories[j][1].lower() + + if directory_name: + search_workaround_base += directory_name + if directory_tags: + search_workaround_base += directory_tags + + this_item.setData(search_workaround_base, QtCore.Qt.UserRole + 4) + break + + # Table Model + for count, i in enumerate(self.table_model.display_data): + all_metadata = i[5] + path = os.path.dirname(all_metadata['path']) + path_ref = pathlib.Path(path) + + for j in library_directories: + if j == path or pathlib.Path(j) in path_ref.parents: + directory_name = library_directories[j][0].lower() + directory_tags = library_directories[j][1].lower() + + try: + i[7] = directory_name + i[8] = directory_tags + except IndexError: + i.extend([directory_name, directory_tags]) + + self.table_model.display_data[count] = i + break + def prune_models(self, valid_paths): # To be executed when the library is updated by folder # All files in unselected directories will have to be removed diff --git a/models.py b/models.py index 9153f51..8e794d0 100644 --- a/models.py +++ b/models.py @@ -147,6 +147,11 @@ class TableProxyModel(QtCore.QSortFilterProxyModel): model.data(i, QtCore.Qt.DisplayRole).lower() for i in valid_indices if model.data( i, QtCore.Qt.DisplayRole) is not None] + try: + valid_data.extend([model.display_data[row_num][7], model.display_data[row_num][8]]) + except IndexError: # Columns 7 and 8 are added after creation of the model + pass + for i in valid_data: if self.filter_string in i: return True diff --git a/settingsdialog.py b/settingsdialog.py index 46279bd..da3a678 100644 --- a/settingsdialog.py +++ b/settingsdialog.py @@ -191,8 +191,7 @@ class SettingsUI(QtWidgets.QDialog, settingswindow.Ui_Dialog): # The books the search thread has found # are now in self.thread.valid_files - valid_files = [i[0] for i in self.thread.valid_files] - if not valid_files: + if not self.thread.valid_files: return # Hey, messaging is important, okay? @@ -201,7 +200,7 @@ class SettingsUI(QtWidgets.QDialog, settingswindow.Ui_Dialog): # We now create a new thread to put those files into the database self.thread = BackGroundBookAddition( - valid_files, self.database_path, True, self.parent) + self.thread.valid_files, self.database_path, True, self.parent) self.thread.finished.connect(self.parent.move_on) self.thread.start() diff --git a/threaded.py b/threaded.py index 9b659b6..54874cb 100644 --- a/threaded.py +++ b/threaded.py @@ -101,17 +101,13 @@ class BackGroundBookSearch(QtCore.QThread): def traverse_directory(incoming_data): root_directory = incoming_data[0] - folder_name = incoming_data[1] - folder_tags = incoming_data[2] - for directory, subdirs, files in os.walk(root_directory, topdown=True): # Black magic fuckery # Skip subdir tree in case it's not wanted subdirs[:] = [d for d in subdirs if is_wanted(os.path.join(directory, d))] for filename in files: if os.path.splitext(filename)[1][1:] in sorter.available_parsers: - self.valid_files.append( - (os.path.join(directory, filename), folder_name, folder_tags)) + self.valid_files.append(os.path.join(directory, filename)) def initiate_threads(): _pool = Pool(5)