From 1093db986050fa458dff7635ce00e7865762a8ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Thu, 19 Dec 2024 08:35:26 +0100 Subject: [PATCH] Reload the store on file modification --- tilecloud_chain/multitilestore.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/tilecloud_chain/multitilestore.py b/tilecloud_chain/multitilestore.py index e237d6ffd..31b3fa2fa 100644 --- a/tilecloud_chain/multitilestore.py +++ b/tilecloud_chain/multitilestore.py @@ -2,13 +2,23 @@ import logging from collections.abc import Callable, Iterable, Iterator +from dataclasses import dataclass from itertools import chain, groupby, starmap +from pathlib import Path from tilecloud import Tile, TileStore logger = logging.getLogger(__name__) +@dataclass +class _DatedStore: + """Store the date and the store.""" + + mtime: float + store: TileStore + + class MultiTileStore(TileStore): """Redirect to the corresponding Tilestore for the layer and config file.""" @@ -16,14 +26,19 @@ def __init__(self, get_store: Callable[[str, str], TileStore | None]) -> None: """Initialize.""" TileStore.__init__(self) self.get_store = get_store - self.stores: dict[tuple[str, str], TileStore | None] = {} + self.stores: dict[tuple[str, str], _DatedStore | None] = {} def _get_store(self, config_file: str, layer: str) -> TileStore | None: + mtime = Path(config_file).stat().st_mtime store = self.stores.get((config_file, layer)) + if store is not None and store.mtime != mtime: + store = None if store is None: - store = self.get_store(config_file, layer) - self.stores[(config_file, layer)] = store - return store + tile_store = self.get_store(config_file, layer) + if tile_store is not None: + store = _DatedStore(mtime, tile_store) + self.stores[(config_file, layer)] = store + return store.store if store is not None else None def _get_store_tile(self, tile: Tile) -> TileStore | None: """Return the store corresponding to the tile."""