Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport master] Reload the store on file modification #2668

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all 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
23 changes: 19 additions & 4 deletions tilecloud_chain/multitilestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@

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."""

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."""
Expand Down
Loading