Fairly substantial rewrite.

This commit is contained in:
BasioMeusPuga
2017-12-28 18:27:42 +05:30
parent 69392c5d4f
commit 01d1be9ddc
23 changed files with 1360 additions and 611 deletions

View File

@@ -1,6 +1,23 @@
#!/usr/bin/env python3
# This file is a part of Lector, a Qt based ebook reader
# Copyright (C) 2017 BasioMeusPuga
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import pathlib
from multiprocessing.dummy import Pool
from PyQt5 import QtCore
@@ -26,38 +43,72 @@ class BackGroundTabUpdate(QtCore.QThread):
class BackGroundBookAddition(QtCore.QThread):
def __init__(self, parent_window, file_list, database_path, parent=None):
def __init__(self, file_list, database_path, prune_required, parent=None):
super(BackGroundBookAddition, self).__init__(parent)
self.parent_window = parent_window
self.file_list = file_list
self.parent = parent
self.database_path = database_path
self.prune_required = prune_required
def run(self):
books = sorter.BookSorter(
self.file_list,
'addition',
self.database_path)
self.database_path,
self.parent.settings['auto_tags'])
parsed_books = books.initiate_threads()
self.parent.lib_ref.generate_model('addition', parsed_books)
if self.prune_required:
self.parent.lib_ref.prune_models(self.file_list)
database.DatabaseFunctions(self.database_path).add_to_database(parsed_books)
self.parent_window.lib_ref.generate_model('addition', parsed_books)
class BackGroundBookDeletion(QtCore.QThread):
def __init__(self, hash_list, database_path, parent=None):
super(BackGroundBookDeletion, self).__init__(parent)
self.parent = parent
self.hash_list = hash_list
self.database_path = database_path
def run(self):
database.DatabaseFunctions(
self.database_path).delete_from_database('Hash', self.hash_list)
class BackGroundBookSearch(QtCore.QThread):
def __init__(self, parent_window, data_list, parent=None):
# TODO
# Change existing sorter module functionality to handle preset tags
# Change database to accomodate User Tags, Folder Name, Folder Tags
def __init__(self, data_list, parent=None):
super(BackGroundBookSearch, self).__init__(parent)
self.parent_window = parent_window
self.data_list = data_list
self.valid_files = [] # A tuple should get added to this containing the
# file path and the folder name / tags
self.parent = parent
self.valid_files = []
# Filter for checked directories
self.valid_directories = [
[i[0], i[1], i[2]] for i in data_list if i[3] == QtCore.Qt.Checked]
self.unwanted_directories = [
pathlib.Path(i[0]) for i in data_list if i[3] == QtCore.Qt.Unchecked]
def run(self):
def is_wanted(directory):
directory_parents = pathlib.Path(directory).parents
for i in self.unwanted_directories:
if i in directory_parents:
return False
return True
def traverse_directory(incoming_data):
root_directory = incoming_data[0]
folder_name = incoming_data[1]
folder_tags = incoming_data[2]
for directory, subdir, files in os.walk(root_directory):
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(
@@ -65,17 +116,9 @@ class BackGroundBookSearch(QtCore.QThread):
def initiate_threads():
_pool = Pool(5)
_pool.map(traverse_directory, self.data_list)
_pool.map(traverse_directory, self.valid_directories)
_pool.close()
_pool.join()
initiate_threads()
# TODO
# Change existing sorter module functionality to handle
# preset tags
# Change database to accomodate User Tags, Folder Name, Folder Tags
# self.valid_files will now be added to the database
# and models will be rebuilt accordingly
# Coming soon to a commit near you
print(len(self.valid_files), 'books found')