Skip to content

Commit 835eecf

Browse files
authored
v0.4 update - Argparse and Debug Option Overhaul
Release 0.4 pull request
2 parents 9555782 + b244508 commit 835eecf

File tree

4 files changed

+105
-44
lines changed

4 files changed

+105
-44
lines changed

FUTURE

Lines changed: 0 additions & 3 deletions
This file was deleted.

FUTURE.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1. A GUI using PyQT
2+
2. Redo command-line interface with Argparse library
3+
3. Add default directory support for Linux
4+
4. Create the correct exceptions for the program so that flake8 gets off my ass.

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# ProPresenter-Rtf-Autoformatter
22
Python script, takes in input of rtf file you need to format and outputs formatted rtf file for ProPresenter import with line break delimiters.
33

4-
Background:
5-
My church uses ProPresenter 5 to display the lyrics to hymns for the congregation. Every Saturday, we recieve the lyrics in an email, which we then copy to an .rtf file using Word. Sometimes, the copying does not go through correctly and we have many "line breaks" instead of "paragraph breaks" in our file. ProPresenter separates the slides based on the "paragraph breaks," which means in order for the lyrics to display correctly on the slide, all of the "line breaks" have to be changed to "paragraph breaks." Until now, that had to be done manually. The task is tedious and time consuming; so in a fit of rage, I sat down and wrote a function that would take care of doing that task for me. This is what you see here.
4+
## Background:
5+
My church uses ProPresenter 5 to display the lyrics to hymns for the congregation. Every Saturday, we receive the lyrics in an email, which we then copy to an .rtf file using Word. Sometimes, the copying does not go through correctly and we have many "line breaks" instead of "paragraph breaks" in our file. ProPresenter separates the slides based on the "paragraph breaks," which means in order for the lyrics to display correctly on the slide, all of the "line breaks" have to be changed to "paragraph breaks." Until now, that had to be done manually. The task is tedious and time consuming; so in a fit of rage, I sat down and wrote a function that would take care of doing that task for me. This is what you see here.
66

77
This python script is written in such a way that if you wanted to, you could also import the function that I wrote to use in your code. If the script is run natively, it will perform the function on its own, so it's both a program and a module. Currently, I only have the command-line interface programed, and it's simple. Just write the full path of each file after the program line. It can accept an unlimited number of files by calling the program name from the command line and calling files after the program. There is also a simple stand alone CLI that can be accessed by running the file or running it from the command line without any arguments.
88

9-
Disclaimer:
10-
The function will only convert line-breaks to paragraph breaks so that the file will work correctly in ProPresenter, so I apologise if you were searching for something else.
9+
## Disclaimer:
10+
The function will only convert line-breaks to paragraph breaks so that the file will work correctly in ProPresenter, so I apologize if you were searching for something else.
1111

12-
For any questions, just contact me through the channels that github provides. *At this point, I an inexperienced programer and this was my first project so please be kind. Thank you!
12+
For any questions, just contact me through the channels that GitHub provides. *At this point, I an inexperienced programmer and this was my first project so please be kind. Thank you!

rtf_formatter.py

Lines changed: 96 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import os
22
import sys
3+
import argparse
34
from tkinter import filedialog as fdialog
45
from tkinter import Tk
56

67

