Source code for viiapackage.results.result_functions.viia_get_latest_result_folder

### ===================================================================================================================
###   viia_get_latest_result_folder
### ===================================================================================================================
# Copyright ©VIIA 2024

### ===================================================================================================================
###   1. Import modules
### ===================================================================================================================

# General imports
from pathlib import Path
from typing import Optional, List


### ===================================================================================================================
###   2. Function viia_get_latest_result_folder
### ===================================================================================================================

[docs]def viia_get_latest_result_folder( project: 'ViiaProject', version: Optional[int] = 1, folder: Optional[Path] = Path.cwd(), files: Optional[List[str]] = None): """ This function selects the latest from the default analysis sub-folders. The path is based on the latest version and time in the name of the folder. The version number determines the required model step. For BSC, the version is always 1. Input: - project (obj): Project object containing collections and of fem objects and project variables. - version (int): The version number as indicated at start of the project. By VIIA convention version number 1 indicates the BSC and higher numbers the strengthening steps. Default value is 1, returning the latest analysis folder for the BSC evaluation. - folder (Path): Directory of the analysis sub-folder. - files (list): Contains file names of result files that are required to exist in the result folder. Output: - The latest result folder for the specified version number, containing the optional inputted files, is returned as path reference. """ # Create a dictionary with all the folders of the correct version, key is the timestamp folders_dict = viia_get_latest_folder(version, folder) if files: if isinstance(files, str): files = [files] # Select the maximum value of the timestamp, this is the latest folder folder_order = reversed(sorted(list(set([int(folder) for folder in folders_dict.keys()])))) files_found = False # Loop for subfolders for subfolder in folder_order: # Check for all files for file in files: if Path(f"{folder._str}\\{subfolder}-v{str(version).zfill(3)}//{file}").is_file(): files_found = True subfolder_temp = subfolder else: # All files must be found files_found = False break # If all files exist, return, else continue if files_found: return Path(f"{folder._str}\\{subfolder_temp}-v{str(version).zfill(3)}") # Exhausted if not files_found: project.write_log(f"WARNING: Not all the files in {files} can be found {folder}, please check.") return None if folders_dict: latest = max(folders_dict.keys()) return folders_dict[latest] else: return None
def viia_get_latest_folder(version, folder): # Create folder list list_folders = [] for item in folder.iterdir(): if item.is_dir(): if item.stem[:14].isdigit(): list_folders.append(item) else: for sub_item in item.iterdir(): if sub_item.is_dir(): if sub_item.stem[:14].isdigit(): list_folders.append(sub_item) # Create a dictionary with the required version number folders_dict = {} if version == 'highest': max_version = 0 for item in list_folders: version_nr = int(item.stem.split('-v')[-1]) if version_nr > max_version: max_version = version_nr version = max_version if type(version) == int: # Remove the version part for the keys to be the timestamp version_check = '-v' + str(version).zfill(3) for item in list_folders: if version_check in item.stem: folders_dict[int(item.stem.replace(version_check, ''))] = item return folders_dict else: return None ### =================================================================================================================== ### 3. End of script ### ===================================================================================================================