mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-08-06 02:21:42 +00:00
Refactor of caching & data storage part 14 #118
This commit is contained in:
@@ -18,6 +18,9 @@ class APIQueryCallsignDataProvider(CallsignDataProvider):
|
||||
""" Set up the provider."""
|
||||
super().__init__(name, provider_config, storage)
|
||||
|
||||
if self.enabled:
|
||||
self.status = "Ready"
|
||||
|
||||
def start(self):
|
||||
pass
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ class CallsignDataProvider:
|
||||
|
||||
self.name = name
|
||||
self.enabled = provider_config["enabled"]
|
||||
self.priority = int(provider_config["priority"])
|
||||
self.last_update_time = datetime.min.replace(tzinfo=pytz.UTC)
|
||||
self.status = "Not Started" if self.enabled else "Disabled"
|
||||
self._storage = storage
|
||||
@@ -38,13 +39,16 @@ class CallsignDataProvider:
|
||||
possible. Data is cached internally for a set period of 30 days to avoid the need to request data from servers
|
||||
each time."""
|
||||
|
||||
if callsign in self._storage:
|
||||
return self._storage[callsign]
|
||||
if self.enabled:
|
||||
if callsign in self._storage:
|
||||
return self._storage[callsign]
|
||||
else:
|
||||
c = self._perform_new_lookup(callsign, lookup_credentials)
|
||||
if c:
|
||||
self._storage.set(callsign, c, expire=DATA_STORE.CALLSIGN_DATA_TTL_SEC)
|
||||
return c
|
||||
else:
|
||||
c = self._perform_new_lookup(callsign, lookup_credentials)
|
||||
if c:
|
||||
self._storage.set(callsign, c, expire=DATA_STORE.CALLSIGN_DATA_TTL_SEC)
|
||||
return c
|
||||
return None
|
||||
|
||||
|
||||
def _perform_new_lookup(self, callsign, lookup_credentials):
|
||||
|
||||
@@ -28,16 +28,17 @@ class ClublogAPI(APIQueryCallsignDataProvider):
|
||||
|
||||
super().__init__("Clublog API", provider_config, DATA_STORE.callsign_data_clublogapi)
|
||||
|
||||
self.status = "Ready"
|
||||
|
||||
|
||||
def _perform_new_lookup(self, callsign, lookup_credentials):
|
||||
callsign_data = Callsign(call=callsign)
|
||||
|
||||
try:
|
||||
callsign_data = get_callsign_object_from_pyhamtools_callinfo(callsign, self._callinfo)
|
||||
self.status = "OK"
|
||||
self.last_update_time = datetime.now(pytz.UTC)
|
||||
if self._callinfo:
|
||||
callsign_data = get_callsign_object_from_pyhamtools_callinfo(callsign, self._callinfo)
|
||||
self.status = "OK"
|
||||
self.last_update_time = datetime.now(pytz.UTC)
|
||||
else:
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
self.status = "Error"
|
||||
|
||||
@@ -49,4 +49,7 @@ class ClublogXML(FileDownloadCallsignDataProvider):
|
||||
return False
|
||||
|
||||
def _perform_new_lookup(self, callsign, lookup_credentials):
|
||||
return get_callsign_object_from_pyhamtools_callinfo(callsign, self._callinfo)
|
||||
if self._callinfo:
|
||||
return get_callsign_object_from_pyhamtools_callinfo(callsign, self._callinfo)
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -30,4 +30,7 @@ class CountryFiles(FileDownloadCallsignDataProvider):
|
||||
return False
|
||||
|
||||
def _perform_new_lookup(self, callsign, lookup_credentials):
|
||||
return get_callsign_object_from_pyhamtools_callinfo(callsign, self._callinfo)
|
||||
if self._callinfo:
|
||||
return get_callsign_object_from_pyhamtools_callinfo(callsign, self._callinfo)
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -24,6 +24,9 @@ class FileDownloadCallsignDataProvider(CallsignDataProvider):
|
||||
self._stop_event = Event()
|
||||
self._url_data_cache = URLDataCache("callsigndata_" + name)
|
||||
|
||||
if self.enabled:
|
||||
self.status = "Ready"
|
||||
|
||||
def start(self):
|
||||
# Fire off the polling thread. It will poll immediately on startup, then sleep for poll_interval between
|
||||
# subsequent polls, so start() returns immediately and the application can continue starting.
|
||||
|
||||
@@ -27,11 +27,10 @@ class HamQTH(APIQueryCallsignDataProvider):
|
||||
# and password, this is valid for an hour, so our cache stores this specifically for 55 minutes.
|
||||
self._CREDENTIALS_CACHE = CachedSession(CACHE_DIR + "/urls/hamqth-creds",
|
||||
expire_after=timedelta(minutes=55))
|
||||
self.status = "Ready"
|
||||
|
||||
def _perform_new_lookup(self, callsign, lookup_credentials):
|
||||
# If we don't have HamQTH credentials, skip this lookup
|
||||
if not ((lookup_credentials.hamqth_username and lookup_credentials.hamqth_password)
|
||||
if not lookup_credentials or not ((lookup_credentials.hamqth_username and lookup_credentials.hamqth_password)
|
||||
or lookup_credentials.hamqth_session_key):
|
||||
return None
|
||||
|
||||
@@ -134,4 +133,5 @@ class HamQTH(APIQueryCallsignDataProvider):
|
||||
grid=grid,
|
||||
dxcc_id=int(data["adif"]) if "adif" in data else None,
|
||||
cq_zone=int(data["cq"]) if "cq" in data else None,
|
||||
itu_zone=int(data["itu"]) if "itu" in data else None)
|
||||
itu_zone=int(data["itu"]) if "itu" in data else None,
|
||||
location_source="HOME QTH")
|
||||
|
||||
@@ -25,11 +25,10 @@ class QRZ(APIQueryCallsignDataProvider):
|
||||
# and password, this is valid for an hour, so our cache stores this specifically for 55 minutes.
|
||||
self._CREDENTIALS_CACHE = CachedSession(CACHE_DIR + "/urls/qrz-creds",
|
||||
expire_after=timedelta(minutes=55))
|
||||
self.status = "Ready"
|
||||
|
||||
def _perform_new_lookup(self, callsign, lookup_credentials):
|
||||
# If we don't have QRZ credentials, skip this lookup
|
||||
if not ((lookup_credentials.qrz_username and lookup_credentials.qrz_password)
|
||||
if not lookup_credentials or not ((lookup_credentials.qrz_username and lookup_credentials.qrz_password)
|
||||
or lookup_credentials.qrz_session_key):
|
||||
return None
|
||||
|
||||
@@ -153,4 +152,5 @@ class QRZ(APIQueryCallsignDataProvider):
|
||||
grid=grid,
|
||||
dxcc_id=int(data["adif"]) if "adif" in data else None,
|
||||
cq_zone=int(data["cqzone"]) if "cqzone" in data else None,
|
||||
itu_zone=int(data["ituzone"]) if "ituzone" in data else None)
|
||||
itu_zone=int(data["ituzone"]) if "ituzone" in data else None,
|
||||
location_source="HOME QTH")
|
||||
|
||||
Reference in New Issue
Block a user