From 11121a7eb8001e660cf3cd0c21fb551465cd944b Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Tue, 8 Oct 2024 16:10:38 +0200 Subject: [PATCH] Allow overwriting a model that is open in QGIS Redoing #569 which got lost in the add API changes. Goes well with https://github.com/evetion/Reloader/ --- python/ribasim/ribasim/model.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/python/ribasim/ribasim/model.py b/python/ribasim/ribasim/model.py index d204e7cbc..d5ab0a785 100644 --- a/python/ribasim/ribasim/model.py +++ b/python/ribasim/ribasim/model.py @@ -1,5 +1,6 @@ import datetime import logging +import shutil from collections.abc import Generator from os import PathLike from pathlib import Path @@ -190,7 +191,12 @@ def _write_toml(self, fn: Path) -> Path: return fn def _save(self, directory: DirectoryPath, input_dir: DirectoryPath): - db_path = directory / input_dir / "database.gpkg" + # We write all tables to a temporary GeoPackage with a dot prefix, + # and at the end move this over the target file. + # This does not throw a PermissionError if the file is open in QGIS. + db_path = directory / input_dir / ".database.gpkg" + + # avoid adding tables to existing model db_path.parent.mkdir(parents=True, exist_ok=True) db_path.unlink(missing_ok=True) context_file_writing.get()["database"] = db_path @@ -207,6 +213,8 @@ def _save(self, directory: DirectoryPath, input_dir: DirectoryPath): for sub in self._nodes(): sub._save(directory, input_dir) + shutil.move(db_path, db_path.with_name("database.gpkg")) + def set_crs(self, crs: str) -> None: """Set the coordinate reference system of the data in the model.