Directory tagging support
This commit is contained in:
@@ -184,6 +184,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
|
|||||||
self.lib_ref.generate_model('build')
|
self.lib_ref.generate_model('build')
|
||||||
self.lib_ref.create_table_model()
|
self.lib_ref.create_table_model()
|
||||||
self.lib_ref.create_proxymodel()
|
self.lib_ref.create_proxymodel()
|
||||||
|
self.lib_ref.generate_library_tags()
|
||||||
|
|
||||||
# ListView
|
# ListView
|
||||||
self.listView.setGridSize(QtCore.QSize(175, 240))
|
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_table_model()
|
||||||
self.lib_ref.create_proxymodel()
|
self.lib_ref.create_proxymodel()
|
||||||
|
self.lib_ref.generate_library_tags()
|
||||||
|
|
||||||
def switch_library_view(self):
|
def switch_library_view(self):
|
||||||
if self.libraryToolBar.coverViewButton.isChecked():
|
if self.libraryToolBar.coverViewButton.isChecked():
|
||||||
|
@@ -19,7 +19,6 @@
|
|||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import datetime
|
|
||||||
|
|
||||||
from PyQt5 import QtCore
|
from PyQt5 import QtCore
|
||||||
|
|
||||||
@@ -43,7 +42,7 @@ class DatabaseInit:
|
|||||||
self.database.execute(
|
self.database.execute(
|
||||||
"CREATE TABLE books \
|
"CREATE TABLE books \
|
||||||
(id INTEGER PRIMARY KEY, Title TEXT, Author TEXT, Year INTEGER, DateAdded TEXT, \
|
(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)")
|
Bookmarks BLOB, CoverImage BLOB)")
|
||||||
|
|
||||||
# CheckState is the standard QtCore.Qt.Checked / Unchecked
|
# CheckState is the standard QtCore.Qt.Checked / Unchecked
|
||||||
@@ -92,9 +91,6 @@ class DatabaseFunctions:
|
|||||||
current_datetime = QtCore.QDateTime().currentDateTime()
|
current_datetime = QtCore.QDateTime().currentDateTime()
|
||||||
current_datetime_bin = sqlite3.Binary(pickle.dumps(current_datetime))
|
current_datetime_bin = sqlite3.Binary(pickle.dumps(current_datetime))
|
||||||
|
|
||||||
# TODO
|
|
||||||
# Account for directory tags
|
|
||||||
|
|
||||||
for i in data.items():
|
for i in data.items():
|
||||||
book_hash = i[0]
|
book_hash = i[0]
|
||||||
title = i[1]['title']
|
title = i[1]['title']
|
||||||
|
65
library.py
65
library.py
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
|
import pathlib
|
||||||
from PyQt5 import QtGui, QtCore
|
from PyQt5 import QtGui, QtCore
|
||||||
|
|
||||||
import database
|
import database
|
||||||
@@ -112,9 +113,9 @@ class Library:
|
|||||||
|
|
||||||
# This remarkably ugly hack is because the QSortFilterProxyModel
|
# This remarkably ugly hack is because the QSortFilterProxyModel
|
||||||
# doesn't easily allow searching through multiple item roles
|
# doesn't easily allow searching through multiple item roles
|
||||||
search_workaround = title + ' ' + author
|
search_workaround_base = title + ' ' + author
|
||||||
if tags:
|
if tags:
|
||||||
search_workaround += tags
|
search_workaround_base += tags
|
||||||
|
|
||||||
# Additional data can be set using an incrementing
|
# Additional data can be set using an incrementing
|
||||||
# QtCore.Qt.UserRole
|
# QtCore.Qt.UserRole
|
||||||
@@ -134,19 +135,20 @@ class Library:
|
|||||||
item.setData(author, QtCore.Qt.UserRole + 1)
|
item.setData(author, QtCore.Qt.UserRole + 1)
|
||||||
item.setData(year, QtCore.Qt.UserRole + 2)
|
item.setData(year, QtCore.Qt.UserRole + 2)
|
||||||
item.setData(all_metadata, QtCore.Qt.UserRole + 3)
|
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(file_exists, QtCore.Qt.UserRole + 5)
|
||||||
item.setData(i[8], QtCore.Qt.UserRole + 6) # File hash
|
item.setData(i[8], QtCore.Qt.UserRole + 6) # File hash
|
||||||
item.setData(position, QtCore.Qt.UserRole + 7)
|
item.setData(position, QtCore.Qt.UserRole + 7)
|
||||||
item.setData(False, QtCore.Qt.UserRole + 8) # Is the cover being displayed?
|
item.setData(False, QtCore.Qt.UserRole + 8) # Is the cover being displayed?
|
||||||
item.setData(date_added, QtCore.Qt.UserRole + 9)
|
item.setData(date_added, QtCore.Qt.UserRole + 9)
|
||||||
|
item.setData(search_workaround_base, QtCore.Qt.UserRole + 10)
|
||||||
item.setIcon(QtGui.QIcon(img_pixmap))
|
item.setIcon(QtGui.QIcon(img_pixmap))
|
||||||
self.view_model.appendRow(item)
|
self.view_model.appendRow(item)
|
||||||
|
|
||||||
# all_metadata is just being sent. It is not being displayed
|
# all_metadata is just being sent. It is not being displayed
|
||||||
# It will be correlated to the current row as its first userrole
|
# It will be correlated to the current row as its first userrole
|
||||||
self.table_rows.append(
|
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):
|
def create_table_model(self):
|
||||||
table_header = ['Title', 'Author', 'Status', 'Year', 'Tags']
|
table_header = ['Title', 'Author', 'Status', 'Year', 'Tags']
|
||||||
@@ -216,6 +218,61 @@ class Library:
|
|||||||
self.proxy_model.sort(0, sort_order)
|
self.proxy_model.sort(0, sort_order)
|
||||||
self.parent.start_culling_timer()
|
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):
|
def prune_models(self, valid_paths):
|
||||||
# To be executed when the library is updated by folder
|
# To be executed when the library is updated by folder
|
||||||
# All files in unselected directories will have to be removed
|
# All files in unselected directories will have to be removed
|
||||||
|
@@ -147,6 +147,11 @@ class TableProxyModel(QtCore.QSortFilterProxyModel):
|
|||||||
model.data(i, QtCore.Qt.DisplayRole).lower() for i in valid_indices if model.data(
|
model.data(i, QtCore.Qt.DisplayRole).lower() for i in valid_indices if model.data(
|
||||||
i, QtCore.Qt.DisplayRole) is not None]
|
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:
|
for i in valid_data:
|
||||||
if self.filter_string in i:
|
if self.filter_string in i:
|
||||||
return True
|
return True
|
||||||
|
@@ -191,8 +191,7 @@ class SettingsUI(QtWidgets.QDialog, settingswindow.Ui_Dialog):
|
|||||||
|
|
||||||
# The books the search thread has found
|
# The books the search thread has found
|
||||||
# are now in self.thread.valid_files
|
# are now in self.thread.valid_files
|
||||||
valid_files = [i[0] for i in self.thread.valid_files]
|
if not self.thread.valid_files:
|
||||||
if not valid_files:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# Hey, messaging is important, okay?
|
# 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
|
# We now create a new thread to put those files into the database
|
||||||
self.thread = BackGroundBookAddition(
|
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.finished.connect(self.parent.move_on)
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
|
|
||||||
|
@@ -101,17 +101,13 @@ class BackGroundBookSearch(QtCore.QThread):
|
|||||||
|
|
||||||
def traverse_directory(incoming_data):
|
def traverse_directory(incoming_data):
|
||||||
root_directory = incoming_data[0]
|
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):
|
for directory, subdirs, files in os.walk(root_directory, topdown=True):
|
||||||
# Black magic fuckery
|
# Black magic fuckery
|
||||||
# Skip subdir tree in case it's not wanted
|
# Skip subdir tree in case it's not wanted
|
||||||
subdirs[:] = [d for d in subdirs if is_wanted(os.path.join(directory, d))]
|
subdirs[:] = [d for d in subdirs if is_wanted(os.path.join(directory, d))]
|
||||||
for filename in files:
|
for filename in files:
|
||||||
if os.path.splitext(filename)[1][1:] in sorter.available_parsers:
|
if os.path.splitext(filename)[1][1:] in sorter.available_parsers:
|
||||||
self.valid_files.append(
|
self.valid_files.append(os.path.join(directory, filename))
|
||||||
(os.path.join(directory, filename), folder_name, folder_tags))
|
|
||||||
|
|
||||||
def initiate_threads():
|
def initiate_threads():
|
||||||
_pool = Pool(5)
|
_pool = Pool(5)
|
||||||
|
Reference in New Issue
Block a user