From 7935a8fe097a53881081cf753b91278f0ebe004a Mon Sep 17 00:00:00 2001 From: edouardbruelhart Date: Fri, 12 Apr 2024 09:38:14 +0200 Subject: [PATCH] Some cleaning to pass tests --- ms_sample_list_creator/csv_batch.py | 51 +++++++++++++++---- ms_sample_list_creator/home_page.py | 10 ++-- ms_sample_list_creator/log.txt | 0 .../ms_sample_list_creator.py | 51 +++++++++++++++---- ms_sample_list_creator/new_batch.py | 23 ++++++--- ms_sample_list_creator/pieces.py | 25 --------- ms_sample_list_creator/prefix_window.py | 47 ----------------- 7 files changed, 106 insertions(+), 101 deletions(-) delete mode 100644 ms_sample_list_creator/log.txt delete mode 100644 ms_sample_list_creator/pieces.py delete mode 100644 ms_sample_list_creator/prefix_window.py diff --git a/ms_sample_list_creator/csv_batch.py b/ms_sample_list_creator/csv_batch.py index ca47db2..0303120 100644 --- a/ms_sample_list_creator/csv_batch.py +++ b/ms_sample_list_creator/csv_batch.py @@ -3,14 +3,15 @@ from datetime import datetime from tkinter import filedialog + class csvBatch(tk.Frame): - def __init__(self, csv_batch_window, root): + def __init__(self, csv_batch_window: tk.Toplevel, root: tk.Tk): """ Initializes an instance of the class. Args: - root(tk.Tk): The parent widget or window where this frame will be placed. - csv_path(str): CSV path and name. + csv_batch_window(tk.Toplevel): The parent widget where this frame will be placed. + root(tk.Tk): The root window to perform actions on it. Returns: None @@ -43,27 +44,59 @@ def __init__(self, csv_batch_window, root): self.timestamp = datetime.now().strftime("%Y%m%d%H%M") self.csv_path = f"{self.output_folder}/{datetime.now().strftime('%Y%m%d')}_{self.operator}_dbgi_{self.file}.csv" - warning_label = tk.Label(self.csv_batch_window, text="Warning, this mode is exclusively made to submit sample lists that have already been made using this tool.") + warning_label = tk.Label( + self.csv_batch_window, + text="Warning, this mode is exclusively made to submit sample lists that have already been made using this tool.", + ) warning_label.pack() label = tk.Label(self.csv_batch_window, text="Search for your CSV:", pady=10) label.pack() - import_button = tk.Button(self.csv_batch_window, text="Import your CSV", width=17, command=self.import_csv, pady=10) + import_button = tk.Button( + self.csv_batch_window, text="Import your CSV", width=17, command=self.import_csv, pady=10 + ) import_button.pack() - button_submit = tk.Button(self.csv_batch_window, text="Submit", width=17, command=self.show_values, pady=10) + button_submit = tk.Button(self.csv_batch_window, text="Submit", width=17, command=self.submit_result, pady=10) button_submit.pack() button_back = tk.Button(self.csv_batch_window, text="Go back to home", width=17, command=self.on_exit, pady=10) button_back.pack() - def on_exit(self): + def on_exit(self) -> None: + """ + Defines behaviour when user quits this window (by x button or specified button). + + Args: + None + + Returns: + None + """ self.csv_batch_window.destroy() self.root.deiconify() - def import_csv(self): + def import_csv(self) -> None: + """ + Asks the path to input CSV. + + Args: + None + + Returns: + None + """ os.environ["FILE_PATH"] = filedialog.askopenfilename(filetypes=[("CSV Files", "*.csv")]) - def show_values(self): + def submit_result(self) -> None: + """ + performs modifications on the CSV, submit them to directus and writes the output csv. + + Args: + None + + Returns: + None + """ print("correctly written") diff --git a/ms_sample_list_creator/home_page.py b/ms_sample_list_creator/home_page.py index e22108b..cff7893 100644 --- a/ms_sample_list_creator/home_page.py +++ b/ms_sample_list_creator/home_page.py @@ -1,12 +1,13 @@ import os import tkinter as tk from tkinter import filedialog +from typing import Any import requests class HomeWindow(tk.Frame): - def __init__(self, parent, *args, **kwargs): + def __init__(self, parent: tk.Tk, *args: Any, **kwargs: Any): """ Initializes an instance of the class. @@ -250,12 +251,12 @@ def output_folder(self) -> None: folder = parts[-1] self.output_path_button.config(text=folder) - def show_values(self, clicked_button) -> None: + def show_values(self, clicked_button: str) -> None: """ Stores all the parameters to the environment when user confirms his choice. Args: - None + clicked_button(str): A string ("new" or "csv"), that defines which window will be launched after home page. Returns: None @@ -370,7 +371,7 @@ def testConnection(self) -> None: # If user didn't enter all necessary values, shows this message self.label.config(text="Please provide all asked values", foreground="red") - def manage_choice(self) -> None: + def manage_choice(self) -> str: """ Returns to main script which option did the user choose. @@ -389,3 +390,4 @@ def manage_choice(self) -> None: else: # If user didn't enter all necessary values, shows this message self.label.config(text="Unknow error, please try again with other parameters", foreground="red") + return "error" diff --git a/ms_sample_list_creator/log.txt b/ms_sample_list_creator/log.txt deleted file mode 100644 index e69de29..0000000 diff --git a/ms_sample_list_creator/ms_sample_list_creator.py b/ms_sample_list_creator/ms_sample_list_creator.py index 9790017..24c4b76 100644 --- a/ms_sample_list_creator/ms_sample_list_creator.py +++ b/ms_sample_list_creator/ms_sample_list_creator.py @@ -5,22 +5,53 @@ # other non-built-in packages (here requests) inside wine. Then run: wine pyinstaller --onefile main.py import tkinter as tk -from home_page import HomeWindow -from new_batch import newBatch -from csv_batch import csvBatch -def submit_results(clicked_button:str): +from csv_batch import csvBatch # type: ignore[import-not-found] +from home_page import HomeWindow # type: ignore[import-not-found] +from new_batch import newBatch # type: ignore[import-not-found] + + +def submit_results(clicked_button: str) -> None: + """ + Permits to detect the button the user clicked. + + Args: + clicked_button (str): string parameter to define which button is clicked. + + Returns: + None + """ home_page.show_values(clicked_button) handle_user_choice() + # Function to handle the user choice -def handle_user_choice(): +def handle_user_choice() -> None: + """ + Collect user choice and transmits it to de function that shows the correct window. + + Args: + None + + Returns: + None + """ # Call the manage_choice method to get the user choice user_choice = home_page.manage_choice() # Show the corresponding window based on the user's choice show_selected_window(user_choice) -def show_selected_window(choice): + +def show_selected_window(choice: str) -> None: + """ + Accepts user choice and launches the correct window. + + Args: + choice (str): Retrieves the user choice. + + Returns: + None + """ if choice == "new": # Create a new Toplevel window for the new batch new_batch_window = tk.Toplevel(root) @@ -38,6 +69,7 @@ def show_selected_window(choice): # Handle the case of an unknown choice print("Unknown error, please try again with other parameters.") + # Create an instance of the main window root = tk.Tk() root.title("Home") @@ -52,14 +84,13 @@ def show_selected_window(choice): frame_submit = tk.Frame(root) frame_submit.pack(pady=(50, 0)) -button_new_batch = tk.Button( -frame_submit, text="New sample list", width=20, command=lambda: submit_results("new")) +button_new_batch = tk.Button(frame_submit, text="New sample list", width=20, command=lambda: submit_results("new")) button_new_batch.pack(side="left") button_submit_csv = tk.Button( -frame_submit, text="Sample list from CSV", width=20, command=lambda: submit_results("csv")) + frame_submit, text="Sample list from CSV", width=20, command=lambda: submit_results("csv") +) button_submit_csv.pack(side="right") # Start the tkinter event loop root.mainloop() - diff --git a/ms_sample_list_creator/new_batch.py b/ms_sample_list_creator/new_batch.py index 0707e84..595cf9d 100644 --- a/ms_sample_list_creator/new_batch.py +++ b/ms_sample_list_creator/new_batch.py @@ -9,13 +9,13 @@ class newBatch: - def __init__(self, new_batch_window, root): + def __init__(self, new_batch_window: tk.Toplevel, root: tk.Tk): """ Initializes an instance of the class. Args: - root(tk.Tk): The parent widget or window where this frame will be placed. - csv_path(str): CSV path and name. + new_batch_window(tk.Toplevel): The parent widget where this frame will be placed. + root(tk.Tk): The root window to perform actions on it. Returns: None @@ -85,7 +85,9 @@ def __init__(self, new_batch_window, root): self.label.grid(row=2, column=0, columnspan=2, pady=10) # Submit button - submit_button = ttk.Button(self.new_batch_window, text="Generate sample list", width=20, command=self.submit_table) + submit_button = ttk.Button( + self.new_batch_window, text="Generate sample list", width=20, command=self.submit_table + ) # Back button button_back = tk.Button(self.new_batch_window, text="Back to Home", width=20, command=self.on_exit) @@ -100,7 +102,16 @@ def __init__(self, new_batch_window, root): self.new_batch_window.mainloop() self.root.withdraw() - def on_exit(self): + def on_exit(self) -> None: + """ + Defines behaviour when user quits this window (by x button or specified button). + + Args: + None + + Returns: + None + """ self.new_batch_window.destroy() self.root.deiconify() @@ -380,4 +391,4 @@ def store_prefix(self) -> None: os.environ["PREFIX"] = self.prefix.get() # Close the AskBoxPrefixWindow - self.master.destroy() \ No newline at end of file + self.master.destroy() diff --git a/ms_sample_list_creator/pieces.py b/ms_sample_list_creator/pieces.py deleted file mode 100644 index 7f29b75..0000000 --- a/ms_sample_list_creator/pieces.py +++ /dev/null @@ -1,25 +0,0 @@ -from tkinter import * - -class MainWindow: - def __init__(self, master): - mainframe = Frame(master, width = 300, height = 200) - button = Button(mainframe, text="Open Window", - command=self.openWindow) - button.place(x = 100, y = 80) - mainframe.pack() - - def openWindow(self): - win = ExtraWindow() - -class ExtraWindow: - def __init__(self): - top = Toplevel() - - subframe = Frame(top, width = 200, height = 150) - button = Button(top, text="Destroy Window", command=top.destroy) - button.place(x = 50, y = 50) - subframe.pack() - -root = Tk() -window = MainWindow(root) -root.mainloop() \ No newline at end of file diff --git a/ms_sample_list_creator/prefix_window.py b/ms_sample_list_creator/prefix_window.py deleted file mode 100644 index 30b2b56..0000000 --- a/ms_sample_list_creator/prefix_window.py +++ /dev/null @@ -1,47 +0,0 @@ -import tkinter as tk -import os - -class AskBoxPrefixWindow(tk.Frame): - def __init__(self, root: tk.Toplevel): - """ - Initializes an instance of the class. - - Args: - root(tk.Toplevel): The parent widget or window where this frame will be placed. - csv_path(str): CSV path and name. - - Returns: - None - """ - tk.Frame.__init__(self, root) - - self.prefix = tk.StringVar() - - # Adjust the window size - root.geometry("300x150") - - # Label + textbox to enter prefix - label_prefix = tk.Label(self, text="Box's prefix:") - label_prefix.pack() - - entry_prefix = tk.Entry(self, textvariable=self.prefix) - entry_prefix.pack() - - # Submit button - button_submit = tk.Button(self, text="Submit", command=self.store_prefix) - button_submit.pack() - - def store_prefix(self) -> None: - """ - Puts the asked prefix to the environment. - - Args: - None - - Returns: - None - """ - os.environ["PREFIX"] = self.prefix.get() - - # Close the AskBoxPrefixWindow - self.master.destroy() \ No newline at end of file