Skip to content

Commit

Permalink
Fix CLI readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Fireye04 committed Jun 25, 2024
1 parent 81ba01e commit 30e5a96
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 56 deletions.
77 changes: 33 additions & 44 deletions src/sasquatchbackpack/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ def check_duration(
ctx: click.Context, param: dict, value: tuple[int, int]
) -> tuple[int, int]:
"""Validate duration inputs."""
total_duration = timedelta(value[0], 0, 0, 0, 0, value[1], 0)
days, hours = value
total_duration = timedelta(days=days, hours=hours)

if total_duration > timedelta(10000, 0, 0, 0, 0, 0, 0):
if total_duration > timedelta(days=10000):
raise click.BadParameter(
f"""Your provided duration ({total_duration!s}) is
too large. The maximum is 10000 days."""
)
if total_duration < timedelta(0, 0, 0, 0, 0, 1, 0):
if total_duration < timedelta(hours=1):
raise click.BadParameter(
f"""Your provided duration ({total_duration!s}) is
too small. The minimum is 1 hour."""
Expand Down Expand Up @@ -54,26 +55,17 @@ def check_coords(
ctx: click.Context, param: dict, value: tuple[float, float]
) -> tuple[float, float]:
"""Validate coords inputs."""
if value[0] < -90.0:
latitude, longitude = value
if latitude < -90.0 or latitude > 90.0:
raise click.BadParameter(
f"""Your provided latitude ({value[0]}) is too low.
The minimum is -90."""
)
if value[0] > 90.0:
raise click.BadParameter(
f"""Your provided latitude ({value[0]}) is too high.
The maximum is 90."""
f"Your provided latitude ({latitude}) is out of bounds."
"The range is -90 to 90."
)

if value[1] < -180:
raise click.BadParameter(
f"""Your provided longitude ({value[1]}) is too low.
The minimum is -180."""
)
if value[1] > 180:
if longitude < -180.0 or longitude > 180.0:
raise click.BadParameter(
f"""Your provided longitude ({value[1]}) is too high.
The maximum is 180."""
f"Your provided longitude ({longitude}) is out of bounds."
"The range is -180 to 180."
)

return value
Expand All @@ -83,32 +75,23 @@ def check_magnitude_bounds(
ctx: click.Context, param: dict, value: tuple[int, int]
) -> tuple[int, int]:
"""Validate magnitude bounds."""
if value[0] < 0:
lower, upper = value
if lower < 0 or lower > 10:
raise click.BadParameter(
f"""Your provided minimum magnitude ({value[0]}) is
too small. The minimum is 0."""
)
if value[0] > 10:
raise click.BadParameter(
f"""Your provided minimum magnitude ({value[0]}) is
too large. The maximum is 10."""
f"Your provided minimum magnitude ({lower}) is "
"out of bounds. The range is 0 to 10."
)

if value[1] > 10:
if upper > 10 or upper < 0:
raise click.BadParameter(
f"""Your provided maximum magnitude ({value[1]}) is
too large. The maximum is 10."""
)
if value[1] < 0:
raise click.BadParameter(
f"""Your provided maximum magnitude ({value[1]}) is
too small. The minimum is 0."""
f"Your provided maximum magnitude ({upper}) is "
"out of bounds. The range is 0 to 10."
)

if value[0] > value[1]:
if lower > upper:
raise click.BadParameter(
f"""Your provided minimum magnitude ({value[0]})
cannot excede your provided maximum magnitude ({value[1]})."""
f"""Your provided minimum magnitude ({lower})
cannot excede your provided maximum magnitude ({upper})."""
)

return value
Expand Down Expand Up @@ -157,17 +140,25 @@ def main() -> None:
show_default=True,
callback=check_magnitude_bounds,
)
@click.option(
"--dry-run",
is_flag=True,
default=False,
help="Perform a trial run with no data being sent to Kafka.",
)
def usgs_earthquake_data(
duration: tuple[int, int],
radius: int,
coords: tuple[float, float],
magnitude_bounds: tuple[int, int],
dry_run: bool, # noqa: FBT001
) -> None:
"""Seaches USGS databases for relevant earthquake data and prints it
to console. Optionally, also allows the user to post the
queried data to kafka.
"""
total_duration = timedelta(duration[0], 0, 0, 0, 0, duration[1], 0)
days, hours = duration
total_duration = timedelta(days=days, hours=hours)

results = usgs.search_api(
total_duration,
Expand All @@ -189,7 +180,9 @@ def usgs_earthquake_data(
click.echo("------")
return

click.confirm("Do you want to post the above data?", abort=True)
if dry_run:
click.echo("Dry run mode: No data will be sent to Kafka.")
return

click.echo("Sending data...")

Expand All @@ -207,7 +200,3 @@ def usgs_earthquake_data(
click.secho(result, fg="red")
else:
click.secho("Data successfully sent!", fg="green")


if __name__ == "__main__":
main()
24 changes: 12 additions & 12 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,84 +21,84 @@
400,
(-30.22573200864174, -70.73932987127506),
(2, 10),
["duration", "too large"],
["duration", "large"],
),
(
(0, 0),
400,
(-30.22573200864174, -70.73932987127506),
(2, 10),
["duration", "too small"],
["duration", "small"],
),
(
(50, 0),
5001,
(-30.22573200864174, -70.73932987127506),
(2, 10),
["radius", "too large"],
["radius", "large"],
),
(
(50, 0),
0,
(-30.22573200864174, -70.73932987127506),
(2, 10),
["radius", "too small"],
["radius", "small"],
),
(
(50, 0),
400,
(-91, -70.73932987127506),
(2, 10),
["latitude", "too low"],
["latitude", "out of bounds"],
),
(
(50, 0),
400,
(91, -70.73932987127506),
(2, 10),
["latitude", "too high"],
["latitude", "out of bounds"],
),
(
(50, 0),
400,
(-30.22573200864174, -181),
(2, 10),
["longitude", "too low"],
["longitude", "out of bounds"],
),
(
(50, 0),
400,
(-30.22573200864174, 181),
(2, 10),
["longitude", "too high"],
["longitude", "out of bounds"],
),
(
(50, 0),
100,
(-30.22573200864174, -70.73932987127506),
(-1, 10),
["minimum magnitude", "too small"],
["minimum magnitude", "out of bounds"],
),
(
(50, 0),
100,
(-30.22573200864174, -70.73932987127506),
(11, 10),
["minimum magnitude", "too large"],
["minimum magnitude", "out of bounds"],
),
(
(50, 0),
100,
(-30.22573200864174, -70.73932987127506),
(2, -1),
["maximum magnitude", "too small"],
["maximum magnitude", "out of bounds"],
),
(
(50, 0),
100,
(-30.22573200864174, -70.73932987127506),
(2, 11),
["maximum magnitude", "too large"],
["maximum magnitude", "out of bounds"],
),
(
(50, 0),
Expand Down

0 comments on commit 30e5a96

Please sign in to comment.