Some improvements to visibility and file input

This commit is contained in:
Sit Melai 2021-11-17 18:50:08 +01:00
parent e31f7b2163
commit 0149bb8ed0

34
main.py
View file

@ -1,4 +1,5 @@
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
from tk_tooltip import CreateToolTip
import re
@ -64,7 +65,7 @@ def get_input_filepath():
global input_filepath
filepath = filedialog.askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
# initialdir='/'.join(ent_input.get().split('/')[:-1])
initialdir='/'.join(ent_input.get().split('/')[:-1 if '.' in ent_input.get() else len(ent_input.get())])
)
if not filepath:
return
@ -87,24 +88,24 @@ def get_output_filepath():
def toggle_channel(index):
def tgl_chan():
if chan_toggle[index]:
btn_chan_toggle[index].config(relief="raised")
btn_chan_toggle[index].config(relief="raised", bg="#ffcccb")
chan_toggle[index] = False
if index == 11:
if channel_names[index] == "SYSTEM":
frm_btn_sys.grid_remove()
else:
btn_chan_toggle[index].config(relief="sunken")
btn_chan_toggle[index].config(relief="sunken", bg="#99e599")
chan_toggle[index] = True
if index == 11:
if channel_names[index] == "SYSTEM":
frm_btn_sys.grid()
return tgl_chan
def toggle_system(index):
def tgl_sys():
if sys_toggle[index]:
btn_sys_toggle[index].config(relief="raised")
btn_sys_toggle[index].config(relief="raised", bg="#ffcccb")
sys_toggle[index] = False
else:
btn_sys_toggle[index].config(relief="sunken")
btn_sys_toggle[index].config(relief="sunken", bg="#99e599")
sys_toggle[index] = True
return tgl_sys
@ -119,6 +120,8 @@ ent_input = None
ent_output = None
ent_outfile = None
frm_btn_sys = None
btn_process = None
window = None
def create_ui():
global btn_chan_toggle
@ -129,6 +132,9 @@ def create_ui():
global ent_output
global ent_outfile
global frm_btn_sys
global window
global btn_process
window = tk.Tk()
window.title("Ryzom Log Cleaner")
@ -169,7 +175,7 @@ def create_ui():
frm_btn_channel.columnconfigure([0,1,2,3], weight=1)
frm_btn_channel.rowconfigure([0,1,2], weight=1)
for i,name in enumerate(channel_names):
btn_chan_toggle.append(tk.Button(frm_btn_channel, text=name, command=toggle_channel(i)))
btn_chan_toggle.append(tk.Button(frm_btn_channel, text=name, command=toggle_channel(i), bg="#ffcccb"))
btn_chan_toggle[i].grid(row=int(i/4), column=i%4, sticky='ew')
btn_sys_toggle = []
@ -179,7 +185,7 @@ def create_ui():
frm_btn_sys.columnconfigure([0,1,2,3,4,5,6], weight=1)
frm_btn_sys.rowconfigure([0,1,2,3,4], weight=1)
for i,(name,tooltip) in enumerate(system_info_categories):
btn_sys_toggle.append(tk.Button(frm_btn_sys, text=name, command=toggle_system(i)))
btn_sys_toggle.append(tk.Button(frm_btn_sys, text=name, command=toggle_system(i), bg="#ffcccb"))
btn_sys_toggle[i].grid(row=int(i/7), column=i%7, sticky='ew')
ttp_sys = CreateToolTip(btn_sys_toggle[i], tooltip)
@ -215,8 +221,12 @@ def process_file():
if sys_toggle[i+1]:
chan_pattern += '|\(SYSTEM/' + name + '\)'
chan_regex = re.compile(chan_pattern)
with open(input_filepath, 'rb') as in_file, open(output_filepath + '/' + ent_outfile.get(), 'w') as out_file:
with open(ent_input.get(), 'r', errors='surrogateescape') as in_file, open(ent_output.get() + '/' + ent_outfile.get(), 'w', errors='surrogateescape') as out_file:
orig_lines = 0
filtered_lines = 0
btn_process["text"] = "Started Processing..."
for line in in_file:
orig_lines += 1
if chan_regex.search(line) == None:
continue
if not keep_color:
@ -234,7 +244,9 @@ def process_file():
# if char_name[0] == '[' and char_name[2] == ']':
# char_name = char_name[3:]
# channel_name = line[21:line.find(')')]
out_file.write(line + '\n')
out_file.write(line.lstrip())
filtered_lines += 1
btn_process["text"]="Processing done! (" + str(filtered_lines) + " lines kept out of " + str(orig_lines) + " original lines)"
if __name__ == '__main__':