From dd92b26e66c5e762a510e9852e0e2999ab890b44 Mon Sep 17 00:00:00 2001 From: Orsiris de Jong Date: Mon, 28 Aug 2023 10:29:01 +0200 Subject: [PATCH] Add snapshot deletion option (actually forget operation) --- npbackup/gui/main.py | 38 +++++++++++++++++++++++++++ npbackup/translations/generic.en.yml | 6 ++++- npbackup/translations/generic.fr.yml | 6 ++++- npbackup/translations/main_gui.en.yml | 4 ++- npbackup/translations/main_gui.fr.yml | 4 ++- 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/npbackup/gui/main.py b/npbackup/gui/main.py index 5009940..d436572 100644 --- a/npbackup/gui/main.py +++ b/npbackup/gui/main.py @@ -244,6 +244,11 @@ def _make_treedata_from_json(ls_result: List[dict]) -> sg.TreeData: sleep(0.0001) return treedata +@threaded +def _delete_backup(config: dict, snapshot_id: str) -> Future: + runner = NPBackupRunner(config_dict=config) + result = runner.forget(snapshot=snapshot_id) + return result @threaded def _ls_window(config: dict, snapshot_id: str) -> Future: @@ -287,6 +292,31 @@ def _ls_window(config: dict, snapshot_id: str) -> Future: return backup_content, result +def delete_backup(config: dict, snapshot: str) -> bool: + snapshot_id = re.match(r".*\[ID (.*)\].*", snapshot).group(1) + # We get a thread result, hence pylint will complain the thread isn't a tuple + # pylint: disable=E1101 (no-member) + thread = _delete_backup(config, snapshot_id) + + while not thread.done() and not thread.cancelled(): + sg.PopupAnimated( + LOADER_ANIMATION, + message="{}. {}".format( + _t("main_gui.execute_operation"), + _t("main_gui.this_will_take_a_while"), + ), + time_between_frames=50, + background_color=GUI_LOADER_COLOR, + text_color=GUI_LOADER_TEXT_COLOR, + ) + sg.PopupAnimated(None) + result = thread.result() + if not result: + sg.PopupError(_t("main_gui.delete_failed"), keep_on_top=True) + return False + else: + sg.Popup("{} {} {}".format(snapshot, _t("generic.deleted"), _t("generic.successfully"))) + def ls_window(config: dict, snapshot: str) -> bool: snapshot_id = re.match(r".*\[ID (.*)\].*", snapshot).group(1) # We get a thread result, hence pylint will complain the thread isn't a tuple @@ -521,6 +551,7 @@ def main_gui(config_dict: dict, config_file: str, version_string: str): [ sg.Button(_t("main_gui.launch_backup"), key="launch-backup"), sg.Button(_t("main_gui.see_content"), key="see-content"), + sg.Button(_t("generic.delete"), key="delete"), sg.Button(_t("generic.configure"), key="configure"), sg.Button(_t("generic.about"), key="about"), sg.Button(_t("generic.quit"), key="exit"), @@ -614,6 +645,13 @@ def main_gui(config_dict: dict, config_file: str, version_string: str): sg.Popup(_t("main_gui.select_backup"), keep_on_top=True) continue ls_window(config_dict, snapshot=values["snapshot-list"][0]) + if event == "delete": + if not values["snapshot-list"]: + sg.Popup(_t("main_gui.select_backup"), keep_on_top=True) + continue + delete_backup(config_dict, snapshot=values["snapshot-list"][0]) + # Make sure we trigger a GUI refresh after deletions + event = "state-button" if event == "configure": config_dict = config_gui(config_dict, config_file) # Make sure we trigger a GUI refresh when configuration is changed diff --git a/npbackup/translations/generic.en.yml b/npbackup/translations/generic.en.yml index 462ce44..f7e1a63 100644 --- a/npbackup/translations/generic.en.yml +++ b/npbackup/translations/generic.en.yml @@ -38,6 +38,10 @@ en: is_uptodate: Program Up to date succes: Succes + successfully: successfully failure: Failure - scheduled_task: Scheduled task \ No newline at end of file + scheduled_task: Scheduled task + + delete: Delete + deleted: deleted \ No newline at end of file diff --git a/npbackup/translations/generic.fr.yml b/npbackup/translations/generic.fr.yml index 18a82cc..05250b1 100644 --- a/npbackup/translations/generic.fr.yml +++ b/npbackup/translations/generic.fr.yml @@ -38,6 +38,10 @@ fr: is_uptodate: Logiciel à jour succes: Succès + successfully: avec succès failure: Echec - scheduled_task: Tâche planifiée \ No newline at end of file + scheduled_task: Tâche planifiée + + delete: Supprimer + deleted: éffacé \ No newline at end of file diff --git a/npbackup/translations/main_gui.en.yml b/npbackup/translations/main_gui.en.yml index 2a8a0b0..7fc4349 100644 --- a/npbackup/translations/main_gui.en.yml +++ b/npbackup/translations/main_gui.en.yml @@ -29,4 +29,6 @@ en: run_as: run as identified_by: identified by unknown_repo: Repo format unknown - repository_not_configured: Repository not configured \ No newline at end of file + repository_not_configured: Repository not configured + execute_operation: Executing operation + delete failed: Deletion failed. Please check the logs \ No newline at end of file diff --git a/npbackup/translations/main_gui.fr.yml b/npbackup/translations/main_gui.fr.yml index 91e8449..b0af2e0 100644 --- a/npbackup/translations/main_gui.fr.yml +++ b/npbackup/translations/main_gui.fr.yml @@ -29,4 +29,6 @@ fr: run_as: faite en tant que identified_by: identifiée en tant que unknown_repo: Format de dépot inconnu - repository_not_configured: Dépot non configuré \ No newline at end of file + repository_not_configured: Dépot non configuré + execute_operation: Opération en cours + delete_failed: Suppression impossible. Veuillez vérifier les journaux \ No newline at end of file