From 765e88c9930e4a709e0664d319a47890391bcb6f Mon Sep 17 00:00:00 2001 From: Marco de Freitas <marco@sillage.ch> Date: Sat, 27 Nov 2021 21:22:48 +0100 Subject: [PATCH] Improved exception handling on click and right click (open file, source) --- didacto/controller.py | 56 +++++++++++++++++++++++-------------------- didacto/view.py | 21 ++++++++++++---- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/didacto/controller.py b/didacto/controller.py index d6b9291..b49d0e5 100644 --- a/didacto/controller.py +++ b/didacto/controller.py @@ -39,28 +39,7 @@ CONFIG_FOLDER = "config" CONFIG_FILE = "preferences.ini" -def edit_pdf_keywords(path, keywords): - with io.open(path, 'rb') as file_in: - reader = PdfFileReader(file_in) - writer = PdfFileWriter() - - metadata_in = reader.getDocumentInfo() - metadata_out = {} - for key in metadata_in: - metadata_out[key] = metadata_in[key] - if '/AAPL:Keywords' in metadata_out: - metadata_out.pop('/AAPL:Keywords') # this fixes some bug with sibelius6 - MacOs pdf - print(metadata_out) - print(type(metadata_out)) - - writer.appendPagesFromReader(reader) - writer.addMetadata(metadata_out) - writer.addMetadata({ - '/Keywords': keywords - }) - - with io.open(path, 'ab') as file_out: # ab is append binary; if you do wb, the file will append blank pages - writer.write(file_out) + def create_default_file_struct(home_dir): @@ -188,10 +167,11 @@ class Controller: def run_file(self, path): """system call open file.""" if not os.path.exists(path): - raise IOError('No such file: %s' % path) - - if hasattr(os, 'access') and not os.access(path, os.R_OK): - raise IOError('Cannot access file: %s' % path) + raise OSError + # ('No such file: %s' % path) TODO replace error message by tkinter.messagebox.showwarning + # + # if hasattr(os, 'access') and not os.access(path, os.R_OK): + # raise IOError('Cannot access file: %s' % path) # os dependant open function @@ -212,3 +192,27 @@ class Controller: "Your `%s` isn't a supported operating system`." % sys.platform) return proc + + @staticmethod + def edit_pdf_keywords(path, keywords): + with io.open(path, 'rb') as file_in: + reader = PdfFileReader(file_in) + writer = PdfFileWriter() + + metadata_in = reader.getDocumentInfo() + metadata_out = {} + for key in metadata_in: + metadata_out[key] = metadata_in[key] + if '/AAPL:Keywords' in metadata_out: + metadata_out.pop('/AAPL:Keywords') # this fixes some bug with sibelius6 - MacOs pdf + # print(metadata_out) + # print(type(metadata_out)) + + writer.appendPagesFromReader(reader) + writer.addMetadata(metadata_out) + writer.addMetadata({ + '/Keywords': str(keywords) + }) # for some reason, keywords string must be converted to string + + with io.open(path, 'ab') as file_out: # ab is append binary; if you do wb, the file will append blank pages + writer.write(file_out) diff --git a/didacto/view.py b/didacto/view.py index ca4b5a1..0840958 100644 --- a/didacto/view.py +++ b/didacto/view.py @@ -175,7 +175,10 @@ class View(tkinter.Frame): else: folder = self.tv.item(item)["values"][1] path = "/".join((folder, name)) - self.controller.run_file(path) + try: + self.controller.run_file(path) + except OSError: + tkinter.messagebox.showwarning("Error", "File doesn't exist") def edit_pdf_keywords(self): name = self.tv.item(self.popup_item)["values"][0] # prompts pdf file name @@ -269,7 +272,7 @@ class View(tkinter.Frame): self.tree_repopulate() self.keyword_explained.set('') if len(self.model.errors_text) > 0: - tkinter.messagebox.showwarning("Erreurs", self.model.errors_text) + tkinter.messagebox.showwarning("Error", self.model.errors_text) # fontions de la fenêtre principale @staticmethod @@ -292,7 +295,7 @@ class View(tkinter.Frame): self.tree_delete(treeSize) self.tree_repopulate() if len(self.model.errors_text) > 0: - tkinter.messagebox.showerror("Erreurs", self.model.errors_text) + tkinter.messagebox.showerror("Error", self.model.errors_text) # gestion des racourcis clavier def select_folder(self, event): @@ -334,7 +337,12 @@ class View(tkinter.Frame): path = "/".join((tv_path, name)) if self.checkbox_value.get() is False: path = "/".join((self.corpora_path.get(), name)) - self.controller.run_file(path) + try: + self.controller.run_file(path) + except OSError: + tkinter.messagebox.showerror("Error", "File doesn't exit.") + + else: key = self.tv.item(item)['text'] ############# self.curent_keyword = key @@ -357,7 +365,10 @@ class View(tkinter.Frame): else: folder = self.tv.item(item)["values"][1] path = "/".join((folder, name)) - self.controller.run_file(path) + try: + self.controller.run_file(path) + except OSError: + tkinter.messagebox.showerror("Error", "File doesn't exist.") @staticmethod def open_readthedocs(): -- GitLab