Skip to content

Commit afdd415

Browse files
authored
Version 0.4a
Release 0.4a merge
2 parents 835eecf + e9f3434 commit afdd415

File tree

1 file changed

+62
-38
lines changed

1 file changed

+62
-38
lines changed

rtf_formatter.py

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66

77

88
def auto_format_rtf(file_path, debug=False):
9-
"""Takes in complete filepath as input and replaces all
10-
line breaks with paragraph breaks and writes to
11-
file with filename + "MODIFIED"
12-
returns the new file path
9+
""" Input complete filepath to .rtf file
10+
replaces all instances of "\\line" to "\\par".
11+
writes new data to new file with "MODFIED" appended.
12+
Prints debug messages to console if debug=True.
1313
"""
14-
# Gets file name and extension for creation of new file name and path
15-
# It's here because the next if statement checks if this file is an ".rtf"
14+
# Separates file name and extension for processing later.
1615
file_name, file_ext = os.path.splitext(os.path.basename(file_path))
1716

1817
# Verifies that file exists and is .rtf before starting
@@ -23,38 +22,42 @@ def auto_format_rtf(file_path, debug=False):
2322
print(" Modifiying \"{filename}\".".format(
2423
filename=os.path.basename(file_path)))
2524

26-
# Opens file and copies data to text_data.
25+
# Opens file and copies data to text_data object.
2726
with open(file_path, "r") as file:
2827
text_data = file.read()
2928
if debug:
3029
print(" Successfully read data")
3130

32-
# Formats data and adds it to list for appending.
33-
# The double line will only be read as one by python.
31+
# Replaces the unwanted "\\line" with "\\par"
32+
# Operation performed on the entire data set instead of line by line.
3433
new_text_data = text_data.replace("\\line", "\\par")
3534
if debug:
3635
print(" Data format operation successful")
3736

38-
# Creates new file name and path from original file data.
37+
# Gets location of file
3938
file_location = os.path.dirname(file_path)
39+
40+
# Creates new file name from original name.
4041
new_file_name = file_name + " MODIFIED" + file_ext
42+
43+
# Creates new complete file path from new name and original path.
4144
new_file = os.path.join(file_location, new_file_name)
42-
if debug:
43-
print(" Created new file at \"{new_file}\"."
44-
.format(new_file=new_file))
4545

46-
# Writes data to new file
46+
# Creates new file @ new path and writes data to new file.
4747
with open(new_file, "w+") as file:
4848
file.write(new_text_data)
49-
if debug:
50-
print(" Wrote data to \"{new_file_name}\".\n"
51-
.format(new_file_name=new_file_name))
49+
if debug:
50+
print(" Created new file at \"{new_file}\"."
51+
.format(new_file=new_file))
52+
print(" Wrote data to \"{new_file_name}\".\n"
53+
.format(new_file_name=new_file_name))
5254

5355
return new_file
5456

5557

5658
if __name__ == '__main__':
5759

60+
# Initializes parser for commandline call and sets flags.
5861
parser = argparse.ArgumentParser(description="Formats .rtf files for use "
5962
"with ProPresenter6 import function. "
6063
"Or optionally, you can run without "
@@ -70,17 +73,23 @@ def auto_format_rtf(file_path, debug=False):
7073

7174
args = parser.parse_args()
7275

73-
# If script is passed to commandline w/ arguments
74-
# interates through the list of arguments and applies function as it goes.
76+
# If script is called from the commandline and supplied arguments.
77+
# Iterates through arguments, applying processing as it goes.
7578
if args.files is not None:
7679
for file in args.files:
80+
81+
# Checks to see if the file exists.
7782
if os.path.exists(file):
7883
print("Modifiying file \"{filename}\"."
7984
.format(filename=file))
80-
85+
# If the "confirm all" flag is not raised, will ask for user
86+
# confirmation for each file before processing is applied.
8187
if not args.confirm:
82-
descision = None
83-
while descision is None:
88+
89+
# Starts decision loop
90+
# User must give valid answer for loop to exit.
91+
confirmation = None
92+
while confirmation is None:
8493
print("\nAre you sure you would like to modify "
8594
"\"{filename}\"? Please confirm. \n"
8695
"(y/n)?".format(filename=file))
@@ -90,59 +99,72 @@ def auto_format_rtf(file_path, debug=False):
9099
print("\nUser canceled processing on "
91100
"\"{filename}\".\n"
92101
.format(filename=file))
93-
descision = False
102+
confirmation = False
94103

95104
elif selection == "y":
96105
print("\nRecieved go-ahead for \"{filename}\"."
97106
.format(filename=file))
98-
descision = True
107+
confirmation = True
99108

100109
else:
101110
print("\nInvalid Selection, please try again. \n")
102111

103-
if not descision:
112+
# If user selects no for this file,
113+
# the program will continue on to the next file.
114+
if not confirmation:
104115
continue
105116

117+
# Performs formatting on file with debugging enabled.
106118
new_file_path = auto_format_rtf(file, debug=True)
119+
120+
# Checks if file was really created.
107121
if os.path.exists(new_file_path):
108122
print("New file created @ \"{file_path}\".\n"
109123
.format(file_path=new_file_path))
110124
else:
111125
print("Error creating new file.\n")
112126

127+
# If file was not valid for program.
113128
else:
114129
print("\"{file_path}\" does not exist."
115130
.format(file_path=file))
116131

132+
# End of program.
117133
print("Instance terminated without any issues.")
118134

119-
# Starts the CLI Environment - will rework with Argparse library
135+
# Starts the interactive CLI when script
136+
# is called from the commandline with no arguments
120137
else:
121138
print("\nProPresenter RTF Autoformatter ©Midlight25 2019\n")
139+
140+
# Defining choices for use in CLI.
122141
acceptable_exit_answers = ["quit", "q"]
123142
acceptable_input_answers = ["input", "i"]
124143
acceptable_cancel_answers = ["cancel", "c"]
125144
currently_running = True
126145

127-
# Processing loop
146+
# Starts program loop with currently_running.
128147
while currently_running:
129148
print("Type (I)nput to select a file "
130149
"or (Q)uit to exit the program:")
131150
selection = input(">")
132151

152+
# Exit program if quit is passed to the CLI
133153
if selection.lower() in acceptable_exit_answers:
134154
sys.exit("Program exited by user")
135155

156+
# Starts file input dialog
136157
elif selection.lower() in acceptable_input_answers:
158+
137159
# Removes an extra window that appears
138160
# when the file dialog activates
139161
root = Tk()
140162
root.withdraw()
141163

142164
# Opens Documents Directory on Windows
143165
if sys.platform.startswith('win32'):
144-
default_directory = os.path.join(os.getenv('USERPROFILE'),
145-
"Documents")
166+
default_directory = os.path.join(
167+
os.getenv('USERPROFILE'), "Documents")
146168
current_selected_file = fdialog.askopenfilename(
147169
initialdir=default_directory,
148170
title="Select file",
@@ -171,32 +193,34 @@ def auto_format_rtf(file_path, debug=False):
171193
continue
172194

173195
# Initiates confirmation session
174-
end_session = False
175-
while not end_session:
196+
confirm = None
197+
while confirm is None:
176198
print("\nYou selected \"{file}\" for formating, "
177199
"is this (OK)? Or type (C)ancel to cancel."
178200
.format(file=os.path.basename
179201
(current_selected_file)))
180202
user_warning = input(">")
181203

204+
# Performs processing if user gives the ok.
182205
if user_warning.lower() == "ok":
183206
try:
184207
auto_format_rtf(current_selected_file, debug=True)
185-
end_session = True
186208
except:
187209
print("\nProgram was unable to create new file,"
188210
" please try again.\n")
189-
end_session = True
211+
confirm = True
190212

213+
# Cancels operation if user requests it.
191214
elif user_warning.lower() in acceptable_cancel_answers:
192-
print("\nUser canceled operation.")
193-
end_session = True
215+
print("\nUser canceled operation. \n")
216+
confirm = False
194217

218+
# Trys again if user gives invalid answer.
195219
else:
196-
print("\nUnable to understand user input, "
197-
"please try again.")
220+
print("\nInvalid Input, please try again.")
198221

222+
# Asks user to try again on the "input, quit" selection.
199223
else:
200-
print("Did not understand user input. Please try again\n")
224+
print("\nInvalid Input, please try again\n")
201225

202226
sys.exit("\nSystem crashed.")

0 commit comments

Comments
 (0)