mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-03-15 12:24:29 +00:00
Bulk convert comments above classes/functions/methods into proper docstrings
This commit is contained in:
@@ -19,13 +19,14 @@ from core.constants import BANDS, UNKNOWN_BAND, CW_MODES, PHONE_MODES, DATA_MODE
|
||||
HTTP_HEADERS, HAMQTH_PRG, MODE_ALIASES
|
||||
|
||||
|
||||
# Singleton class that provides lookup functionality.
|
||||
class LookupHelper:
|
||||
"""Singleton class that provides lookup functionality."""
|
||||
|
||||
# Create the lookup helper. Note that nothing actually happens until the start() method is called, and that all
|
||||
# lookup methods will fail if start() has not yet been called. This therefore needs starting before any spot or
|
||||
# alert handlers are created.
|
||||
def __init__(self):
|
||||
"""Create the lookup helper. Note that nothing actually happens until the start() method is called, and that all
|
||||
lookup methods will fail if start() has not yet been called. This therefore needs starting before any spot or
|
||||
alert handlers are created."""
|
||||
|
||||
self.CLUBLOG_CALLSIGN_DATA_CACHE = None
|
||||
self.LOOKUP_LIB_CLUBLOG_XML = None
|
||||
self.CLUBLOG_XML_AVAILABLE = None
|
||||
@@ -105,11 +106,12 @@ class LookupHelper:
|
||||
for dxcc in self.DXCC_DATA.values():
|
||||
dxcc["_prefixRegexCompiled"] = re.compile(dxcc["prefixRegex"])
|
||||
|
||||
# Download the cty.plist file from country-files.com on first startup. The pyhamtools lib can actually download and use
|
||||
# this itself, but it's occasionally offline which causes it to throw an error. By downloading it separately, we can
|
||||
# catch errors and handle them, falling back to a previous copy of the file in the cache, and we can use the
|
||||
# requests_cache library to prevent re-downloading too quickly if the software keeps restarting.
|
||||
def download_country_files_cty_plist(self):
|
||||
"""Download the cty.plist file from country-files.com on first startup. The pyhamtools lib can actually download and use
|
||||
this itself, but it's occasionally offline which causes it to throw an error. By downloading it separately, we can
|
||||
catch errors and handle them, falling back to a previous copy of the file in the cache, and we can use the
|
||||
requests_cache library to prevent re-downloading too quickly if the software keeps restarting."""
|
||||
|
||||
try:
|
||||
logging.info("Downloading Country-files.com cty.plist...")
|
||||
response = SEMI_STATIC_URL_DATA_CACHE.get("https://www.country-files.com/cty/cty.plist",
|
||||
@@ -124,12 +126,14 @@ class LookupHelper:
|
||||
logging.error("Exception when downloading Clublog cty.xml", e)
|
||||
return False
|
||||
|
||||
# Download the dxcc.json file on first startup.
|
||||
def download_dxcc_json(self):
|
||||
"""Download the dxcc.json file on first startup."""
|
||||
|
||||
try:
|
||||
logging.info("Downloading dxcc.json...")
|
||||
response = SEMI_STATIC_URL_DATA_CACHE.get("https://raw.githubusercontent.com/k0swe/dxcc-json/refs/heads/main/dxcc.json",
|
||||
headers=HTTP_HEADERS).text
|
||||
response = SEMI_STATIC_URL_DATA_CACHE.get(
|
||||
"https://raw.githubusercontent.com/k0swe/dxcc-json/refs/heads/main/dxcc.json",
|
||||
headers=HTTP_HEADERS).text
|
||||
|
||||
with open(self.DXCC_JSON_DOWNLOAD_LOCATION, "w") as f:
|
||||
f.write(response)
|
||||
@@ -140,9 +144,10 @@ class LookupHelper:
|
||||
logging.error("Exception when downloading dxcc.json", e)
|
||||
return False
|
||||
|
||||
# Download the cty.xml (gzipped) file from Clublog on first startup, so we can use it in preference to querying the
|
||||
# database live if possible.
|
||||
def download_clublog_ctyxml(self):
|
||||
"""Download the cty.xml (gzipped) file from Clublog on first startup, so we can use it in preference to querying the
|
||||
database live if possible."""
|
||||
|
||||
try:
|
||||
logging.info("Downloading Clublog cty.xml.gz...")
|
||||
response = self.CLUBLOG_CTY_XML_CACHE.get("https://cdn.clublog.org/cty.php?api=" + self.CLUBLOG_API_KEY,
|
||||
@@ -161,8 +166,9 @@ class LookupHelper:
|
||||
logging.error("Exception when downloading Clublog cty.xml", e)
|
||||
return False
|
||||
|
||||
# Infer a mode from the comment
|
||||
def infer_mode_from_comment(self, comment):
|
||||
"""Infer a mode from the comment"""
|
||||
|
||||
for mode in ALL_MODES:
|
||||
if mode in comment.upper():
|
||||
return mode
|
||||
@@ -171,8 +177,9 @@ class LookupHelper:
|
||||
return MODE_ALIASES[mode]
|
||||
return None
|
||||
|
||||
# Infer a "mode family" from a mode.
|
||||
def infer_mode_type_from_mode(self, mode):
|
||||
"""Infer a "mode family" from a mode."""
|
||||
|
||||
if mode.upper() in CW_MODES:
|
||||
return "CW"
|
||||
elif mode.upper() in PHONE_MODES:
|
||||
@@ -184,15 +191,17 @@ class LookupHelper:
|
||||
logging.warn("Found an unrecognised mode: " + mode + ". Developer should categorise this.")
|
||||
return None
|
||||
|
||||
# Infer a band from a frequency in Hz
|
||||
def infer_band_from_freq(self, freq):
|
||||
"""Infer a band from a frequency in Hz"""
|
||||
|
||||
for b in BANDS:
|
||||
if b.start_freq <= freq <= b.end_freq:
|
||||
return b
|
||||
return UNKNOWN_BAND
|
||||
|
||||
# Infer a country name from a callsign
|
||||
def infer_country_from_callsign(self, call):
|
||||
"""Infer a country name from a callsign"""
|
||||
|
||||
try:
|
||||
# Start with the basic country-files.com-based decoder.
|
||||
country = self.CALL_INFO_BASIC.get_country_name(call)
|
||||
@@ -224,8 +233,9 @@ class LookupHelper:
|
||||
country = dxcc_data["name"]
|
||||
return country
|
||||
|
||||
# Infer a DXCC ID from a callsign
|
||||
def infer_dxcc_id_from_callsign(self, call):
|
||||
"""Infer a DXCC ID from a callsign"""
|
||||
|
||||
try:
|
||||
# Start with the basic country-files.com-based decoder.
|
||||
dxcc = self.CALL_INFO_BASIC.get_adif_id(call)
|
||||
@@ -257,8 +267,9 @@ class LookupHelper:
|
||||
dxcc = dxcc_data["entityCode"]
|
||||
return dxcc
|
||||
|
||||
# Infer a continent shortcode from a callsign
|
||||
def infer_continent_from_callsign(self, call):
|
||||
"""Infer a continent shortcode from a callsign"""
|
||||
|
||||
try:
|
||||
# Start with the basic country-files.com-based decoder.
|
||||
continent = self.CALL_INFO_BASIC.get_continent(call)
|
||||
@@ -286,8 +297,9 @@ class LookupHelper:
|
||||
continent = dxcc_data["continent"][0]
|
||||
return continent
|
||||
|
||||
# Infer a CQ zone from a callsign
|
||||
def infer_cq_zone_from_callsign(self, call):
|
||||
"""Infer a CQ zone from a callsign"""
|
||||
|
||||
try:
|
||||
# Start with the basic country-files.com-based decoder.
|
||||
cqz = self.CALL_INFO_BASIC.get_cqz(call)
|
||||
@@ -320,8 +332,9 @@ class LookupHelper:
|
||||
cqz = dxcc_data["cq"][0]
|
||||
return cqz
|
||||
|
||||
# Infer a ITU zone from a callsign
|
||||
def infer_itu_zone_from_callsign(self, call):
|
||||
"""Infer a ITU zone from a callsign"""
|
||||
|
||||
try:
|
||||
# Start with the basic country-files.com-based decoder.
|
||||
ituz = self.CALL_INFO_BASIC.get_ituz(call)
|
||||
@@ -345,12 +358,14 @@ class LookupHelper:
|
||||
ituz = dxcc_data["itu"]
|
||||
return ituz
|
||||
|
||||
# Get an emoji flag for a given DXCC entity ID
|
||||
def get_flag_for_dxcc(self, dxcc):
|
||||
"""Get an emoji flag for a given DXCC entity ID"""
|
||||
|
||||
return self.DXCC_DATA[dxcc]["flag"] if dxcc in self.DXCC_DATA else None
|
||||
|
||||
# Infer an operator name from a callsign (requires QRZ.com/HamQTH)
|
||||
def infer_name_from_callsign_online_lookup(self, call):
|
||||
"""Infer an operator name from a callsign (requires QRZ.com/HamQTH)"""
|
||||
|
||||
data = self.get_qrz_data_for_callsign(call)
|
||||
if data and "fname" in data:
|
||||
name = data["fname"]
|
||||
@@ -363,32 +378,41 @@ class LookupHelper:
|
||||
else:
|
||||
return None
|
||||
|
||||
# Infer a latitude and longitude from a callsign (requires QRZ.com/HamQTH)
|
||||
# Coordinates that look default are rejected (apologies if your position really is 0,0, enjoy your voyage)
|
||||
def infer_latlon_from_callsign_online_lookup(self, call):
|
||||
"""Infer a latitude and longitude from a callsign (requires QRZ.com/HamQTH)
|
||||
Coordinates that look default are rejected (apologies if your position really is 0,0, enjoy your voyage)"""
|
||||
|
||||
data = self.get_qrz_data_for_callsign(call)
|
||||
if data and "latitude" in data and "longitude" in data and (float(data["latitude"]) != 0 or float(data["longitude"]) != 0) and -89.9 < float(data["latitude"]) < 89.9:
|
||||
if data and "latitude" in data and "longitude" in data and (
|
||||
float(data["latitude"]) != 0 or float(data["longitude"]) != 0) and -89.9 < float(
|
||||
data["latitude"]) < 89.9:
|
||||
return [float(data["latitude"]), float(data["longitude"])]
|
||||
data = self.get_hamqth_data_for_callsign(call)
|
||||
if data and "latitude" in data and "longitude" in data and (float(data["latitude"]) != 0 or float(data["longitude"]) != 0) and -89.9 < float(data["latitude"]) < 89.9:
|
||||
if data and "latitude" in data and "longitude" in data and (
|
||||
float(data["latitude"]) != 0 or float(data["longitude"]) != 0) and -89.9 < float(
|
||||
data["latitude"]) < 89.9:
|
||||
return [float(data["latitude"]), float(data["longitude"])]
|
||||
else:
|
||||
return None
|
||||
|
||||
# Infer a grid locator from a callsign (requires QRZ.com/HamQTH).
|
||||
# Grids that look default are rejected (apologies if your grid really is AA00aa, enjoy your research)
|
||||
def infer_grid_from_callsign_online_lookup(self, call):
|
||||
"""Infer a grid locator from a callsign (requires QRZ.com/HamQTH).
|
||||
Grids that look default are rejected (apologies if your grid really is AA00aa, enjoy your research)"""
|
||||
|
||||
data = self.get_qrz_data_for_callsign(call)
|
||||
if data and "locator" in data and data["locator"].upper() != "AA00" and data["locator"].upper() != "AA00AA" and data["locator"].upper() != "AA00AA00":
|
||||
if data and "locator" in data and data["locator"].upper() != "AA00" and data["locator"].upper() != "AA00AA" and \
|
||||
data["locator"].upper() != "AA00AA00":
|
||||
return data["locator"]
|
||||
data = self.get_hamqth_data_for_callsign(call)
|
||||
if data and "grid" in data and data["grid"].upper() != "AA00" and data["grid"].upper() != "AA00AA" and data["grid"].upper() != "AA00AA00":
|
||||
if data and "grid" in data and data["grid"].upper() != "AA00" and data["grid"].upper() != "AA00AA" and data[
|
||||
"grid"].upper() != "AA00AA00":
|
||||
return data["grid"]
|
||||
else:
|
||||
return None
|
||||
|
||||
# Infer a textual QTH from a callsign (requires QRZ.com/HamQTH)
|
||||
def infer_qth_from_callsign_online_lookup(self, call):
|
||||
"""Infer a textual QTH from a callsign (requires QRZ.com/HamQTH)"""
|
||||
|
||||
data = self.get_qrz_data_for_callsign(call)
|
||||
if data and "addr2" in data:
|
||||
return data["addr2"]
|
||||
@@ -398,8 +422,9 @@ class LookupHelper:
|
||||
else:
|
||||
return None
|
||||
|
||||
# Infer a latitude and longitude from a callsign (using DXCC, probably very inaccurate)
|
||||
def infer_latlon_from_callsign_dxcc(self, call):
|
||||
"""Infer a latitude and longitude from a callsign (using DXCC, probably very inaccurate)"""
|
||||
|
||||
try:
|
||||
data = self.CALL_INFO_BASIC.get_lat_long(call)
|
||||
if data and "latitude" in data and "longitude" in data:
|
||||
@@ -419,8 +444,9 @@ class LookupHelper:
|
||||
loc = [float(data["Lat"]), float(data["Lon"])]
|
||||
return loc
|
||||
|
||||
# Infer a grid locator from a callsign (using DXCC, probably very inaccurate)
|
||||
def infer_grid_from_callsign_dxcc(self, call):
|
||||
"""Infer a grid locator from a callsign (using DXCC, probably very inaccurate)"""
|
||||
|
||||
latlon = self.infer_latlon_from_callsign_dxcc(call)
|
||||
grid = None
|
||||
try:
|
||||
@@ -429,8 +455,9 @@ class LookupHelper:
|
||||
logging.debug("Invalid lat/lon received for DXCC")
|
||||
return grid
|
||||
|
||||
# Infer a mode from the frequency (in Hz) according to the band plan. Just a guess really.
|
||||
def infer_mode_from_frequency(self, freq):
|
||||
"""Infer a mode from the frequency (in Hz) according to the band plan. Just a guess really."""
|
||||
|
||||
try:
|
||||
khz = freq / 1000.0
|
||||
mode = freq_to_band(khz)["mode"]
|
||||
@@ -449,8 +476,9 @@ class LookupHelper:
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
# Utility method to get QRZ.com data from cache if possible, if not get it from the API and cache it
|
||||
def get_qrz_data_for_callsign(self, call):
|
||||
"""Utility method to get QRZ.com data from cache if possible, if not get it from the API and cache it"""
|
||||
|
||||
# Fetch from cache if we can, otherwise fetch from the API and cache it
|
||||
if call in self.QRZ_CALLSIGN_DATA_CACHE:
|
||||
return self.QRZ_CALLSIGN_DATA_CACHE.get(call)
|
||||
@@ -477,8 +505,9 @@ class LookupHelper:
|
||||
else:
|
||||
return None
|
||||
|
||||
# Utility method to get HamQTH data from cache if possible, if not get it from the API and cache it
|
||||
def get_hamqth_data_for_callsign(self, call):
|
||||
"""Utility method to get HamQTH data from cache if possible, if not get it from the API and cache it"""
|
||||
|
||||
# Fetch from cache if we can, otherwise fetch from the API and cache it
|
||||
if call in self.HAMQTH_CALLSIGN_DATA_CACHE:
|
||||
return self.HAMQTH_CALLSIGN_DATA_CACHE.get(call)
|
||||
@@ -505,7 +534,8 @@ class LookupHelper:
|
||||
try:
|
||||
lookup_data = SEMI_STATIC_URL_DATA_CACHE.get(
|
||||
self.HAMQTH_BASE_URL + "?id=" + session_id + "&callsign=" + urllib.parse.quote_plus(
|
||||
callinfo.Callinfo.get_homecall(call)) + "&prg=" + HAMQTH_PRG, headers=HTTP_HEADERS).content
|
||||
callinfo.Callinfo.get_homecall(call)) + "&prg=" + HAMQTH_PRG,
|
||||
headers=HTTP_HEADERS).content
|
||||
data = xmltodict.parse(lookup_data)["HamQTH"]["search"]
|
||||
self.HAMQTH_CALLSIGN_DATA_CACHE.add(call, data, expire=604800) # 1 week in seconds
|
||||
return data
|
||||
@@ -520,8 +550,9 @@ class LookupHelper:
|
||||
logging.error("Exception when looking up HamQTH data")
|
||||
return None
|
||||
|
||||
# Utility method to get Clublog API data from cache if possible, if not get it from the API and cache it
|
||||
def get_clublog_api_data_for_callsign(self, call):
|
||||
"""Utility method to get Clublog API data from cache if possible, if not get it from the API and cache it"""
|
||||
|
||||
# Fetch from cache if we can, otherwise fetch from the API and cache it
|
||||
if call in self.CLUBLOG_CALLSIGN_DATA_CACHE:
|
||||
return self.CLUBLOG_CALLSIGN_DATA_CACHE.get(call)
|
||||
@@ -547,8 +578,9 @@ class LookupHelper:
|
||||
else:
|
||||
return None
|
||||
|
||||
# Utility method to get Clublog XML data from file
|
||||
def get_clublog_xml_data_for_callsign(self, call):
|
||||
"""Utility method to get Clublog XML data from file"""
|
||||
|
||||
if self.CLUBLOG_XML_AVAILABLE:
|
||||
try:
|
||||
data = self.LOOKUP_LIB_CLUBLOG_XML.lookup_callsign(callsign=call)
|
||||
@@ -560,15 +592,17 @@ class LookupHelper:
|
||||
else:
|
||||
return None
|
||||
|
||||
# Utility method to get generic DXCC data from our lookup table, if we can find it
|
||||
def get_dxcc_data_for_callsign(self, call):
|
||||
"""Utility method to get generic DXCC data from our lookup table, if we can find it"""
|
||||
|
||||
for entry in self.DXCC_DATA.values():
|
||||
if entry["_prefixRegexCompiled"].match(call):
|
||||
return entry
|
||||
return None
|
||||
|
||||
# Shutdown method to close down any caches neatly.
|
||||
def stop(self):
|
||||
"""Shutdown method to close down any caches neatly."""
|
||||
|
||||
self.QRZ_CALLSIGN_DATA_CACHE.close()
|
||||
self.CLUBLOG_CALLSIGN_DATA_CACHE.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user