mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-20 22:37:44 +00:00
39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
import logging
|
|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
import pytz
|
|
|
|
from data.activity_ref import ActivityRef
|
|
from providers.activityrefdata.activity_ref_data_provider import ActivityRefDataProvider
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class LocalFileActivityRefDataProvider(ActivityRefDataProvider):
|
|
"""Generic activity ref data provider class for providers that fetch their data from a local file on startup."""
|
|
|
|
def __init__(self, sig_name: str, provider_config: dict[str, Any], path: str) -> None:
|
|
super().__init__(sig_name, provider_config)
|
|
self._path = path
|
|
|
|
def start(self) -> None:
|
|
logger.debug(f"Loading {self.sig_name} activity ref data from file.")
|
|
try:
|
|
new_data = self._file_to_data(self._path)
|
|
if new_data:
|
|
self._add_data(new_data)
|
|
self.status = "OK"
|
|
self.last_update_time = datetime.now(pytz.UTC)
|
|
else:
|
|
self.status = "Error"
|
|
logger.info(f"Failed to load activity ref data for {self.sig_name}")
|
|
except Exception:
|
|
self.status = "Error"
|
|
logger.exception(f"Exception in local file Activity Ref Data Provider ({self.sig_name})")
|
|
|
|
def _file_to_data(self, path: str) -> list[ActivityRef]:
|
|
"""Load a file on the given path and turn it into activity ref data."""
|
|
|
|
raise NotImplementedError("Subclasses must implement this method")
|