Skip to content

Commit

Permalink
refactor with Step_Frame and Step
Browse files Browse the repository at this point in the history
  • Loading branch information
lversaw committed Apr 3, 2024
1 parent c9fbd29 commit f027bb9
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 219 deletions.
55 changes: 15 additions & 40 deletions usfm/g_MarkParagraphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ def __init__(self, mainframe, mainapp):
self.frame = MarkParagraphs_Frame(mainframe, self)
self.frame.grid(row=1, column=0, sticky="nsew")

def name(self):
return stepname

def onExecute(self, values):
self.enablebutton(2, False)
self.enablebutton(3, False)
self.values = values
count = 2
count = 1
if not values['filename']:
count = g_util.count_files(values['source_dir'], ".*sfm$") * 2
count = g_util.count_files(values['source_dir'], ".*sfm$")
self.script = "mark_paragraphs"
self.mainapp.execute_script(self.script, count)
self.mainapp.execute_script(self.script, count*2)
self.frame.clear_status()

# Runs the revertChanges script to revert mark_paragraphs changes.
Expand All @@ -52,12 +57,14 @@ def onScriptEnd(self, status):
else:
msg = "No issues reported. This is a good time to reverify the USFM files."
self.frame.show_progress(msg)
self.enablebutton(2, True)
self.enablebutton(3, True)
self.enablebutton(4, True)
self.frame.onScriptEnd(nIssues)

