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] Be sure that the config file exists #2670

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
45 changes: 40 additions & 5 deletions tilecloud_chain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from argparse import ArgumentParser, Namespace
from collections.abc import Callable, Iterable, Iterator
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from datetime import datetime, timedelta
from fractions import Fraction
from hashlib import sha1
Expand Down Expand Up @@ -1542,6 +1543,14 @@ def __call__(self, tile: Tile) -> Tile | None:
return None


@dataclass
class _DatedAction:
"""Dated action."""

mtime: float
action: Callable[[Tile], Tile | None]


class MultiAction:
"""
Used to perform an action based on the tile's layer name.
Expand All @@ -1554,21 +1563,47 @@ def __init__(
get_action: Callable[[str, str], Callable[[Tile], Tile | None] | None],
) -> None:
self.get_action = get_action
self.actions: dict[tuple[str, str], Callable[[Tile], Tile | None] | None] = {}
self.actions: dict[tuple[str, str], _DatedAction | None] = {}

def _get_action(self, config_file: str, layer: str) -> Callable[[Tile], Tile | None] | None:
"""Get the action based on the tile's layer name."""
config_path = pathlib.Path(config_file)
if not config_path.exists():
_LOGGER.warning("Config file %s does not exist", config_file)
return None
mtime = config_path.stat().st_mtime
action = self.actions.get((config_file, layer))
if action is not None and action.mtime != mtime:
action = None
if action is None:
action_item = self.get_action(config_file, layer)
if action_item is not None:
action = _DatedAction(mtime, action_item)
self.actions[(config_file, layer)] = action
return action.action if action is not None else None

def __call__(self, tile: Tile) -> Tile | None:
"""Run the action."""
layer = tile.metadata["layer"]
config_file = tile.metadata["config_file"]
action = self.actions.get((config_file, layer))
if action is None:
action = self.get_action(config_file, layer)
self.actions[(config_file, layer)] = action
action = self._get_action(config_file, layer)
if action:
_LOGGER.debug("[%s] Run action %s.", tile.tilecoord, action)
return action(tile)
return tile

def __str__(self) -> str:
"""Return a string representation of the object."""
actions = {str(action) for action in self.actions.values()}
keys = {f"{config_file}:{layer}" for config_file, layer in self.actions}
return f"{self.__class__.__name__}({', '.join(actions)} - {', '.join(keys)})"

def __repr__(self) -> str:
"""Return a string representation of the object."""
actions = {repr(action) for action in self.actions.values()}
keys = {f"{config_file}:{layer}" for config_file, layer in self.actions}
return f"{self.__class__.__name__}({', '.join(actions)} - {', '.join(keys)})"


class HashLogger:
"""Log the tile size and hash."""
Expand Down
6 changes: 5 additions & 1 deletion tilecloud_chain/multitilestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def __init__(self, get_store: Callable[[str, str], TileStore | None]) -> 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
config_path = Path(config_file)
if not config_path.exists():
logger.warning("Config file %s does not exist", config_file)
return None
mtime = config_path.stat().st_mtime
store = self.stores.get((config_file, layer))
if store is not None and store.mtime != mtime:
store = None
Expand Down
Loading