Skip to content

Commit

Permalink
Reload the store on file modification
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Dec 19, 2024
1 parent f97d20b commit 62b80e4
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions tilecloud_chain/multitilestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,41 @@

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)
store = _DatedStore(mtime, self.get_store(config_file, layer))
self.stores[(config_file, layer)] = store
return 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

0 comments on commit 62b80e4

Please sign in to comment.