-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore_data.py
More file actions
115 lines (93 loc) · 4.56 KB
/
restore_data.py
File metadata and controls
115 lines (93 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import csv
import os
import shutil
import time
import logging
from tkinter import messagebox, filedialog
from datetime import datetime
# If Emergency_backup, clear_interview_data, create_new_directory_and_log_file,
# set_project_type, check_api_key are defined in other modules, import them as well
# from your_module_name import Emergency_backup, clear_interview_data, ...
#emergency backup code
def restore_data(logger):
logger.setLevel(logging.CRITICAL) # Disable logging
# Prompt to save log file if it exists and has any information other than lines containing "program started"
if os.path.exists("log.txt"):
try:
with open("log.txt", "r") as f:
log_file_contents = f.readlines()
if len(log_file_contents) > 1:
save_log_prompt = messagebox.askyesno("Save Log", "Do you want to save the log file before continuing?")
if save_log_prompt:
save_log_file()
except FileNotFoundError:
# the file could not be found
pass
Emergency_backup()
# Warn that restoring data will overwrite current project data
if os.path.exists("log.txt") or os.path.exists("interview.txt") or os.path.exists("outline.txt") or os.path.exists("author_style.txt"):
confirm_restore = messagebox.askyesno("Confirm Data Restore",
"Restoring data will overwrite current project data. Do you want to continue?")
if not confirm_restore:
return
# Clear all prior interview data and create new directory and log file
clear_interview_data()
create_new_directory_and_log_file()
# Open browse file dialog to select log file to restore
filetypes = (("Text files", "*.txt"), ("All files", "*.*"))
selected_file = filedialog.askopenfilename(title="Select Log File", filetypes=filetypes)
# Read log file to determine whether it was generated in this program and whether it's fiction or non-fiction or biography
if not os.path.exists(selected_file):
return
with open(selected_file, "r") as f:
first_line = f.readline().strip()
second_line = f.readline().strip()
if "logger.info('Generated by FoolsGoldPyWrite')" not in first_line:
messagebox.showerror("Invalid Log File", "The selected file was not generated by FoolsGoldPyWrite.")
return
project_type = None
if "Fiction" in second_line:
project_type = "Fiction"
elif "Non-Fiction" in second_line:
project_type = "Non-Fiction"
elif "Biography" in second_line:
project_type = "Biography"
else:
messagebox.showerror("Invalid Log File", "Unable to determine project type from the log file.")
return
set_project_type(project_type)
# Restore data from the log file
with open(selected_file, "r") as f:
interview_data = []
for line in f:
# Check if line contains interview information
if "Interview:" in line:
# Parse interview information from the line
interview_info = line.split(":")[1].strip().split(",")
interview_data.append(interview_info)
# Write interview data to interview.txt
if interview_data:
with open("interview.txt", "a") as f:
writer = csv.writer(f)
for row in interview_data:
writer.writerow(row)
if os.path.exists("outline.txt"):
shutil.copyfile("outline.txt", "outline_backup.txt")
with open("outline.txt", "r") as f:
outline_contents = f.read()
with open("outline_backup.txt", "a") as f:
f.write(f"\n\nBackup created on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n{outline_contents}")
with open("outline.txt", "w") as f:
f.write("")
if os.path.exists("author_style.txt"):
shutil.copyfile("author_style.txt", "author_style_backup.txt")
with open("author_style.txt", "r") as f:
author_style_contents = f.read()
with open("author_style_backup.txt", "a") as f:
f.write(f"\n\nBackup created on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n{author_style_contents}")
with open("author_style.txt", "w") as f:
f.write("")
# Ask user to check API key
check_api_key()
logger.setLevel(logging.INFO) # Enable logging again
messagebox.showinfo("Success", "Data has been restored from the log file.")