Source code for viiapackage.general.file_handling.restructure_folder

### ===================================================================================================================
###   Change the structure of A4/A12/A15 folders to match the current structure
### ===================================================================================================================
# Copyright ©VIIA 2024

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

# General imports
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING

# References for functions and classes in the viiaPackage
if TYPE_CHECKING:
    from viiapackage.viiaStatus import ViiaProject
import os
import shutil


### ===================================================================================================================
###    1. Function to restructure folder of A4,A12,A15
### ===================================================================================================================

[docs]def viia_restructure_folder(project: ViiaProject, analysis_nr: str): """ This function restructures an A4, A12 or A15 folder from the old structure to the current structure. The current structure involves a folder whose name contains the timestamp. This timestamp folder contains folder(s) corresponding to signal(s). Input: - project (obj): VIIA project object containing collections of fem objects and project variables. - analysis_nr (int): The analysis number as a string. The acceptable analysis_nr are 'A4', 'A12, 'A15'. Output: - The function does not return anything. A new folder is created with the new folder structure. The old folder remains with ' old' added in front of its name, however, its contents are moved to the new folder. """ path = None if analysis_nr == 'A4': path = project.workfolder_location / 'A4 - LTH fixed base' elif analysis_nr == 'A12': path = project.workfolder_location / 'A12 - NLTH flex base' elif analysis_nr == 'A15': path = project.workfolder_location / 'A15 - NLTH flex base strengthening' else: raise ValueError(f"ERROR: The analysis_nr should be one of 'A4', 'A12', 'A15'. You specified {analysis_nr}.") if not path or not path.exists(): project.write_log( f"WARNING: {analysis_nr} analysis folder cannot be found. Folder can not be restructured.") return # Rename the existing path folder existing_folder_name = path.name existing_folder_new_name = existing_folder_name + ' old' os.rename(path, Path(path.parent / existing_folder_new_name)) existing_folder_path = Path(path.parent / existing_folder_new_name) signals = {} for x in existing_folder_path.iterdir(): if 'Signaal ' in x.name and x.is_dir(): new_folder_name = x.name.replace('Signaal ', 'S') signals[new_folder_name] = [] for y in x.iterdir(): if y.is_dir() and len(y.name) == 19 and '-v' in y.name: signals[new_folder_name].append(y.name) new_folder = Path(x.parent / new_folder_name) os.rename(x, new_folder) elif 'S' in x.name and x.is_dir(): signals[x.name] = [] for y in x.iterdir(): if y.is_dir() and len(y.name) == 19 and '-v' in y.name: signals[x.name].append(y.name) for _key, _values in signals.items(): for _value in _values: source_path = Path(existing_folder_path / _key / _value) destination_path = Path(path / _value / _key) if not destination_path.exists(): destination_path.mkdir(parents=True, exist_ok=True) for _file in source_path.iterdir(): target = Path(destination_path / _file.name) shutil.move(_file, target)