7-
def auto_format_rtf(file_path):
8+
def auto_format_rtf(file_path, debug=False):
89
"""Takes in complete filepath as input and replaces all
910
line breaks with paragraph breaks and writes to
1011
file with filename + "MODIFIED"
@@ -16,72 +17,125 @@ def auto_format_rtf(file_path):
1617

1718
# Verifies that file exists and is .rtf before starting
1819
if os.path.exists(file_path) and file_ext == ".rtf":
19-
print("Checks passed on \"{file_path}\", beginning process.".format(file_path=file_path))
20-
print("Modifiying \"{file_name}{file_ext}\".".format(file_name=file_name,
21-
file_ext=file_ext))
20+
if debug:
21+
print("\nFile Operation Confirmed".format(
22+
file_path=file_path))
23+
print(" Modifiying \"{filename}\".".format(
24+
filename=os.path.basename(file_path)))
2225

2326
# Opens file and copies data to text_data.
2427
with open(file_path, "r") as file:
2528
text_data = file.read()
26-
print("Opened file and read data to text_data.")
29+
if debug:
30+
print(" Successfully read data")
2731

2832
# Formats data and adds it to list for appending.
29-
new_text_data = text_data.replace("\line", "\par")
30-
print("Formatted data")
33+
# The double line will only be read as one by python.
34+
new_text_data = text_data.replace("\\line", "\\par")
35+
if debug:
36+
print(" Data format operation successful")
3137

3238
# Creates new file name and path from original file data.
3339
file_location = os.path.dirname(file_path)
3440
new_file_name = file_name + " MODIFIED" + file_ext
3541
new_file = os.path.join(file_location, new_file_name)
36-
print("Created new file name, new file at \"{new_file}\"".format(new_file=new_file))
42+
if debug:
43+
print(" Created new file at \"{new_file}\"."
44+
.format(new_file=new_file))
3745

3846
# Writes data to new file
3947
with open(new_file, "w+") as file:
4048
file.write(new_text_data)
41-
print("Wrote data to \"{new_file_name}\".\n".format(new_file_name=new_file_name))
49+
if debug:
50+
print(" Wrote data to \"{new_file_name}\".\n"
51+
.format(new_file_name=new_file_name))
4252

4353
return new_file
4454

4555

4656
if __name__ == '__main__':
4757

58+
parser = argparse.ArgumentParser(description="Formats .rtf files for use "
59+
"with ProPresenter6 import function. "
60+
"Or optionally, you can run without "
61+
"arguments and you will be brought to an "
62+
"interactive commandline interface.")
63+
64+
parser.add_argument("-c", "--confirm", action="store_true",
65+
help="Skips having to confirm processing on every "
66+
"file")
67+
parser.add_argument("-f", "--files", nargs="*",
68+
help="Full file paths of all files "
69+
"that you want to process")
70+
71+
args = parser.parse_args()
72+
4873
# If script is passed to commandline w/ arguments
4974
# interates through the list of arguments and applies function as it goes.
50-
if len(sys.argv) > 1:
51-
for arg in sys.argv[1:]:
52-
if os.path.exists(arg):
53-
print("Modifiying file \"{filename}\".\n".format(filename=arg))
54-
new_file_path = auto_format_rtf(arg)
55-
75+
if args.files is not None:
76+
for file in args.files:
77+
if os.path.exists(file):
78+
print("Modifiying file \"{filename}\"."
79+
.format(filename=file))
80+
81+
if not args.confirm:
82+
descision = None
83+
while descision is None:
84+
print("\nAre you sure you would like to modify "
85+
"\"{filename}\"? Please confirm. \n"
86+
"(y/n)?".format(filename=file))
87+
selection = input(">")
88+
89+
if selection == "n":
90+
print("\nUser canceled processing on "
91+
"\"{filename}\".\n"
92+
.format(filename=file))
93+
descision = False
94+
95+
elif selection == "y":
96+
print("\nRecieved go-ahead for \"{filename}\"."
97+
.format(filename=file))
98+
descision = True
99+
100+
else:
101+
print("\nInvalid Selection, please try again. \n")
102+
103+
if not descision:
104+
continue
105+
106+
new_file_path = auto_format_rtf(file, debug=True)
56107
if os.path.exists(new_file_path):
57-
print("New file created @ \"{file_path}\".\n".format(
58-
file_path=new_file_path))
108+
print("New file created @ \"{file_path}\".\n"
109+
.format(file_path=new_file_path))
59110
else:
60111
print("Error creating new file.\n")
61112

62113
else:
63-
print(
64-
"\"{file_path}\" does not exist, file not created.".format(file_path=arg))
114+
print("\"{file_path}\" does not exist."
115+
.format(file_path=file))
116+
117+
print("Instance terminated without any issues.")
65118

66119
# Starts the CLI Environment - will rework with Argparse library
67120
else:
68-
69-
print("\nProPresenter RTF Autoformatter © Midlight25 2019\n")
121+
print("\nProPresenter RTF Autoformatter ©Midlight25 2019\n")
70122
acceptable_exit_answers = ["quit", "q"]
71123
acceptable_input_answers = ["input", "i"]
72124
acceptable_cancel_answers = ["cancel", "c"]
73125
currently_running = True
74126

75127
# Processing loop
76-
while currently_running == True:
77-
print("Type (I)nput to select your file or (Q)uit to exit the program:")
78-
selection = input("")
128+
while currently_running:
129+
print("Type (I)nput to select a file "
130+
"or (Q)uit to exit the program:")
131+
selection = input(">")
79132

80133
if selection.lower() in acceptable_exit_answers:
81134
sys.exit("Program exited by user")
82135

83136
elif selection.lower() in acceptable_input_answers:
84-
# Removes an extra window that appears when the file dialog activates
137+
# Removes an extra window that appears
138+
# when the file dialog activates
85139
root = Tk()
86140
root.withdraw()
87141

@@ -96,7 +150,8 @@ def auto_format_rtf(file_path):
96150

97151
# Opens Desktop Directory on Mac OS X
98152
elif sys.platform.startswith('darwin'):
99-
default_directory = os.path.join(os.getenv("HOME"), "Desktop")
153+
default_directory = os.path.join(
154+
os.getenv("HOME"), "Desktop")
100155
current_selected_file = fdialog.askopenfilename(
101156
initialdir=default_directory,
102157
title="Select file",
@@ -111,32 +166,37 @@ def auto_format_rtf(file_path):
111166

112167
# When user cancels file selection, tk returns empty string.
113168
if current_selected_file == "":
114-
print("User canceled file operation, returning to main menu.\n")
169+
print("User canceled file operation, "
170+
"returning to main menu.\n")
115171
continue
116172

117173
# Initiates confirmation session
118174
end_session = False
119-
while end_session == False:
120-
user_warning = input("\nYou selected \"{file}\" for formating, is this (OK)? Or type (C)ancel to cancel:\n".format(
121-
file=os.path.basename(current_selected_file)))
175+
while not end_session:
176+
print("\nYou selected \"{file}\" for formating, "
177+
"is this (OK)? Or type (C)ancel to cancel."
178+
.format(file=os.path.basename
179+
(current_selected_file)))
180+
user_warning = input(">")
122181

123182
if user_warning.lower() == "ok":
124183
try:
125-
auto_format_rtf(current_selected_file)
184+
auto_format_rtf(current_selected_file, debug=True)
126185
end_session = True
127186
except:
128-
print("Program was unable to create new file, \
129-
please try again.\n")
187+
print("\nProgram was unable to create new file,"
188+
" please try again.\n")
130189
end_session = True
131190

132191
elif user_warning.lower() in acceptable_cancel_answers:
133-
print("User canceled operation.")
192+
print("\nUser canceled operation.")
134193
end_session = True
135194

136195
else:
137-
print("Unable to understand user input, please try again.")
196+
print("\nUnable to understand user input, "
197+
"please try again.")
138198

139199
else:
140200
print("Did not understand user input. Please try again\n")
141201

142-
sys.exit("System crashed.")
202+
sys.exit("\nSystem crashed.")

0 commit comments

Comments
 (0)