Use ruff linter to fix issues and provide consistent formatting

This commit is contained in:
Ian Renton
2026-08-15 08:25:54 +01:00
parent 7391c28cd0
commit af3f82c14d
121 changed files with 1989 additions and 996 deletions
+8 -5
View File
@@ -120,12 +120,12 @@ class Alert:
self.id = hashlib.sha256(str({"s": self.source, "sid": self.source_id}).encode("utf-8")).hexdigest()
else:
self.id = hashlib.sha256(
str({"s": self.source, "c": self.dx_calls, "t": self.start_time}).encode("utf-8")).hexdigest()
str({"s": self.source, "c": self.dx_calls, "t": self.start_time}).encode("utf-8")
).hexdigest()
# DX operator name lookup, using QRZ.com/HamQTH.
if self.dx_calls and not self.dx_names:
self.dx_names = list(
map(lambda c: get_call_info(c, credentials).name, self.dx_calls))
self.dx_names = list(map(lambda c: get_call_info(c, credentials).name, self.dx_calls))
except Exception:
logging.exception("Exception while inferring missing data from spot")
@@ -141,5 +141,8 @@ class Alert:
either having an end_time in the past, or if it only has a start_time, then that start time was more than 3 hours
ago. If it somehow doesn't have a start_time either, it is considered to be expired."""
return not self.start_time or (self.end_time and self.end_time < datetime.now(pytz.UTC).timestamp()) or (
not self.end_time and self.start_time < (datetime.now(pytz.UTC) - timedelta(hours=3)).timestamp())
return (
not self.start_time
or (self.end_time and self.end_time < datetime.now(pytz.UTC).timestamp())
or (not self.end_time and self.start_time < (datetime.now(pytz.UTC) - timedelta(hours=3)).timestamp())
)
+18 -8
View File
@@ -13,18 +13,18 @@ class Callsign:
# "Home" call, i.e. with any prefixes and suffixes stripped off
home_call: str | None = None
# Operator name
name : str | None = None
name: str | None = None
# QTH (location), free text
qth : str | None = None
qth: str | None = None
# Maidenhead grid locator. Depending on the source of lookup this could be a home location from QRZ/HamQTH or just
# the centre of the country they're operating in if no other data is available.
grid : str | None = None
grid: str | None = None
# Latitude. Depending on the source of lookup this could be a home location from QRZ/HamQTH or just
# the centre of the country they're operating in if no other data is available.
latitude : float | None = None
latitude: float | None = None
# Longitude. Depending on the source of lookup this could be a home location from QRZ/HamQTH or just
# the centre of the country they're operating in if no other data is available.
longitude : float | None = None
longitude: float | None = None
# Country in which the callsign indicates they are operating
country: str | None = None
# Continent in which the callsign indicates they are operating
@@ -42,6 +42,16 @@ class Callsign:
"""Utility method to indicate that the callsign data is fully populated. Multiple providers can return data for
a callsign, and we try them in sequence until we have all the data we can, in which case there's no point
querying any other providers."""
return self.home_call is not None and self.name is not None and self.qth is not None and self.grid is not None\
and self.latitude is not None and self.longitude is not None and self.country is not None and self.continent\
is not None and self.dxcc_id is not None and self.cq_zone is not None and self.itu_zone is not None
return (
self.home_call is not None
and self.name is not None
and self.qth is not None
and self.grid is not None
and self.latitude is not None
and self.longitude is not None
and self.country is not None
and self.continent is not None
and self.dxcc_id is not None
and self.cq_zone is not None
and self.itu_zone is not None
)
+1
View File
@@ -4,6 +4,7 @@ from dataclasses import dataclass
@dataclass
class LookupCredentials:
"""Per-request credentials for QRZ.com and HamQTH online callsign lookups."""
qrz_username: str = ""
qrz_password: str = ""
qrz_session_key: str = "" # alternative to username/password
+6 -6
View File
@@ -4,12 +4,12 @@ from dataclasses import dataclass, field
@dataclass
class SIG:
"""Data class that defines a Special Interest Group. Each contains a name and a longer form description.
They also contain comment_names which attempts to separate out the way people might refer to it in
cluster comments from how it is referred to in the UI & API. (For example, "TOTA" in cluster spot comments
almost always means Towers on the Air, but no single programme is referred to in the UI as "TOTA" as
it's ambiguous between Towers, Toilets and Tiles. And while Beaches got the name "BOTA" first, "BOTA" spots
are much more likely to be bunkers.) Finally, there is a ref_regex which provides a regular expression to
match what references (such as parks and summits) look like for that programme."""
They also contain comment_names which attempts to separate out the way people might refer to it in
cluster comments from how it is referred to in the UI & API. (For example, "TOTA" in cluster spot comments
almost always means Towers on the Air, but no single programme is referred to in the UI as "TOTA" as
it's ambiguous between Towers, Toilets and Tiles. And while Beaches got the name "BOTA" first, "BOTA" spots
are much more likely to be bunkers.) Finally, there is a ref_regex which provides a regular expression to
match what references (such as parks and summits) look like for that programme."""
# SIG name as used in the UI and API, e.g. "Towers"
name: str
+2 -2
View File
@@ -82,9 +82,9 @@ def _xray_blackout_scale(xray):
number = float(xray[1:])
except ValueError:
return 0
if letter == 'M':
if letter == "M":
return 1 if number < 5 else 2
if letter == 'X':
if letter == "X":
if number < 10:
return 3
if number < 20:
+47 -25
View File
@@ -1,5 +1,4 @@
import hashlib
import hashlib
import json
import logging
import re
@@ -7,16 +6,25 @@ from dataclasses import dataclass
from datetime import datetime, timedelta
import pytz
from pyhamtools.locator import locator_to_latlong, latlong_to_locator
from pyhamtools.locator import latlong_to_locator, locator_to_latlong
from core.call_lookup_helper import get_call_info
from core.config import MAX_SPOT_AGE
from core.constants import MODE_ALIASES, PROPAGATION_MODES
from core.geo_utils import lat_lon_to_cq_zone, lat_lon_to_itu_zone
from core.sig_lookup_helper import populate_missing_sig_ref_info
from core.sig_utils import ANY_SIG_REGEX, get_ref_regex_for_sig, get_sig_name_from_comment_name
from core.utils import infer_band_from_freq, infer_mode_from_comment, \
infer_mode_from_frequency, infer_mode_type_from_mode, get_flag_for_dxcc
from core.sig_utils import (
ANY_SIG_REGEX,
get_ref_regex_for_sig,
get_sig_name_from_comment_name,
)
from core.utils import (
get_flag_for_dxcc,
infer_band_from_freq,
infer_mode_from_comment,
infer_mode_from_frequency,
infer_mode_type_from_mode,
)
from data.sig_ref import SIGRef
@@ -141,10 +149,7 @@ class Spot:
objects such as the sig_refs list.."""
if self.sig_refs:
self.sig_refs = [
sig_ref if isinstance(sig_ref, SIGRef) else SIGRef(**sig_ref)
for sig_ref in self.sig_refs
]
self.sig_refs = [sig_ref if isinstance(sig_ref, SIGRef) else SIGRef(**sig_ref) for sig_ref in self.sig_refs]
def infer_missing(self, credentials=None):
"""Infer missing parameters where possible"""
@@ -207,8 +212,11 @@ class Spot:
# Spotter country, continent, zones etc. from callsign.
# DE call with no digits, or APRS servers starting "T2" are not things we can look up location for
de_call_info = get_call_info(self.de_call, credentials)
if self.de_call and any(char.isdigit() for char in self.de_call) and not (
self.de_call.startswith("T2") and self.source == "APRS-IS"):
if (
self.de_call
and any(char.isdigit() for char in self.de_call)
and not (self.de_call.startswith("T2") and self.source == "APRS-IS")
):
if not self.de_country:
self.de_country = de_call_info.country
if not self.de_continent:
@@ -279,9 +287,11 @@ class Spot:
# If so, add that to the sig_refs list for this spot.
ref_regex = get_ref_regex_for_sig(found_sig)
if ref_regex:
ref_matches = re.finditer(r"(^|\W)" + found_sig + r"([ -])(" + ref_regex + r")($|\W)",
self.comment,
re.IGNORECASE)
ref_matches = re.finditer(
r"(^|\W)" + found_sig + r"([ -])(" + ref_regex + r")($|\W)",
self.comment,
re.IGNORECASE,
)
for ref_match in ref_matches:
self._append_sig_ref_if_missing(SIGRef(id=ref_match.group(3).upper(), sig=found_sig))
@@ -312,8 +322,9 @@ class Spot:
# being the only source we have for propagation mode. Brace for nightmare regex from hell.
if self.comment:
grid_mode_grid_match = re.search(
r'\b([A-Ra-r]{2}\d{2}(?:[A-Xa-x]{2}(?:\d{2})?)?)(?:<([^>]*)>|\(([^)]*)\))([A-Ra-r]{2}\d{2}(?:[A-Xa-x]{2}(?:\d{2})?)?)\b',
self.comment)
r"\b([A-Ra-r]{2}\d{2}(?:[A-Xa-x]{2}(?:\d{2})?)?)(?:<([^>]*)>|\(([^)]*)\))([A-Ra-r]{2}\d{2}(?:[A-Xa-x]{2}(?:\d{2})?)?)\b",
self.comment,
)
if grid_mode_grid_match:
# regex matches, so extract grids:
if not self.de_grid:
@@ -329,13 +340,14 @@ class Spot:
self.propagation_mode = PROPAGATION_MODES[mode_tag]
else:
self.propagation_mode = mode_tag
logging.info("Seen a new propagation mode tag not yet in the system: %s", mode_tag)
logging.info(f"Seen a new propagation mode tag not yet in the system: {mode_tag}")
# Parse "de_grid -> dx_grid" structures from the comment
if self.comment:
grid_mode_grid_match = re.search(
r'\b([A-Ra-r]{2}\d{2}(?:[A-Xa-x]{2}(?:\d{2})?)?)\s*->\s*([A-Ra-r]{2}\d{2}(?:[A-Xa-x]{2}(?:\d{2})?)?)\b',
self.comment)
r"\b([A-Ra-r]{2}\d{2}(?:[A-Xa-x]{2}(?:\d{2})?)?)\s*->\s*([A-Ra-r]{2}\d{2}(?:[A-Xa-x]{2}(?:\d{2})?)?)\b",
self.comment,
)
if grid_mode_grid_match:
# regex matches, so extract grids:
if not self.dx_grid:
@@ -370,7 +382,8 @@ class Spot:
self.id = hashlib.sha256(str({"s": self.source, "sid": self.source_id}).encode("utf-8")).hexdigest()
else:
self.id = hashlib.sha256(
str({"s": self.source, "c": self.dx_call, "t": self.time}).encode("utf-8")).hexdigest()
str({"s": self.source, "c": self.dx_call, "t": self.time}).encode("utf-8")
).hexdigest()
# DX operator details lookup. This should be the last resort compared to taking the data from the actual
# spotting service, e.g. we don't want to accidentally use a user's QRZ.com home lat/lon or DXCC lat/lon
@@ -407,14 +420,23 @@ class Spot:
# DX Location is "good" if it is from a spot, or from QRZ if the callsign doesn't contain a slash, so the operator
# is likely at home.
self.dx_location_good = bool(self.dx_latitude and self.dx_longitude and (
self.dx_location_source == "SPOT" or self.dx_location_source == "SIG REF LOOKUP"
self.dx_location_good = bool(
self.dx_latitude
and self.dx_longitude
and (
self.dx_location_source == "SPOT"
or self.dx_location_source == "SIG REF LOOKUP"
or self.dx_location_source == "GRID"
or (self.dx_location_source == "HOME QTH" and "/" not in (self.dx_call or ""))))
or (self.dx_location_source == "HOME QTH" and "/" not in (self.dx_call or ""))
)
)
# DE with no digits and APRS servers starting "T2" are not things we can look up location for
if self.de_call and any(char.isdigit() for char in str(self.de_call)) and not (
self.de_call.startswith("T2") and self.source == "APRS-IS"):
if (
self.de_call
and any(char.isdigit() for char in str(self.de_call))
and not (self.de_call.startswith("T2") and self.source == "APRS-IS")
):
# DE operator location lookup
if not self.de_latitude:
self.de_latitude = de_call_info.latitude