-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtf_formatter.py
227 lines (185 loc) · 9.31 KB
/
rtf_formatter.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import os
import sys
import argparse
from tkinter import filedialog as fdialog
from tkinter import Tk
def auto_format_rtf(file_path, debug=False):
""" Input complete filepath to .rtf file
replaces all instances of "\\line" to "\\par".
writes new data to new file with "MODFIED" appended.
Prints debug messages to console if debug=True.
"""
# Separates file name and extension for processing later.
file_name, file_ext = os.path.splitext(os.path.basename(file_path))
# Verifies that file exists and is .rtf before starting
if os.path.exists(file_path) and file_ext == ".rtf":
if debug:
print("\nFile Operation Confirmed".format(
file_path=file_path))
print(" Modifiying \"{filename}\".".format(
filename=os.path.basename(file_path)))
# Opens file and copies data to text_data object.
with open(file_path, "r") as file:
text_data = file.read()
if debug:
print(" Successfully read data")
# Replaces the unwanted "\\line" with "\\par"
# Operation performed on the entire data set instead of line by line.
new_text_data = text_data.replace("\\line", "\\par")
if debug:
print(" Data format operation successful")
# Gets location of file
file_location = os.path.dirname(file_path)
# Creates new file name from original name.
new_file_name = file_name + " MODIFIED" + file_ext
# Creates new complete file path from new name and original path.
new_file = os.path.join(file_location, new_file_name)
# Creates new file @ new path and writes data to new file.
with open(new_file, "w+") as file:
file.write(new_text_data)
if debug:
print(" Created new file at \"{new_file}\"."
.format(new_file=new_file))
print(" Wrote data to \"{new_file_name}\".\n"
.format(new_file_name=new_file_name))
return new_file
if __name__ == '__main__':
# Initializes parser for commandline call and sets flags.
parser = argparse.ArgumentParser(description="Formats .rtf files for use "
"with ProPresenter6 import function. "
"Or optionally, you can run without "
"arguments and you will be brought to an "
"interactive commandline interface.")
parser.add_argument("-c", "--confirm", action="store_true",
help="Skips having to confirm processing on every "
"file")
parser.add_argument("-f", "--files", nargs="*",
help="Full file paths of all files "
"that you want to process")
args = parser.parse_args()
# If script is called from the commandline and supplied arguments.
# Iterates through arguments, applying processing as it goes.
if args.files is not None:
for file in args.files:
# Checks to see if the file exists.
if os.path.exists(file):
print("Modifiying file \"{filename}\"."
.format(filename=file))
# If the "confirm all" flag is not raised, will ask for user
# confirmation for each file before processing is applied.
if not args.confirm:
# Starts decision loop
# User must give valid answer for loop to exit.
confirmation = None
while confirmation is None:
print("\nAre you sure you would like to modify "
"\"{filename}\"? Please confirm. \n"
"(y/n)?".format(filename=file))
selection = input(">")
if selection == "n":
print("\nUser canceled processing on "
"\"{filename}\".\n"
.format(filename=file))
confirmation = False
elif selection == "y":
print("\nRecieved go-ahead for \"{filename}\"."
.format(filename=file))
confirmation = True
else:
print("\nInvalid Selection, please try again. \n")
# If user selects no for this file,
# the program will continue on to the next file.
if not confirmation:
continue
# Performs formatting on file with debugging enabled.
new_file_path = auto_format_rtf(file, debug=True)
# Checks if file was really created.
if os.path.exists(new_file_path):
print("New file created @ \"{file_path}\".\n"
.format(file_path=new_file_path))
else:
print("Error creating new file.\n")
# If file was not valid for program.
else:
print("\"{file_path}\" does not exist."
.format(file_path=file))
# End of program.
print("Instance terminated without any issues.")
# Starts the interactive CLI when script
# is called from the commandline with no arguments
else:
print("\nProPresenter RTF Autoformatter ©Midlight25 2019\n")
# Defining choices for use in CLI.
acceptable_exit_answers = ["quit", "q"]
acceptable_input_answers = ["input", "i"]
acceptable_cancel_answers = ["cancel", "c"]
currently_running = True
# Starts program loop with currently_running.
while currently_running:
print("Type (I)nput to select a file "
"or (Q)uit to exit the program:")
selection = input(">")
# Exit program if quit is passed to the CLI
if selection.lower() in acceptable_exit_answers:
sys.exit("Program exited by user")
# Starts file input dialog
elif selection.lower() in acceptable_input_answers:
# Removes an extra window that appears
# when the file dialog activates
root = Tk()
root.withdraw()
# Opens Documents Directory on Windows
if sys.platform.startswith('win32'):
default_directory = os.path.join(
os.getenv('USERPROFILE'), "Documents")
current_selected_file = fdialog.askopenfilename(
initialdir=default_directory,
title="Select file",
filetypes=[("Rich Text Format files", "*.rtf")])
# Opens Desktop Directory on Mac OS X
elif sys.platform.startswith('darwin'):
default_directory = os.path.join(
os.getenv("HOME"), "Desktop")
current_selected_file = fdialog.askopenfilename(
initialdir=default_directory,
title="Select file",
filetypes=[("Rich Text Format files", "*.rtf")])
# Any Unrecognized OS - Need to add Linux support
else:
current_selected_file = fdialog.askopenfilename(
initialdir="/",
title="Select file",
filetypes=[("Rich Text Format files", "*.rtf")])
# When user cancels file selection, tk returns empty string.
if current_selected_file == "":
print("User canceled file operation, "
"returning to main menu.\n")
continue
# Initiates confirmation session
confirm = None
while confirm is None:
print("\nYou selected \"{file}\" for formating, "
"is this (OK)? Or type (C)ancel to cancel."
.format(file=os.path.basename
(current_selected_file)))
user_warning = input(">")
# Performs processing if user gives the ok.
if user_warning.lower() == "ok":
try:
auto_format_rtf(current_selected_file, debug=True)
except:
print("\nProgram was unable to create new file,"
" please try again.\n")
confirm = True
# Cancels operation if user requests it.
elif user_warning.lower() in acceptable_cancel_answers:
print("\nUser canceled operation. \n")
confirm = False
# Trys again if user gives invalid answer.
else:
print("\nInvalid Input, please try again.")
# Asks user to try again on the "input, quit" selection.
else:
print("\nInvalid Input, please try again\n")
# Signed Midlight25 2019
sys.exit("\nSystem crashed.")