Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/plaid/containers/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import glob
import logging
import os
import shutil
from typing import Optional, Union

import CGNS.MAP as CGM
Expand Down Expand Up @@ -1792,19 +1793,23 @@ def get_feature_from_identifier(
return self.get_nodes(**feature_identifier_)

# -------------------------------------------------------------------------#
def save(self, dir_path: str) -> None:
def save(self, dir_path: str, overwrite: Optional[bool] = False) -> None:
"""Save the Sample in directory `dir_path`.

Args:
dir_path (str): relative or absolute directory path.
overwrite (str, optional): target directory overwritten if True.
"""
if os.path.isdir(dir_path):
if len(glob.glob(os.path.join(dir_path, "*"))):
if os.path.exists(dir_path) and os.path.isdir(dir_path):
if overwrite:
shutil.rmtree(dir_path)
logger.warning(f"Existing {dir_path} directory has been reset.")
elif len(glob.glob(os.path.join(dir_path, "*"))):
raise ValueError(
f"directory {dir_path} already exists and is not empty"
f"directory {dir_path} already exists and is not empty. Set `overwrite` to True if needed."
)
else:
os.makedirs(dir_path)

os.makedirs(dir_path, exist_ok=True)

mesh_dir = os.path.join(dir_path, "meshes")

Expand Down
1 change: 1 addition & 0 deletions tests/containers/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ def test_save(self, sample_with_tree_and_scalar_and_time_series, tmp_path):
assert save_dir.is_dir()
with pytest.raises(ValueError):
sample_with_tree_and_scalar_and_time_series.save(save_dir)
sample_with_tree_and_scalar_and_time_series.save(save_dir, overwrite=True)

def test_load_from_saved_file(
self, sample_with_tree_and_scalar_and_time_series, tmp_path
Expand Down
Loading