-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_new_directory_and_log_file.py
More file actions
82 lines (66 loc) · 3.01 KB
/
create_new_directory_and_log_file.py
File metadata and controls
82 lines (66 loc) · 3.01 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
# Standard library imports
import os
import shutil
import time
# Third-party library imports
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
from PIL import Image, ImageTk
from tkinter import simpledialog
# Local imports
from definitions import *
def create_new_directory_and_log_file(project_title, root, definitions, logger):
"""
Creates a new directory with a log.txt file and clears any prior interview data from the tables.
"""
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"):
with open("log.txt", "r") as f:
log_file_contents = f.readlines()
if len(log_file_contents) > 1 and messagebox.askyesno("Save Log", "Do you want to save the log file before continuing?"):
save_log_file()
# Create new directory
directory_name = f"{project_title} {time.strftime('%Y-%m-%d %H-%M-%S')}"
os.mkdir(directory_name)
global project_directory
project_directory = os.path.abspath(directory_name)
# Back up all data to an "emergency backups" directory
backup_directory_name = "emergency backups"
os.makedirs(os.path.join(backup_directory_name, time.strftime('%Y-%m-%d %H-%M-%S')))
files_to_backup = ["interview.txt", "log.txt", "outline.txt", "author_style.txt"]
for file in files_to_backup:
if os.path.exists(file):
shutil.copyfile(file, os.path.join(backup_directory_name, time.strftime('%Y-%m-%d %H-%M-%S'), file))
# Clear all prior interview data
clear_interview_data()
# Prompt user to enter project title
project_title = simpledialog.askstring("Project Title", "Please enter a title for your project")
if not project_title:
messagebox.showerror("Error", "Project title cannot be empty.")
return
# Create new directory
project_directory = os.path.join(os.getcwd(), project_title)
if os.path.exists(project_directory):
messagebox.showerror("Error", "A project with this title already exists.")
return
os.mkdir(project_directory)
# Create new log file
with open(os.path.join(project_directory, "log.txt"), "w") as f:
f.write("Generated by FoolsGoldPyWrite\n")
f.write("Program started\n")
# Update program state
update_program_state(project_directory, "new project")
# Update UI
project_title_label.configure(text=project_title)
new_project_button.configure(state="normal")
restore_button.configure(state="normal")
fiction_button.configure(state="normal")
biography_button.configure(state="normal")
# Update log file with work type
work_type = select_work_type()
update_log_file(work_type)
logger.setLevel(logging.INFO) # Enable logging again
# Display confirmation message
messagebox.showinfo( f"A {work_type} project has been restored.")