diff --git a/tugui/gui_widgets.py b/tugui/gui_widgets.py index 889527e..59d9584 100644 --- a/tugui/gui_widgets.py +++ b/tugui/gui_widgets.py @@ -38,45 +38,30 @@ def __init__(self, frame: tk.Frame, width: int, col: int, row: int, def validate(self, entry_txt: str = "") -> bool: """ - Method that checks if the entry is valid. The "end" parameter indicates the - extension to check against. + Method that checks if the entry is valid. """ - # Do not check anything if the entry is empty; return immediately + # Return if the entry is empty if not entry_txt: return True # Check the entry value according to the validation function try: self.validation_func(entry_txt, self.entry_extension) + # FIXME: to add the following message into the log file print("The entry is valid!") self.entry.configure(foreground="#343638") # Generate a virtual event stating that the entry is valid self.entry.event_generate("<>") return True except Exception as e: - # Get the exception error message - error_message = str(e) # Handle the invalid case - self.on_invalid(error_message) + self.on_invalid(str(e)) return False - # if re.match(r"^.*\." + self.entry_extension + "$", entry_txt) is not None: - # # The entry is valid if a match is found - # print("The entry is valid!") - # self.entry.configure(foreground="#343638") - # # Generate a virtual event stating that the entry is valid - # self.entry.event_generate("<>") - # return True - # else: - # # If no match is found, handle the invalid case only if the entry value is not empty - # if entry_txt != "": - # self.on_invalid() - # return False - def on_invalid(self, error_message: str) -> None: """ Show the error message if the data is not valid. """ - # error_message = "The entry is not valid: please provide a path to a file with the valid \"" + self.entry_extension + "\" extension!" + # FIXME: to add the following message to the log file print(error_message) # Highlight the entry color in red self.entry.configure(foreground="red") diff --git a/tugui/main.py b/tugui/main.py index 902ad97..a2e9cb8 100644 --- a/tugui/main.py +++ b/tugui/main.py @@ -788,7 +788,7 @@ def __select_file_and_fill_entry(self, entry: ttk.Entry, fileToSearch: str, form # and retrieve the path of the selected file. filename = self.__select_file(fileToSearch, format) - # Delete any already present path in the given entry + # Delete the content of the entry, if any entry.delete(0, tk.END) # Insert the selected file path in the given entry entry.insert(0, filename) diff --git a/tugui/support.py b/tugui/support.py index 2b60114..2edc0d6 100644 --- a/tugui/support.py +++ b/tugui/support.py @@ -77,43 +77,39 @@ def description(self) -> str: return self.value[1] -def check_file_existence(file_path: str, file_extension: str) -> None: +def check_file_existence(file_path: str) -> None: """ - Function that can be accessed globally for checking if the given - file path exists and is a file. If not, the function raises an - exception. + Function for checking if the given file path exists and is a file. + If not, the function raises an exception. """ if not os.path.isfile(file_path): # If the file does not exists, throw an exception - raise Exception(f"Error: the .{file_extension} file does not exist at the specified path.") + raise Exception(f"Error: file {file_path} does not exist!") def check_file_extension_and_existence(file_path: str, file_extension: str) -> None: """ - Function that can be accessed globally for checking if the given - file has the correct instance, its path exists and corresponds to - a file. If so, True is returned; False otherwise. + Function for checking if the given file has the correct extension and + exists. If so, True is returned; False otherwise. Parameters ---------- - file_path : str - The path to the file to check + file_path : str + The path to the file to check. file_extension : str - The extension the file must have and is checked for + The extension the file must have and is checked for. Raises ------ An Exception if the file extension does not match with the required - one or the file does not exists at the specified path + one or the file does not exist. """ + # Check the extension extension = os.path.splitext(file_path) - print("Extension", extension) if not (extension[1] == '.' + file_extension): # The file specified by the given path has not the correct extension raise Exception(f"The indicated file with extension '{extension[1]}' is " "not valid. Please provide one with the " f"'{file_extension}' extension.") - if not os.path.isfile(file_path): - # The file does not exists - raise Exception(f"Error: the file specified by the '{file_path}' path " - "does not exist.") + # Check the existence + check_file_existence(file_path) diff --git a/tugui/tu_interface.py b/tugui/tu_interface.py index 0db099e..4553ff3 100644 --- a/tugui/tu_interface.py +++ b/tugui/tu_interface.py @@ -199,11 +199,11 @@ def save_loaded_inp(self) -> str: else: plireader = PliReader.init_PliReader(os.path.join(self.inp_dir, diagr.pli_name)) # Check if any of the DAT files is missing - check_file_existence(os.path.join(self.inp_dir, plireader.mac_path), 'mac') - check_file_existence(os.path.join(self.inp_dir, plireader.mic_path), 'mic') + check_file_existence(os.path.join(self.inp_dir, plireader.mac_path)) + check_file_existence(os.path.join(self.inp_dir, plireader.mic_path)) # Check the .sta file existence only if required, i.e if the 'ISTATI' field is '1' if plireader.opt_dict['ISTATI'] == 1: - check_file_existence(os.path.join(self.inp_dir, plireader.sta_path), 'sta') + check_file_existence(os.path.join(self.inp_dir, plireader.sta_path)) # Declare a string holding the .inp filename (with default to 'TuPlot') filename = "TuPlot.inp" @@ -451,8 +451,6 @@ def init_PliReader(pli_path: str) -> Self: This method returns the built instance of the 'PliReader' class. """ # Check the .pli file existence - # FIXME to delete as already performed by the entry widget - # check_file_existence(pli_path, 'pli') # Get the path to the .pli file directory pli_dir = os.path.dirname(pli_path) # Instantiate the 'PliReader' class @@ -521,7 +519,7 @@ def __init__(self, da_path: str, extension: str) -> None: direct-access file to read and checks its actual existence. """ # Check the direct-access file existence - check_file_existence(da_path, extension) + check_file_existence(da_path) # Store the direct-access file path self.da_path: str = da_path # Initialize the time values read from the direct-access file