You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

135 lines
4.0 KiB

import sys
import tkinter as tk
import tkinter.ttk as ttk
from pathlib import Path
from tkinter import filedialog
from .main import process_log_files
from .utils import find_log_files, open_with_default_app
if getattr(sys, "frozen", False):
# we are inside the standalone gui app
icon_path = Path(sys._MEIPASS) / "program_icon.ico"
elif __file__:
# this should be the normal cli thingy
icon_path = Path(__file__).parent.parent / "images" / "program_icon.ico"
else:
# there's something strange
icon_path = None
class StatusPanel(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent.master)
self._parent = parent
self.label = tk.Label(self, bd=1, relief=tk.SUNKEN, anchor=tk.S)
self.label.pack(fill=tk.X)
self.pack(fill=tk.X)
def set_text(self, text):
self.label.config(text=text)
self.label.update_idletasks()
def clear_text(self):
self.set_text("")
class FilePanel(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent.master)
self._parent = parent
self.btn_files = tk.Button(
self, text="Select Folder", command=self._parent.select_folder
)
self.btn_files.pack(pady=5, padx=5)
self.label = tk.Label(self, anchor=tk.CENTER)
self.label.pack(fill=tk.X, pady=5, padx=5)
ttk.Separator(self, orient=tk.HORIZONTAL).pack(side=tk.BOTTOM, fill=tk.X)
self.pack(side=tk.TOP, fill=tk.X)
def set_text(self, text):
self.label.config(text=text)
self.label.update_idletasks()
def clear_text(self):
self.set_text("")
class ActionPanel(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent.master)
self._parent = parent
self.btn_quit = tk.Button(self, text="Quit", command=self._parent.quit)
self.btn_quit.pack(side=tk.LEFT, pady=35, padx=35)
self.btn_go = tk.Button(self, text="GO!", command=self._parent.generate_report)
self.btn_go.pack(side=tk.RIGHT, pady=35, padx=35)
self.pack(fill=tk.X)
def disable(self):
self.btn_go.config(state="disabled")
def enable(self):
self.btn_go.config(state="active")
class Application(tk.Frame):
def __init__(self, master):
master.minsize(height=150, width=400)
tk.Frame.__init__(self, master)
self._master = master
self.log_files = None
self.pack(fill=tk.BOTH)
self.file_panel = FilePanel(self)
self.action_panel = ActionPanel(self)
self.status_panel = StatusPanel(self)
self.reset()
def reset(self):
self.status_panel.clear_text()
self.action_panel.disable()
def select_folder(self):
self.reset()
if self.log_files is not None:
initial_dir = self.log_files.folder.parent
else:
initial_dir = "~/Desktop"
opts = {"initialdir": initial_dir, "mustexist": True}
selection = tk.filedialog.askdirectory(**opts)
if selection:
self.log_files = find_log_files(selection)
self.set_active_state()
def set_active_state(self, event=None):
if self.log_files:
self.file_panel.set_text(self.log_files.folder)
self.action_panel.enable()
else:
self.file_panel.set_text("This is not a S3 Print Log Folder")
self.action_panel.disable()
def quit(self):
self._master.quit()
def generate_report(self):
self.status_panel.set_text(
"Generating report, PDF should be opened in a couple of seconds"
)
report_file = process_log_files(self.log_files)
open_with_default_app(report_file)
self.status_panel.set_text("Report Generated.")
def run():
root = tk.Tk()
root.title("S3 Print Log Report")
if icon_path is not None:
root.iconbitmap(str(icon_path))
app = Application(master=root)
app.mainloop()
try:
root.destroy()
except tk.TclError:
pass