Skip to content

Commit

Permalink
Merge pull request #48 from jordantgh/bugfix_checkboxes_saving
Browse files Browse the repository at this point in the history
Bugfix checkboxes & saving

- Prevents selection of an article from selecting all tables
(this was breaking the saving of table checked states as all
tables we being selected for any selected article. Still dont 
know why the article checkboxes get toggled on loading)

- Fixes the irrational saving behaviour of database files
(Implemented the same logic as for app session file)

- Unrelated -> added darkmode (not worth its own PR)
  • Loading branch information
jordantgh authored Feb 6, 2024
2 parents 3915924 + 75f7788 commit e9fa964
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 42 deletions.
4 changes: 4 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +55,8 @@ def main():
view = View()
_ = Controller(model, view)

app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())

view.show()
app.exec_()

Expand Down
11 changes: 9 additions & 2 deletions app/model/article_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
63 changes: 29 additions & 34 deletions app/model/database.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -41,35 +41,34 @@ 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()

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:
Expand Down Expand Up @@ -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()

Expand Down
25 changes: 19 additions & 6 deletions app/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit e9fa964

Please sign in to comment.