diff --git a/src/scgenerator/spectra.py b/src/scgenerator/spectra.py index edc89ec..734cf5b 100644 --- a/src/scgenerator/spectra.py +++ b/src/scgenerator/spectra.py @@ -191,6 +191,7 @@ def propagation( file_or_params: os.PathLike | Parameters, params: Parameters | None = None, bundle_data: bool = False, + overwrite: bool = False, ) -> Propagation: file = None if isinstance(file_or_params, Parameters): @@ -198,7 +199,7 @@ def propagation( else: file = Path(file_or_params) - if file is not None and file.exists(): + if file is not None and file.exists() and params is None: io = ZipFileIOHandler(file) return _open_existing_propagation(io) @@ -206,6 +207,11 @@ def propagation( raise ValueError("Parameters must be specified to create new simulation") if file is not None: + if file.exists() and params is not None: + if overwrite: + Path(file).unlink() + else: + raise FileExistsError(f"{file} already exists. use overwrite=True to overwrite") io = ZipFileIOHandler(file) else: io = MemoryIOHandler() diff --git a/tests/test_io_handlers.py b/tests/test_io_handlers.py index e1a0457..deec0d9 100644 --- a/tests/test_io_handlers.py +++ b/tests/test_io_handlers.py @@ -101,6 +101,21 @@ def test_clear(tmp_path: Path): assert not zpath.exists() +def test_overwrite(tmp_path: Path): + params = Parameters(**PARAMS) + zpath = tmp_path / "file.zip" + _ = propagation(zpath, params) + orig_file = zpath.read_bytes() + + with pytest.raises(FileExistsError): + _ = propagation(zpath, params) + + _ = propagation(zpath, params, overwrite=True) + + assert zpath.read_bytes() != orig_file + assert len(zpath.read_bytes()) == len(orig_file) + + def test_zip_bundle(tmp_path: Path): params = Parameters(**PARAMS)