From 0f5afbf8039c5a3f95b96fe99d19170760f52c7e Mon Sep 17 00:00:00 2001 From: Jordan Taylor Date: Tue, 6 Feb 2024 13:00:55 +0000 Subject: [PATCH 1/3] Only allow article checks to UNCHECK tables --- app/model/article_managers.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/model/article_managers.py b/app/model/article_managers.py index ab58017..73e6dd4 100644 --- a/app/model/article_managers.py +++ b/app/model/article_managers.py @@ -161,8 +161,10 @@ def __init__(self, article_json: 'dict'): self.pruned_tables: 'list[ProcessedTable]' = [] self.observers: 'dict[PageIdentity, ArticleListItem]' = {} - def checkbox_toggled(self, context: 'Optional[PageIdentity]' = None): - if context is not None: + def checkbox_toggled(self, context: 'PageIdentity'): + # ensure this only works if the checked state would be set to False + # (to prevent all tables being selected when the article is selected) + if self.checked[context] != True: # cascade state change down to the tables new_checked_state = self.checked[context] tables = self._get_tables_by_context(context) @@ -202,6 +204,11 @@ def update_based_on_elements(self, context: 'PageIdentity'): for con in (PageIdentity.PARSED, PageIdentity.PRUNED): # check the context exists in the observers dict first # this is mainly for when there hasnt yet been any pruning + + # Note that iterating through article contexts like this is a + # hack until I figure out whether I need page specific checked + # states at all. (TODO) + if con in self.observers: self.checked[con] = has_checked self.notify_observers(con) From 4e435223ed522094f09f7729c391b01dd757693f Mon Sep 17 00:00:00 2001 From: Jordan Taylor Date: Tue, 6 Feb 2024 13:01:35 +0000 Subject: [PATCH 2/3] Add darkmode --- app/main.py | 4 ++++ pyproject.toml | 1 + 2 files changed, 5 insertions(+) diff --git a/app/main.py b/app/main.py index 9105a12..7d8ef74 100644 --- a/app/main.py +++ b/app/main.py @@ -10,6 +10,8 @@ from controller.controller import Controller import sys +import qdarkstyle + def get_app_data_path(): if getattr(sys, 'frozen', False): # Running as compiled binary @@ -53,6 +55,8 @@ def main(): view = View() _ = Controller(model, view) + app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5()) + view.show() app.exec_() diff --git a/pyproject.toml b/pyproject.toml index 057283b..8d29f41 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,6 +23,7 @@ nltk = "^3.8.1" toml = "^0.10.2" pyparsing = "^3.1.1" xlwings = "^0.30.12" +qdarkstyle = "^3.2.3" [tool.poetry.group.dev.dependencies] From 75f7788aa81f9164fa9b0ab245ac358f982a2977 Mon Sep 17 00:00:00 2001 From: Jordan Taylor Date: Tue, 6 Feb 2024 13:02:19 +0000 Subject: [PATCH 3/3] Rationalise save/load logic for dbs --- app/model/database.py | 63 ++++++++++++++++++++----------------------- app/model/model.py | 25 ++++++++++++----- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/app/model/database.py b/app/model/database.py index 9735901..7fc1134 100644 --- a/app/model/database.py +++ b/app/model/database.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Optional if TYPE_CHECKING: from uuid import UUID from pandas import DataFrame @@ -41,19 +41,15 @@ def __init__( self, db_temp_path_root: 'str' = ".", db_perm_path_root: 'str' = ".", - old_processed_path: 'str' = None, - old_pruned_path: 'str' = None + db_session_files: 'Optional[list[str]]' = None ): self.db_temp_path_root = db_temp_path_root self.db_perm_path_root = db_perm_path_root - if old_processed_path and old_pruned_path: - self._loaded = True - self.old_paths = [old_processed_path, old_pruned_path] - self._create_temp_dbs(db_temp_path_root) + if db_session_files: + self._create_temp_dbs(db_temp_path_root, db_session_files) else: - self._loaded = False self._create_temp_dbs(db_temp_path_root) self._establish_connections() @@ -61,15 +57,18 @@ def __init__( Base.metadata.create_all(self.processed_engine) Base.metadata.create_all(self.post_pruning_engine) - def _create_temp_dbs(self, db_path: 'str'): + def _create_temp_dbs( + self, db_path: 'str', + db_session_files: 'Optional[list[str]]' = None + ): id = str(uuid4()) self.processed_db_temp_path = os.path.join( db_path, f"processed_db-{id}.db" ) self.pruned_db_temp_path = os.path.join(db_path, f"pruned_db-{id}.db") - if self._loaded: - shutil.copyfile(self.old_paths[0], self.processed_db_temp_path) - shutil.copyfile(self.old_paths[1], self.pruned_db_temp_path) + if db_session_files: + shutil.copyfile(db_session_files[0], self.processed_db_temp_path) + shutil.copyfile(db_session_files[1], self.pruned_db_temp_path) def _get_engine_and_session(self, table_class: 'TableDBEntry') -> 'tuple': if table_class == ProcessedTableDBEntry: @@ -207,32 +206,28 @@ def delete_table( session.delete(table_entry) session.commit() - def save_dbs(self, filename: 'str') -> 'list[str]': - - basename = os.path.basename(filename) - basename_no_ext = os.path.splitext(basename)[0] - - if self._loaded: + def save_dbs( + self, + filename: 'str', + db_session_files: 'Optional[list[str]]' = None + ) -> 'list[str]': - processed_target_path = self.old_paths[0] - pruned_target_path = self.old_paths[1] + if db_session_files: + os.remove(db_session_files[0]) + os.remove(db_session_files[1]) - # Delete old versions of the databases - if os.path.exists(processed_target_path): - os.remove(processed_target_path) + basename = os.path.basename(filename) + basename_no_ext = os.path.splitext(basename)[0] - if os.path.exists(pruned_target_path): - os.remove(pruned_target_path) + processed_target_path = os.path.join( + self.db_perm_path_root, + f"{basename_no_ext}-processed.db" + ) - else: - processed_target_path = os.path.join( - self.db_perm_path_root, - f"{basename_no_ext}-processed.db" - ) - pruned_target_path = os.path.join( - self.db_perm_path_root, - f"{basename_no_ext}-pruned.db" - ) + pruned_target_path = os.path.join( + self.db_perm_path_root, + f"{basename_no_ext}-pruned.db" + ) self._dispose_connections() diff --git a/app/model/model.py b/app/model/model.py index 41be50f..32b6049 100644 --- a/app/model/model.py +++ b/app/model/model.py @@ -4,7 +4,7 @@ from pandas import DataFrame import pickle - +import os from model.article_managers import ( Bibliography, Article, ProcessedTable, ProcessedTableManager, stash_all_observers, restore_all_observers @@ -29,12 +29,15 @@ def __init__( self.session_file = None self.db_temp_path_root = db_temp_path_root self.db_perm_path_root = db_perm_path_root + self.db_session_files = None self.saves_path = saves_path self._state = Mode.BROWSING self.bibliography = Bibliography() self.search_thread = SearchThread() self.search_preview_thread = FilePreviewThread() - self.table_db_manager = TableDBManager(db_temp_path_root, db_perm_path_root) + self.table_db_manager = TableDBManager( + db_temp_path_root, db_perm_path_root + ) self.processed_table_manager = ProcessedTableManager() self.processing_thread = FileProcessingThread(self.table_db_manager) self.last_selected_table: 'ProcessedTable' = None @@ -96,7 +99,7 @@ def update_article( def prune_tables_and_columns(self, context: 'PageIdentity'): # TODO unspaghettify this - article: 'Article' + article: 'Article' for article in self.bibliography.articles.values(): unchecked_tables = [ @@ -207,7 +210,12 @@ def save(self, filename: 'str'): stash_all_observers(self.bibliography, global_stash, visited_objects) - proc, prune = self.table_db_manager.save_dbs(filename) + if self.db_session_files: + proc, prune = self.table_db_manager.save_dbs( + filename, self.db_session_files + ) + else: + proc, prune = self.table_db_manager.save_dbs(filename) save_object = { 'db_perm_path_root': self.db_perm_path_root, @@ -226,17 +234,22 @@ def save(self, filename: 'str'): visited_objects.clear() restore_all_observers(self.bibliography, global_stash, visited_objects) self.session_file = filename + self.db_session_files = [proc, prune] def load(self, file: 'str'): self.session_file = file + self.saves_path = os.path.dirname(file) with open(file, 'rb') as f: save_object = pickle.load(f) self.bibliography = save_object['bibliography'] + self.db_session_files = [ + save_object['processed_db_path'], + save_object['pruned_db_path'] + ] self.table_db_manager = TableDBManager( self.db_temp_path_root, save_object['db_perm_path_root'], - save_object['processed_db_path'], - save_object['pruned_db_path'] + self.db_session_files ) self.processed_table_manager = save_object['processed_table_manager'] self.search_thread = SearchThread()