import logging from datetime import datetime from pyhamtools import LookupLib, Callinfo from core.constants import BANDS, UNKNOWN_BAND, CW_MODES, PHONE_MODES, DATA_MODES, ALL_MODES # Static lookup helpers from pyhamtools # todo in future add QRZ as a second lookup option in case it provides more data? lookuplib = LookupLib(lookuptype="countryfile") callinfo = Callinfo(lookuplib) # Infer a mode from the comment def infer_mode_from_comment(comment): for mode in ALL_MODES: if mode in comment.upper(): return mode return None # Infer a "mode family" from a mode. def infer_mode_family_from_mode(mode): if mode.upper() in CW_MODES: return "CW" elif mode.upper() in PHONE_MODES: return "PHONE" elif mode.upper() in DATA_MODES: return "DATA" else: if mode.upper() != "OTHER": logging.warn("Found an unrecognised mode: " + mode + ". Developer should categorise this.") return None # Infer a band from a frequency in kHz def infer_band_from_freq(freq): 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(call): try: return callinfo.get_country_name(call) except KeyError as e: return None # Infer a DXCC ID from a callsign def infer_dxcc_id_from_callsign(call): try: return callinfo.get_adif_id(call) except KeyError as e: return None # Infer a continent shortcode from a callsign def infer_continent_from_callsign(call): try: return callinfo.get_continent(call) except KeyError as e: return None # Infer a CQ zone from a callsign def infer_cq_zone_from_callsign(call): try: return callinfo.get_cqz(call) except KeyError as e: return None # Infer a ITU zone from a callsign def infer_itu_zone_from_callsign(call): try: return callinfo.get_ituz(call) except KeyError as e: return None # Convert objects to serialisable things. Used by JSON serialiser as a default when it encounters unserializable things. # Converts datetimes to ISO. # Anything else it tries to convert to a dict. def serialize_everything(obj): if isinstance(obj, datetime): return obj.isoformat() return obj.__dict__