Skip to content

Commit

Permalink
[036_pli_checker], issue #36, Reviewed the modifications to solve the…
Browse files Browse the repository at this point in the history
… bug
  • Loading branch information
lelaus committed Nov 7, 2024
1 parent 4f12e94 commit fca809d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 44 deletions.
25 changes: 5 additions & 20 deletions tugui/gui_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<<Valid-Entry>>")
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("<<Valid-Entry>>")
# 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")
Expand Down
2 changes: 1 addition & 1 deletion tugui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 13 additions & 17 deletions tugui/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 4 additions & 6 deletions tugui/tu_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fca809d

Please sign in to comment.