Skip to content

Commit

Permalink
Allow overwriting a model that is open in QGIS (#1881)
Browse files Browse the repository at this point in the history
Redoing #569 which got lost in the add API changes.
Goes well with https://github.com/evetion/Reloader/
  • Loading branch information
visr authored Oct 8, 2024
1 parent 5bb6cdc commit 32ebc6f
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion python/ribasim/ribasim/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import logging
import shutil
from collections.abc import Generator
from os import PathLike
from pathlib import Path
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 32ebc6f

Please sign in to comment.