class MarkParagraphs_Frame(ttk.Frame):
class MarkParagraphs_Frame(g_step.Step_Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
super().__init__(parent, controller)

self.source_dir = StringVar()
self.model_dir = StringVar()
Expand All @@ -69,7 +76,6 @@ def __init__(self, parent, controller):
var.trace_add("write", self._onChangeEntry)
self.columnconfigure(3, weight=1) # keep column 1 from expanding
self.columnconfigure(4, minsize=115)
self.rowconfigure(88, minsize=170, weight=1) # let the message expand vertically

model_dir_label = ttk.Label(self, text="Location of model files:", width=21)
model_dir_label.grid(row=3, column=1, sticky=(W,E), pady=2)
Expand All @@ -78,14 +84,14 @@ def __init__(self, parent, controller):
model_dir__Tip = Hovertip(self.model_dir_entry, hover_delay=500,
text="Folder containing USFM files with well marked paragraphs, e.g. English UDB folder")
model_dir_find = ttk.Button(self, text="...", width=2, command=self._onFindModelDir)
model_dir_find.grid(row=3, column=4, sticky=W, padx=8)
model_dir_find.grid(row=3, column=4, sticky=W)

source_dir_label = ttk.Label(self, text="Location of files\n to be marked:", width=15)
source_dir_label.grid(row=4, column=1, sticky=W, pady=2)
self.source_dir_entry = ttk.Entry(self, width=43, textvariable=self.source_dir)
self.source_dir_entry.grid(row=4, column=2, columnspan=3, sticky=W)
src_dir_find = ttk.Button(self, text="...", width=2, command=self._onFindSrcDir)
src_dir_find.grid(row=4, column=4, sticky=W, padx=8)
src_dir_find.grid(row=4, column=4, sticky=W)

file_label = ttk.Label(self, text="File name:", width=20)
file_label.grid(row=5, column=1, sticky=W, pady=2)
Expand Down Expand Up @@ -117,15 +123,6 @@ def __init__(self, parent, controller):
sentence_sensitive_Tip = Hovertip(sentence_sensitive_checkbox, hover_delay=500,
text="Only insert \p marks *between punctuated sentences. (Recommended)")

self.message_area = Text(self, height=10, width=30, wrap="word")
self.message_area['borderwidth'] = 2
self.message_area['relief'] = 'sunken'
self.message_area['background'] = 'grey97'
self.message_area.grid(row=88, column=1, columnspan=5, sticky='nsew', pady=6)
ys = ttk.Scrollbar(self, orient = 'vertical', command = self.message_area.yview)
ys.grid(column = 6, row = 88, sticky = 'ns')
self.message_area['yscrollcommand'] = ys.set

self.model_dir_entry.focus()

def show_values(self, values):
Expand All @@ -148,23 +145,13 @@ def show_values(self, values):
self.controller.showbutton(5, ">>>", tip="Verify manifest", cmd=self._onNext)
self._set_button_status()

# Displays status messages from the running script.
def show_progress(self, status):
self.message_area.insert('end', status + '\n')
self.message_area.see('end')
self.controller.enablebutton(2, False)
self.controller.enablebutton(3, False)

def onScriptEnd(self, nIssues):
issuespath = os.path.join(self.values['source_dir'], "issues.txt")
if nIssues > 0:
self.message_area.insert('end', "Now issues.txt contains the list of issues encountered in marking paragraphs.\n")
self.message_area.insert('end', "Resolve as appropriate.\n")
self.message_area.see('end')
self.message_area['state'] = DISABLED # prevents insertions to message area
self.controller.enablebutton(2, True)
self.controller.enablebutton(3, True)
self.controller.enablebutton(4, True)

# Called by the controller when script execution begins.
def clear_status(self):
Expand All @@ -181,10 +168,6 @@ def _save_values(self):
self.controller.mainapp.save_values(stepname, self.values)
self._set_button_status()

def _onExecute(self, *args):
self._save_values()
self.controller.onExecute(self.values)

def _onFindModelDir(self, *args):
self.controller.askdir(self.model_dir)
def _onFindSrcDir(self, *args):
Expand All @@ -199,14 +182,6 @@ def _onOpenIssues(self, *args):
path = os.path.join(self.values['source_dir'], "issues.txt")
os.startfile(path)

def _onBack(self, *args):
self.controller.enablebutton(4, False)
self._save_values()
self.controller.onBack()
def _onNext(self, *args):
self.controller.enablebutton(4, False)
self._save_values()
self.controller.onNext()
def _onUndo(self, *args):
self._save_values()
self.controller.revertChanges()
Expand Down
71 changes: 22 additions & 49 deletions usfm/g_plaintext2usfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,74 +20,66 @@ def __init__(self, mainframe, mainapp):
self.frame = Plaintext2Usfm_Frame(parent=mainframe, controller=self)
self.frame.grid(row=1, column=0, sticky="nsew")

def name(self):
return stepname

def onExecute(self, values):
self.values = values
count = g_util.count_files(values['source_dir'], ".*txt$")
self.enablebutton(2, False)
count = 1
if not values['filename']:
count = g_util.count_files(values['source_dir'], ".*txt$")
self.mainapp.execute_script("plaintext2usfm", count)
self.frame.clear_status()
def onNext(self):
copyparms = {'source_dir': self.values['target_dir']}
self.mainapp.step_next(copyparms)
def onSkip(self):
self.mainapp.step_next()

# Called by the main app.
def onScriptEnd(self, status: str):
if not status:
status = f"The conversion is done.\nAdvance to USFM verification and cleanup."
self.frame.show_progress(status)
self.frame.onScriptEnd()
self.enablebutton(2, True)

class Plaintext2Usfm_Frame(ttk.Frame):
class Plaintext2Usfm_Frame(g_step.Step_Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
super().__init__(parent, controller)

self.filename = StringVar()
self.source_dir = StringVar()
self.target_dir = StringVar()
for var in (self.source_dir, self.target_dir):
var.trace_add("write", self._onChangeEntry)
for col in [2,3]:
for col in [3,4]:
self.columnconfigure(col, weight=1) # keep column 1 from expanding
self.columnconfigure(4, minsize=94)
self.rowconfigure(88, minsize=170, weight=1) # let the message expand vertically

source_dir_label = ttk.Label(self, text="Location of text files:", width=20)
source_dir_label.grid(row=3, column=1, sticky=W, pady=2)
source_dir_entry = ttk.Entry(self, width=45, textvariable=self.source_dir)
source_dir_entry = ttk.Entry(self, width=42, textvariable=self.source_dir)
source_dir_entry.grid(row=3, column=2, columnspan=3, sticky=W)
target_dir_Tip = Hovertip(source_dir_entry, hover_delay=1000,
text="Folder containing the files to be converted")
src_dir_find = ttk.Button(self, text="...", width=2, command=self._onFindSrcDir)
src_dir_find.grid(row=3, column=4, sticky=W, padx=1)
src_dir_find.grid(row=3, column=4, sticky=W, padx=5)

file_label = ttk.Label(self, text="File name:", width=20)
file_label.grid(row=4, column=1, sticky=W, pady=2)
file_entry = ttk.Entry(self, width=20, textvariable=self.filename)
file_entry.grid(row=4, column=2, columnspan=3, sticky=W)
file_entry.grid(row=4, column=2, sticky=W)
file_Tip = Hovertip(file_entry, hover_delay=500,
text="Leave filename blank to convert all .txt files in the folder.")
file_find = ttk.Button(self, text="...", width=2, command=self._onFindFile)
file_find.grid(row=4, column=3, sticky=W, padx=14)
file_find.grid(row=4, column=3, sticky=W, padx=8)

target_dir_label = ttk.Label(self, text="Location for .usfm files:", width=21)
target_dir_label.grid(row=5, column=1, sticky=W, pady=2)
target_dir_entry = ttk.Entry(self, width=45, textvariable=self.target_dir)
target_dir_entry = ttk.Entry(self, width=42, textvariable=self.target_dir)
target_dir_entry.grid(row=5, column=2, columnspan=3, sticky=W)
target_dir_Tip = Hovertip(target_dir_entry, hover_delay=1000,
text="Folder for the new usfm files. The folder will be created if it doesn't exist.")
target_dir_find = ttk.Label(self, text="(may be new)")
target_dir_find.grid(row=5, column=4, sticky=W, padx=0)

self.message_area = Text(self, height=10, width=30, wrap="none")
self.message_area['borderwidth'] = 2
self.message_area['relief'] = 'sunken'
self.message_area['background'] = 'grey97'
self.message_area.grid(row=88, column=1, columnspan=4, sticky='nsew', pady=6)
ys = ttk.Scrollbar(self, orient = 'vertical', command = self.message_area.yview)
ys.grid(column = 5, row = 88, sticky = 'ns')
self.message_area['yscrollcommand'] = ys.set
target_dir_find = ttk.Button(self, text="...", width=2, command=self._onFindTargetDir)
target_dir_find.grid(row=5, column=4, sticky=W, padx=5)

source_dir_entry.focus()

Expand All @@ -107,17 +99,6 @@ def show_values(self, values):
self.controller.showbutton(5, ">>>", tip="Verify USFM", cmd=self._onSkip)
self._set_button_status()

# Displays status message from the running script.
def show_progress(self, status):
self.message_area.insert('end', status + '\n')
self.message_area.see('end')
self.controller.enablebutton(2, False)

def onScriptEnd(self):
self.message_area['state'] = DISABLED # prevents insertions to message area
self.controller.enablebutton(2, True)
self.controller.enablebutton(3, True)

# Called by the controller when script execution begins.
def clear_status(self):
self.message_area['state'] = NORMAL # enables insertions to message area
Expand All @@ -133,6 +114,8 @@ def _save_values(self):

def _onFindSrcDir(self, *args):
self.controller.askdir(self.source_dir)
def _onFindTargetDir(self, *args):
self.controller.askdir(self.target_dir)
def _onFindFile(self, *args):
path = filedialog.askopenfilename(initialdir=self.source_dir.get(), title = "Select text file with Scripture text",
filetypes=[('Text file', '*.txt')])
Expand All @@ -145,18 +128,8 @@ def _onOpenTextDir(self, *args):
def _onOpenTargetDir(self, *args):
self._save_values()
os.startfile(self.values['target_dir'])
def _onBack(self, *args):
self._save_values()
self.controller.onBack()
def _onSkip(self, *args):
self._save_values()
self.controller.onSkip()
def _onNext(self, *args):
self._save_values()
self.controller.onNext()
def _onExecute(self, *args):
self._save_values()
self.controller.onExecute(self.values)
def onScriptEnd(self):
self.message_area['state'] = DISABLED # prevents insertions to message area
self.controller.showbutton(5, ">>>", tip="Verify USFM", cmd=self._onNext)

def _set_button_status(self):
Expand Down
51 changes: 24 additions & 27 deletions usfm/g_selectProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,39 @@ def __init__(self, mainframe, mainapp):
self.frame = Select_Frame(mainframe, self)
self.frame.grid(row=1, column=0, sticky="nsew")

def name(self):
return stepname

def onNext(self):
self.mainapp.set_process(self.values['selection'])
self.mainapp.step_next()

class Select_Frame(ttk.Frame):
class Select_Frame(g_step.Step_Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
super().__init__(parent, controller)

self.process = StringVar()
subheadingFont = font.Font(size=10, slant='italic') # normal size is 9
suppressions_label = ttk.Label(self, text="What do you want to do today?", font=subheadingFont)
suppressions_label.grid(row=3, column=1, columnspan=2, sticky=W, pady=(4,2))

process1_rb = ttk.Radiobutton(self, text='Convert text to USFM', variable=self.process,
process1_rb = ttk.Radiobutton(self, text='Convert BTT-Writer text to USFM', variable=self.process,
command=self._onRbChange, value='Txt2USFM')
process1_rb.grid(row=4, column=1, sticky=W)
process1_Tip = Hovertip(process1_rb, hover_delay=500,
text="Convert a set of text files from BTT-Writer repos to USFM.")

process2_rb = ttk.Radiobutton(self, text='Verify and clean USFM', variable=self.process,
command=self._onRbChange, value='VerifyUSFM')
process2_rb = ttk.Radiobutton(self, text='Convert plain text to USFM', variable=self.process,
command=self._onRbChange, value='Plaintext2Usfm')
process2_rb.grid(row=5, column=1, sticky=W)
process2_Tip = Hovertip(process2_rb, hover_delay=500,
text="Verify and/or clean up existing USFM file.")
text="Convert plain text filed containing books of the Bible to usfm files.")

process3_rb = ttk.Radiobutton(self, text='Convert plain text to USFM', variable=self.process,
command=self._onRbChange, value='Plaintext2Usfm')
process3_rb = ttk.Radiobutton(self, text='Verify and clean USFM', variable=self.process,
command=self._onRbChange, value='VerifyUSFM')
process3_rb.grid(row=6, column=1, sticky=W)
process3_Tip = Hovertip(process3_rb, hover_delay=500,
text="Convert plain text filed containing books of the Bible to usfm files.")
text="Verify and/or clean up existing USFM files.")

process4_rb = ttk.Radiobutton(self, text='Convert USFM to USX', variable=self.process,
command=self._onRbChange, value='Usfm2Usx')
Expand All @@ -55,17 +57,6 @@ def __init__(self, parent, controller):
text="Produce a BTTW-compatible resource container from USFM.")

self.columnconfigure(1, minsize=505)
self.rowconfigure(88, minsize=150, weight=1) # let the message expand vertically

self.message_area = Text(self, height=10, width=60, wrap="word")
self.message_area['borderwidth'] = 2
self.message_area['relief'] = 'sunken'
self.message_area['background'] = 'grey97'
self.message_area.grid(row=88, column=1, sticky='nsew', pady=6)

ys = ttk.Scrollbar(self, orient = 'vertical', command = self.message_area.yview)
ys.grid(column = 5, row = 88, sticky = 'ns')
self.message_area['yscrollcommand'] = ys.set

# Called when the frame is first activated. Populate the initial values.
def show_values(self, values):
Expand All @@ -84,13 +75,10 @@ def _save_values(self):

# Handles the radio button click event.
def _onRbChange(self, *args):
self.values['selection'] = self.process.get()
self._explain()
self._set_button_status()

def _onNext(self, *args):
self._save_values()
self.controller.onNext()

# Presents a detailed message about the selected process.
def _explain(self, *args):
self.message_area['state'] = NORMAL # enables insertions to message area
Expand All @@ -108,8 +96,8 @@ def _explain(self, *args):
self.message_area.insert('end', "Find and address issues in USFM files.")
case 'Plaintext2Usfm':
self.message_area.insert('end',
"This process converts text files containing books of the Bible to usfm.\n\
The text files must meet these rigid conditions:\n\
"This process converts SCripture text files, *not* of BTT-Writer origin, to USFM. \
To be converted, the text files must meet these conditions:\n\
* Each file contains a single book of the Bible.\n\
* No extraneous text in file.\n\
* File names must be like XXX.txt (where XXX = book id).\n\
Expand All @@ -131,6 +119,15 @@ def _explain(self, *args):
self.message_area.see('1.0')
self.message_area['state'] = DISABLED # prevents editing of message area


# Required ABC methods
def _save_values(self):
pass
def _onExecute(self, *args):
pass
def onScriptEnd(self, nIssues):
pass

def _set_button_status(self):
# okay = self.process.get() in {'Txt2USFM', 'VerifyUSFM', 'Plaintext2Usfm', 'Usfm2Usx'}
self.controller.enablebutton(5, True)
Loading

0 comments on commit f027bb9

Please sign in to comment.