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 1093db9
Showing 1 changed file with 19 additions and 4 deletions.
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

0 comments on commit 1093db9

Please sign in to comment.