Files
spothole/providers/staticdata/local_file_static_data_provider.py
T

41 lines
1.4 KiB
Python

import logging
from datetime import datetime
import pytz
from providers.staticdata.static_data_provider import StaticDataProvider
logger = logging.getLogger(__name__)
class LocalFileStaticDataProvider(StaticDataProvider):
"""Generic static reference data provider class for providers that fetch their data from a local file on startup."""
def __init__(self, name, provider_config, path):
super().__init__(name, provider_config)
self._path = path
self._stop = False
def start(self):
logger.debug(f"Loading {self.name} static reference data from file.")
try:
ok = self._load_data(self._path)
if ok:
self.status = "OK"
self.last_update_time = datetime.now(pytz.UTC)
logger.info(f"Updated static reference data for {self.name}")
else:
self.status = "Error"
logger.error(f"Failed to load data for {self.name}")
except Exception:
self.status = "Error"
logger.exception(f"Exception in local file Static Data Provider ({self.name})")
def stop(self):
self._stop = True
def _load_data(self, path):
"""Load data from the given file path. Return true if successful, false otherwise."""
raise NotImplementedError("Subclasses must implement this method")