Skip to content

Commit a7476c2

Browse files
committed
Debugging restore_backup function
1 parent 54a5398 commit a7476c2

5 files changed

+23
-11
lines changed

main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,10 @@ def restore_backup(contact_manager):
335335

336336
# Validate the user's choice
337337
if 1 <= choice <= len(recent_backups):
338+
338339
backup_filename = recent_backups[choice - 1]
340+
339341
contact_manager.restore_backup(backup_filename)
340-
hf.show_info_message("Restoring from backup...")
341-
hf.show_success_message(f"Backup '{backup_filename}' successfully restored.\n")
342342
else:
343343
hf.show_error_message("Invalid selection.\n")
344344
except ValueError:
-102 Bytes
Binary file not shown.
344 Bytes
Binary file not shown.

modules/backup_manager.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ def get_backup_file(self, backup_filename):
7171
The full path to the backup file if it exists, otherwise None.
7272
"""
7373
# Construct the full path to the backup file
74-
backup_file = os.path.join(self.backup_folder, backup_filename)
74+
backup_file_path = os.path.join(self.backup_folder, backup_filename)
7575

7676
# Check if the backup file exists
77-
if os.path.exists(backup_file):
78-
return backup_file
77+
if os.path.exists(backup_file_path):
78+
return backup_file_path
7979
else:
80-
hf.show_error_message(f"Backup file {backup_filename} not found.")
80+
# hf.show_error_message(f"Backup file {backup_filename} not found.")
8181
return None
8282

8383
def list_recent_backups(self, n=3):
@@ -102,9 +102,11 @@ def list_recent_backups(self, n=3):
102102
# List all files that start with "contacts_backup_" in the backup folder
103103
backup_files = [file for file in os.listdir(self.backup_folder) if file.startswith("contacts_backup_")]
104104

105-
# Sort the files by their modification time in descending order
105+
# # Sort the files by their modification time in descending order
106106
backup_files.sort(key=lambda x: os.path.getmtime(os.path.join(self.backup_folder, x)), reverse=True)
107107

108-
# Return the most recent 'n' backup files
108+
# # Return the most recent 'n' backup files
109109
return backup_files[:n]
110+
# backups = sorted(os.listdir(self.backup_folder), reverse=True)
111+
# return backups[:n]
110112

modules/contact_manager.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ def _read_from_file(self, file_name):
4141
list | A list of Contact objects, or an empty list if an error occurs.
4242
"""
4343
try:
44+
4445
with open(file_name, "r", encoding="utf-8") as file:
4546
contacts_list = json.load(file) # Load JSON data from the file
47+
4648
# Convert JSON to Contact objects
4749
return [Contact(**data) for data in contacts_list]
4850

@@ -332,11 +334,19 @@ def restore_backup(self, backup_filename):
332334
"""
333335
# Get the backup file path using BackupManager
334336
backup_file = self.backup_manager.get_backup_file(backup_filename)
335-
337+
336338
# If the backup file exists, restore the contacts
337339
if backup_file:
338-
self.contacts = self._read_from_file(backup_filename)
339-
# hf.show_success_message(f"Backup {backup_filename} successfully restored.")
340+
341+
contacts = self._read_from_file(backup_file)
342+
343+
# Wenn Kontakte erfolgreich geladen wurden
344+
if contacts is not None:
345+
self.contacts = contacts
346+
hf.show_info_message("\nRestoring from backup...")
347+
hf.show_success_message(f"Backup '{backup_filename}' successfully restored.\n")
348+
else:
349+
hf.show_error_message(f"Failed to restore backup '{backup_filename}'.")
340350
else:
341351
hf.show_error_message(f"Unable to restore backup {backup_filename}.")
342352

0 commit comments

Comments
 (0)