Skip to content

Commit

Permalink
[Halpy-361] Split differentiate() function
Browse files Browse the repository at this point in the history
- Splits the ``differentiate()`` function into two functions, one that actually differentiates a system/cmdr/caseID input and one that handles calls to this function.
- Updates relevant commands to use the new ``diff_handler()`` function.
- Updates ``Points`` class to make ``point_b`` optional.
  • Loading branch information
C1701D committed May 13, 2024
1 parent 600a3f1 commit d23d59e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 54 deletions.
115 changes: 65 additions & 50 deletions halpybot/commands/edsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from ..packages.case import get_case


async def differentiate(
async def diff_handler(
ctx: Context, args: List[str], single_arg: bool = False
) -> Points:
"""
Expand All @@ -44,41 +44,47 @@ async def differentiate(
args (List): A list of arguments from the bot.
Returns:
Points (Points): A Points object containing two Point objects and an optional Jump range
Points (Points): A Points object containing one or two Point objects and an optional Jump range
Raises:
DifferentiateArgsIssue: Arguments are malformed
"""
# Parse System/CMDR/caseID from string
list_to_str = " ".join([str(elem) for elem in args])
list_points: List[str] = list_to_str.split(":")
if len(list_points) and single_arg:
list_points = [list_points[0], ""]
if len(list_points) < 2:
if single_arg:
if single_arg: # Handle single argument commands
if len(list_points): # Point provided
point_a: Point = Point(list_points[0].strip())
points: Points = Points(point_a)
else: # No point provided
await ctx.reply("Please provide a point to look up.")
raise DifferentiateArgsIssue
if not point_a.name: # No point provided
await ctx.reply("Please provide a point to look up.")
raise DifferentiateArgsIssue
elif len(list_points) < 2: # Multi-argument command but less than 2 points provided
await ctx.reply("Please provide two points to look up, separated by a :")
raise DifferentiateArgsIssue
point_a: Point = Point(list_points[0].strip())
point_b: Point = Point(list_points[1].strip())
points: Points = Points(point_a, point_b)
else: # Handle multi-argument command with two points provided
point_a: Point = Point(list_points[0].strip())
point_b: Point = Point(list_points[1].strip())
points: Points = Points(point_a, point_b)
if not point_a.name or not point_b.name: # One or both points are missing
await ctx.reply("Please provide two points to look up, separated by a :")
raise DifferentiateArgsIssue

if not single_arg and (not point_a.name or not point_b.name):
await ctx.reply("Please provide two points to look up, separated by a :")
raise DifferentiateArgsIssue
if not point_a.name: # If no point_a at this time then single_arg must be True
await ctx.reply("Please provide a point to look up.")
raise DifferentiateArgsIssue
# Differentiate points
points.point_a = await differentiate(ctx=ctx, input=points.point_a.name)
if points.point_b: # Multi-argument command
points.point_b = await differentiate(ctx=ctx, input=points.point_b.name)

# Define Jump Count
if len(list_points) == 3:
try:
# Parse Jump Range if provided
if len(list_points) == 3: # Jump Range provided
try: # Attempt to parse Jump Range
points.jump_range = float(
re.sub("(?i)LY", "", "".join(list_points[2])).strip()
)
except ValueError as val_err:
# Jump Range must not be formatted correctly
except ValueError as val_err: # Jump Range must not be formatted correctly
await ctx.reply(
"The Jump Range must be given as digits with an optional decimal point."
)
Expand All @@ -87,34 +93,43 @@ async def differentiate(
# Jump Range has values that don't really make sense
await ctx.reply("The Jump Range must be between 10 LY and 500 LY.")
raise DifferentiateArgsIssue

for point in [point_a, point_b]:
# Skip second point for single_arg commands
if single_arg and not point.name:
point.name, point.pretty = "", ""
continue
# Check if Point is CaseID
try:
case: Case = await get_case(ctx, point.name)
temp_point = case.system
temp_pretty = f"Case {case.board_id} ({case.client_name} in {case.system})"
except KeyError:
temp_point = "".join(point.name).strip()
temp_pretty = await sys_cleaner(point.name)
# Check if point is CMDR
try:
loc_cmdr = await Commander.location(name=point.name)
# Looks like CMDR's back on the menu, boys!
if loc_cmdr and loc_cmdr.system is not None:
temp_point = await sys_cleaner(loc_cmdr.system)
temp_pretty = f"{await sys_cleaner(point.name)} (in {temp_point})"
except EDSMLookupError:
logger.warning("EDSM appears to be down! Trying to continue regardless...")
await ctx.reply("Warning! EDSM appears to be down. Trying to continue.")
point.name, point.pretty = temp_point, temp_pretty
return points


async def differentiate(ctx: Context, input: str) -> Points:
"""
Differentiate if a given input is a CMDR, Case, or System.
Args:
ctx (Context): The Bot Context
input (str): A string to differentiate
Returns:
Point (Point): A Point object containing the system name and a formatted name
"""
point: Point = Point(input)
# Check if Point is CaseID
try:
case: Case = await get_case(ctx, point.name)
temp_point = case.system
temp_pretty = f"Case {case.board_id} ({case.client_name} in {case.system})"
except KeyError:
temp_point = "".join(point.name).strip()
temp_pretty = await sys_cleaner(point.name)
# Check if point is CMDR
try:
loc_cmdr = await Commander.location(name=point.name)
# Looks like CMDR's back on the menu,
if loc_cmdr and loc_cmdr.system is not None:
temp_point = await sys_cleaner(loc_cmdr.system)
temp_pretty = f"{await sys_cleaner(point.name)} (in {temp_point})"
except EDSMLookupError:
logger.warning("EDSM appears to be down! Trying to continue regardless...")
await ctx.reply("Warning! EDSM appears to be down. Trying to continue.")
point.name, point.pretty = temp_point, temp_pretty
return point


@Commands.command("lookup", "syslookup")
@sys_exceptions
async def cmd_systemlookup(ctx: Context, cleaned_sys, cache_override):
Expand All @@ -126,7 +141,7 @@ async def cmd_systemlookup(ctx: Context, cleaned_sys, cache_override):
"""
try:
# Process provided arguments
points: Points = await differentiate(
points: Points = await diff_handler(
ctx=ctx, args=cleaned_sys.split(), single_arg=True
)
except DifferentiateArgsIssue:
Expand Down Expand Up @@ -167,7 +182,7 @@ async def cmd_distlookup(ctx: Context, args: List[str], cache_override):
"""
try:
# Process provided arguments
points: Points = await differentiate(ctx=ctx, args=args)
points: Points = await diff_handler(ctx=ctx, args=args)
except DifferentiateArgsIssue:
# Arguments were malformed, user has already been informed, abort
return await ctx.reply("Error encountered in Dist command, not continuing")
Expand Down Expand Up @@ -199,7 +214,7 @@ async def cmd_landmarklookup(ctx: Context, cleaned_sys, cache_override):
"""
try:
# Process provided arguments
points: Points = await differentiate(
points: Points = await diff_handler(
ctx=ctx, args=cleaned_sys.split(), single_arg=True
)
except DifferentiateArgsIssue:
Expand Down Expand Up @@ -235,7 +250,7 @@ async def cmd_dssalookup(ctx: Context, cleaned_sys, cache_override):
"""
try:
# Process provided arguments
points: Points = await differentiate(
points: Points = await diff_handler(
ctx=ctx, args=cleaned_sys.split(), single_arg=True
)
except DifferentiateArgsIssue:
Expand Down Expand Up @@ -281,7 +296,7 @@ async def cmd_diversionlookup(ctx: Context, cleaned_sys, cache_override):
"""
try:
# Process provided arguments
points: Points = await differentiate(
points: Points = await diff_handler(
ctx=ctx, args=cleaned_sys.split(), single_arg=True
)
except DifferentiateArgsIssue:
Expand Down
4 changes: 2 additions & 2 deletions halpybot/commands/spansh.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import List
from loguru import logger
from halpybot import config
from .edsm import differentiate
from .edsm import diff_handler
from ..packages.exceptions import DifferentiateArgsIssue
from ..packages.utils import spansh, dist_exceptions
from ..packages.command import Commands, get_help_text
Expand All @@ -31,7 +31,7 @@ async def cmd_spansh(ctx: Context, args: List[str], cache_override):
return await ctx.reply("Unable to comply. SPANSH module not enabled.")
try:
# Process provided arguments
points: Points = await differentiate(ctx=ctx, args=args)
points: Points = await diff_handler(ctx=ctx, args=args)
except DifferentiateArgsIssue:
# Arguments were malformed, user has already been informed, abort
return await ctx.reply("Error encountered in Dist command, not continuing")
Expand Down
4 changes: 2 additions & 2 deletions halpybot/packages/models/edsm_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class Point:

@define()
class Points:
"""A pair of valid EDSM Points"""
"""One or two valid EDSM Points and an optional jump range"""

point_a: Point
point_b: Point
point_b: Optional[Point] = None
jump_range: Optional[float] = None

0 comments on commit d23d59e

Please sign in to comment.