Skip to content

Commit

Permalink
change of structure (#47)
Browse files Browse the repository at this point in the history
* Changed the structure

* added generation for level 0

* fix test failing because precice config was moved

---------

Co-authored-by: Toddelismyname <[email protected]>
  • Loading branch information
VanLaareN and Toddelismyname authored Jan 9, 2025
1 parent 4e158e8 commit 02b73b6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 26 deletions.
49 changes: 38 additions & 11 deletions FileGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, input_file: Path, output_path: Path) -> None:
self.logger = Logger()
self.structure = StructureHandler(output_path)

def generate_precice_config(self) -> None:
def _generate_precice_config(self) -> None:
"""Generates the precice-config.xml file based on the topology.yaml file."""

# Try to open the yaml file and get the configuration
Expand Down Expand Up @@ -89,27 +89,57 @@ def _generate_static_files(self, target: Path, name: str) -> None:
self.logger.error(f"An unexpected error occurred: {generalExcpetion}")
pass

def generate_README(self) -> None:
def _generate_README(self) -> None:
"""Generates the README.md file"""
self._generate_static_files(target=self.structure.README,
name="README.md")

def generate_run(self) -> None:
def _generate_run(self) -> None:
"""Generates the run.sh file"""
self._generate_static_files(target=self.structure.run,
name="run.sh")

def generate_clean(self) -> None:
def _generate_clean(self) -> None:
"""Generates the clean.sh file."""
self._generate_static_files(target=self.structure.clean,
name="clean.sh")

def generate_adapter_config(self, target_participant: str) -> None:
def _generate_adapter_config(self, target_participant: str, adapter_config: Path) -> None:
"""Generates the adapter-config.json file."""
adapter_config_generator = AdapterConfigGenerator(adapter_config_path=self.structure.adapter_config,
adapter_config_generator = AdapterConfigGenerator(adapter_config_path=adapter_config,
precice_config_path=self.structure.precice_config,
target_participant=target_participant)
adapter_config_generator.write_to_file()

def generate_level_0(self) -> None:
"""Fills out the files of level 0 (everything in the root folder)."""
self._generate_clean()
self._generate_README()
self._generate_precice_config()

def _extract_participants(self) -> list[str]:
"""Extracts the participants from the topology.yaml file."""
try:
with open(self.input_file, "r") as config_file:
config = yaml.load(config_file.read(), Loader=yaml.SafeLoader)
self.logger.info(f"Input YAML file: {self.input_file}")
except FileNotFoundError:
self.logger.error(f"Input YAML file {self.input_file} not found.")
return
except Exception as e:
self.logger.error(f"Error reading input YAML file: {str(e)}")
return

return list(config["participants"].keys())

def generate_level_1(self) -> None:
"""Generates the files of level 1 (everything in the generated sub-folders)."""

participants = self._extract_participants()
for participant in participants:
target_participant = self.structure.create_level_1_structure(participant)
adapter_config = target_participant[1]
self._generate_adapter_config(target_participant=participant, adapter_config=adapter_config)

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Takes topology.yaml files as input and writes out needed files to start the precice.")
Expand All @@ -131,8 +161,5 @@ def generate_adapter_config(self, target_participant: str) -> None:
args = parser.parse_args()

fileGenerator = FileGenerator(args.input_file, args.output_path)
fileGenerator.generate_precice_config()
fileGenerator.generate_README()
fileGenerator.generate_clean()
fileGenerator.generate_run()
fileGenerator.generate_adapter_config(target_participant="Calculix")
fileGenerator.generate_level_0()
fileGenerator.generate_level_1()
43 changes: 29 additions & 14 deletions generation_utils/StructureHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,31 @@ def __init__(self, output_path: Path, clean_generated: bool = True) -> None:
# Objects
self.root = output_path
self.generated_root = self.root / "_generated"
self.config_dir = self.generated_root / "config"
self.logger = Logger()

# Methods in pre-processing
if clean_generated:
self._cleaner()
# Create level 0 structure (everything in the root folder)
self._create_folder_sturcture()
self._create_files()
self._create_level_0_structure()

def _create_folder_sturcture(self) -> None:
"""Creates the structure needed for generated files"""
try:
self.generated_root.mkdir(parents=True, exist_ok=True)
self.logger.success(f"Created folder: {self.generated_root}")

self.config_dir.mkdir(parents=True, exist_ok=True)
self.logger.success(f"Created folder: {self.config_dir}")
except Exception as create_folder_structure_excpetion:
self.logger.error(f"Failed to create folder structure. Error: {create_folder_structure_excpetion}")

def _create_files(self) -> None:
"""Creates the necessary files."""
def _create_level_0_structure(self) -> None:
"""Creates the necessary files of level 0 (everything in the root folder)."""

# Files that need to be created
files = [
self.generated_root / "clean.sh",
self.generated_root / "run.sh",
self.generated_root / "README.md",
self.config_dir / "precice-config.xml",
self.config_dir / "adapter-config.json"
self.generated_root / "precice-config.xml",
]

self.clean, self.run, self.README, self.precice_config, self.adapter_config = files
self.clean, self.README, self.precice_config = files

for file in files:
try:
Expand All @@ -51,6 +43,29 @@ def _create_files(self) -> None:
except Exception as create_files_exception:
self.logger.error(f"Failed to create file {file}. Error: {create_files_exception}")

def create_level_1_structure(self, participant: str) -> list[Path]:
""" Creates the necessary files of level 1 (everything in the generated sub-folders).
:param participant: The participant for which the files should be created.
:return: participant_folder, adapter_config, run"""
try:
# Create the participant folder
participant_folder = self.generated_root / participant
participant_folder.mkdir(parents=True, exist_ok=True)
self.logger.success(f"Created folder: {participant_folder}")

# Create the adapter-config.json file
adapter_config = participant_folder / "adapter-config.json"
adapter_config.touch(exist_ok=True)
self.logger.success(f"Created file: {adapter_config}")

# Create the run.sh file
run = participant_folder / "run.sh"
run.touch(exist_ok=True)
self.logger.success(f"Created file: {run}")

return [participant_folder, adapter_config, run]
except Exception as create_participant_folder_exception:
self.logger.error(f"Failed to create folder/file for participant: {participant_folder}. Error: {create_participant_folder_exception}")

def _cleaner(self) -> None:
"""
Expand Down
1 change: 0 additions & 1 deletion tests/test_porting_to_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def generated_config():
os.path.dirname(__file__),
'..',
'_generated',
'config',
'precice-config.xml'
)

Expand Down

0 comments on commit 02b73b6

Please sign in to comment.