mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-20 14:27:42 +00:00
Refactor activity names to use an enum instead of a string to avoid typos #147
This commit is contained in:
@@ -3,9 +3,9 @@ import re
|
||||
|
||||
from pyhamtools.locator import latlong_to_locator, locator_to_latlong
|
||||
|
||||
from core.constants import ACTIVITIES
|
||||
from core.activity_utils import get_activity_by_name
|
||||
from core.data_store import DATA_STORE
|
||||
from core.enums import ActivityRefType
|
||||
from core.enums import ActivityName, ActivityRefType
|
||||
from core.geo_utils import wab_wai_square_to_lat_lon
|
||||
from data.activity_ref import ActivityRef
|
||||
|
||||
@@ -30,10 +30,10 @@ def get_activity_ref_info(activity_name, ref_id):
|
||||
activity_ref = ActivityRef(sig=activity_name, id=ref_id)
|
||||
|
||||
# We can always get the reference type and the icon from the activity itself
|
||||
for activity in ACTIVITIES:
|
||||
if activity.name.upper() == activity_name.upper():
|
||||
activity_ref.ref_type = activity.ref_type
|
||||
activity_ref.icon = activity.icon
|
||||
activity = get_activity_by_name(activity_name)
|
||||
if activity:
|
||||
activity_ref.ref_type = activity.ref_type
|
||||
activity_ref.icon = activity.icon
|
||||
|
||||
try:
|
||||
### FUDGES ###
|
||||
@@ -41,7 +41,7 @@ def get_activity_ref_info(activity_name, ref_id):
|
||||
# DME fudge. Our database has leading zeros padding to 5 digits which is the expected format, but not all
|
||||
# activators add leading zeros. We also need to normalise "DME 01234" to "DME-01234" to match what's in our
|
||||
# database.
|
||||
if activity_name.upper() == "DME":
|
||||
if activity_name.upper() == ActivityName.DME:
|
||||
match = re.match(r"DME[\- ](\d{3,5})", ref_id, re.IGNORECASE)
|
||||
if match:
|
||||
number = match.group(1)
|
||||
@@ -49,21 +49,21 @@ def get_activity_ref_info(activity_name, ref_id):
|
||||
|
||||
# DTMBA spotters sometimes include spaces and dashes, our regex allows them but they must be removed here so we
|
||||
# can look up against the official list which doesn't have them
|
||||
if activity_name.upper() == "DTMBA":
|
||||
if activity_name.upper() == ActivityName.DTMBA:
|
||||
ref_id = ref_id.replace("-", "").replace(" ", "")
|
||||
|
||||
### NO DATA ACTIVITIES ###
|
||||
#
|
||||
# If the activity is HEMA or BIWOTA, we have no way to either generate useful data or look it up on a
|
||||
# reference list, so just skip the lookup here.
|
||||
if activity_name.upper() == "HEMA" or activity_name.upper() == "BIWOTA":
|
||||
if activity_name.upper() == ActivityName.HEMA or activity_name.upper() == ActivityName.BIWOTA:
|
||||
return activity_ref
|
||||
|
||||
### PROGRAMMATIC DATA GENERATION INSTEAD OF LOOKUPS ###
|
||||
#
|
||||
# If the activity is Tiles, WAB, WAI or BOTA (Beaches), we don't have anything to look up from the data
|
||||
# store, we can calculate all the information we are going to get directly.
|
||||
if activity_name.upper() == "TILES":
|
||||
if activity_name.upper() == ActivityName.TILES.upper():
|
||||
# Tiles on the Air just uses Maidenhead 6-digit squares, so ID, Name and Grid are all the same
|
||||
if not activity_ref.name:
|
||||
activity_ref.name = activity_ref.id
|
||||
@@ -75,7 +75,7 @@ def get_activity_ref_info(activity_name, ref_id):
|
||||
activity_ref.longitude = ll[1]
|
||||
return activity_ref
|
||||
|
||||
elif activity_name.upper() == "WAB" or activity_name.upper() == "WAI":
|
||||
elif activity_name.upper() == ActivityName.WAB or activity_name.upper() == ActivityName.WAI:
|
||||
ll = wab_wai_square_to_lat_lon(ref_id)
|
||||
if ll:
|
||||
activity_ref.name = ref_id
|
||||
@@ -87,7 +87,7 @@ def get_activity_ref_info(activity_name, ref_id):
|
||||
logger.warning("Invalid lat/lon received for WAB/WAI reference")
|
||||
return activity_ref
|
||||
|
||||
elif activity_name.upper() == "BOTA":
|
||||
elif activity_name.upper() == ActivityName.BOTA:
|
||||
# For BOTA all we can ever generate is the URL, there is no data file or lookup for lat/longs
|
||||
if not activity_ref.name:
|
||||
activity_ref.name = activity_ref.id
|
||||
@@ -97,11 +97,11 @@ def get_activity_ref_info(activity_name, ref_id):
|
||||
)
|
||||
return activity_ref
|
||||
|
||||
elif activity_name.upper() == "GMA Islands":
|
||||
elif activity_name.upper() == ActivityName.GMA_ISLANDS.upper():
|
||||
# GMA Islands is a bit of a mess of GMA and IOTA references. Try looking them both up and see what returns
|
||||
# the best result.
|
||||
iota_lookup = get_activity_ref_info("IOTA", ref_id)
|
||||
gma_lookup = get_activity_ref_info("GMA", ref_id)
|
||||
iota_lookup = get_activity_ref_info(ActivityName.IOTA, ref_id)
|
||||
gma_lookup = get_activity_ref_info(ActivityName.GMA, ref_id)
|
||||
for key, value in iota_lookup.__dict__.items():
|
||||
if value is not None and activity_ref.__dict__.get(key) is None:
|
||||
activity_ref.__dict__[key] = value
|
||||
|
||||
Reference in New Issue
Block a user