Skip to content

Commit

Permalink
Fix ruff linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Fireye04 committed Jun 3, 2024
1 parent fb363d5 commit 0bd8dd8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 108 deletions.
63 changes: 25 additions & 38 deletions src/sasquatchbackpack/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Backpack CLI."""

from datetime import timedelta

import click
Expand All @@ -11,37 +13,35 @@

DEFAULT_MAGNITUDE_BOUNDS = (2, 10)

DEFAULT_TEST = False


def check_duration(
ctx: click.Context, param: dict, value: tuple[int, int]
) -> tuple[int, int]:
"""Validate duration inputs"""
"""Validate duration inputs."""
total_duration = timedelta(value[0], 0, 0, 0, 0, value[1], 0)

if total_duration > timedelta(10000, 0, 0, 0, 0, 0, 0):
raise click.BadParameter(
f"""Your provided duration ({str(total_duration)}) is
f"""Your provided duration ({total_duration!s}) is
too large. The maximum is 10000 days."""
)
elif total_duration < timedelta(0, 0, 0, 0, 0, 1, 0):
if total_duration < timedelta(0, 0, 0, 0, 0, 1, 0):
raise click.BadParameter(
f"""Your provided duration ({str(total_duration)}) is
f"""Your provided duration ({total_duration!s}) is
too small. The minimum is 1 hour."""
)

return value


def check_radius(ctx: click.Context, param: dict, value: int) -> int:
"""Validate radius inputs"""
"""Validate radius inputs."""
if value > 5000:
raise click.BadParameter(
f"""Your provided radius ({value}) is too large.
The maximum is 5000."""
)
elif value <= 0:
if value <= 0:
raise click.BadParameter(
f"""Your provided radius ({value}) is too small.
The minimum is 0.1."""
Expand All @@ -53,13 +53,13 @@ def check_radius(ctx: click.Context, param: dict, value: int) -> int:
def check_coords(
ctx: click.Context, param: dict, value: tuple[float, float]
) -> tuple[float, float]:
"""Validate coords inputs"""
"""Validate coords inputs."""
if value[0] < -90.0:
raise click.BadParameter(
f"""Your provided latitude ({value[0]}) is too low.
The minimum is -90."""
)
elif value[0] > 90.0:
if value[0] > 90.0:
raise click.BadParameter(
f"""Your provided latitude ({value[0]}) is too high.
The maximum is 90."""
Expand All @@ -70,7 +70,7 @@ def check_coords(
f"""Your provided longitude ({value[1]}) is too low.
The minimum is -180."""
)
elif value[1] > 180:
if value[1] > 180:
raise click.BadParameter(
f"""Your provided longitude ({value[1]}) is too high.
The maximum is 180."""
Expand All @@ -82,13 +82,13 @@ def check_coords(
def check_magnitude_bounds(
ctx: click.Context, param: dict, value: tuple[int, int]
) -> tuple[int, int]:
"""Validate magnitude bounds"""
"""Validate magnitude bounds."""
if value[0] < 0:
raise click.BadParameter(
f"""Your provided minimum magnitude ({value[0]}) is
too small. The minimum is 0."""
)
elif value[0] > 10:
if value[0] > 10:
raise click.BadParameter(
f"""Your provided minimum magnitude ({value[0]}) is
too large. The maximum is 10."""
Expand All @@ -99,7 +99,7 @@ def check_magnitude_bounds(
f"""Your provided maximum magnitude ({value[1]}) is
too large. The maximum is 10."""
)
elif value[1] < 0:
if value[1] < 0:
raise click.BadParameter(
f"""Your provided maximum magnitude ({value[1]}) is
too small. The minimum is 0."""
Expand All @@ -124,7 +124,7 @@ def main() -> None:
@click.option(
"-d",
"--duration",
help="How far back from the present should be searched" + " (days, hours)",
help="How far back from the present should be searched (days, hours)",
required=True,
type=(int, int),
callback=check_duration,
Expand All @@ -141,9 +141,8 @@ def main() -> None:
@click.option(
"-c",
"--coords",
help="latitude and longitude of the central coordnates"
+ " (latitude, longitude). Defaults to the coordinates of"
+ " Cerro Pachon.",
help="latitude and longitude of the central coordnates "
"(latitude, longitude). Defaults to the coordinates of Cerro Pachon.",
default=DEFAULT_COORDS,
type=(float, float),
show_default=True,
Expand All @@ -158,23 +157,14 @@ def main() -> None:
show_default=True,
callback=check_magnitude_bounds,
)
@click.option(
"-t",
"--test",
help="set to True to echo API results without sending them",
default=DEFAULT_TEST,
type=bool,
show_default=True,
)
def usgs_earthquake_data(
duration: tuple[int, int],
radius: int,
coords: tuple[float, float],
magnitude_bounds: tuple[int, int],
test: bool,
) -> None:
"""Seaches USGS databases for relevant earthquake data and prints it
to console
to console.
"""
total_duration = timedelta(duration[0], 0, 0, 0, 0, duration[1], 0)

Expand All @@ -198,19 +188,16 @@ def usgs_earthquake_data(
click.echo("------")
return

if not test:
click.echo("Sending data...")
click.echo("Sending data...")

config = sources.USGSConfig(
total_duration, radius, coords, magnitude_bounds
)
source = sources.USGSSource(config)
config = sources.USGSConfig(
total_duration, radius, coords, magnitude_bounds
)
source = sources.USGSSource(config)

poster = sasquatch.BackpackDispatcher(
source, sasquatch.DispatcherConfig()
)
poster = sasquatch.BackpackDispatcher(source, sasquatch.DispatcherConfig())

click.echo(poster.post())
click.echo(poster.post())


if __name__ == "__main__":
Expand Down
46 changes: 24 additions & 22 deletions src/sasquatchbackpack/sasquatch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Handles dispatch of backpack data to kafka."""

import json
from dataclasses import dataclass

Expand All @@ -12,7 +14,7 @@
@dataclass
class DispatcherConfig:
"""Class containing relevant configuration information for the
BackpackDispatcher
BackpackDispatcher.
"""

sasquatch_rest_proxy_url = (
Expand All @@ -33,14 +35,16 @@ class BackpackDispatcher:
the Dispatcher
"""

def __init__(self, source: sources.DataSource, config: DispatcherConfig):
def __init__(
self, source: sources.DataSource, config: DispatcherConfig
) -> None:
self.source = source
self.config = config
self.schema = source.load_schema()
self.namespace = self.get_namespace()

def get_namespace(self) -> str:
"""Sorts the schema and returns the namespace value
"""Sorts the schema and returns the namespace value.
Returns
-------
Expand All @@ -52,7 +56,7 @@ def get_namespace(self) -> str:
return json_schema["namespace"]

def create_topic(self) -> str:
"""Creates kafka topic based off data from provided source
"""Create kafka topic based off data from provided source.
Returns
-------
Expand All @@ -64,6 +68,7 @@ def create_topic(self) -> str:
r = requests.get(
f"{self.config.sasquatch_rest_proxy_url}/v3/clusters",
headers=headers,
timeout=10,
)

cluster_id = r.json()["data"][0]["cluster_id"]
Expand All @@ -81,45 +86,42 @@ def create_topic(self) -> str:

response = requests.post(
f"{self.config.sasquatch_rest_proxy_url}/v3/clusters/"
+ f"{cluster_id}/topics",
f"{cluster_id}/topics",
json=topic_config,
headers=headers,
timeout=10,
)
return response.text

def post(self) -> dict:
"""Assemble schema and payload from the given source, then
makes a POST request to kafka
makes a POST request to kafka.
Returns
-------
response text
The results of the POST request in string format
"""
# Currently unused, TODO: Uncomment when POSTing begins
records = self.source.get_records()

payload = {"value_schema": self.schema, "records": records}

# Temporary lint bypassing during testing
# ruff: noqa: ERA001
return payload # noqa: RET504

# url = f"{self.config.sasquatch_rest_proxy_url}/topics/"
# f"{self.namespace}.{self.source.topic_name}"

# url = f"{sasquatch_rest_proxy_url}/topics/"
# + f"{self.source.namespace}.{self.source.topic_name}"
# headers = {
# "Content-Type": "application/vnd.kafka.avro.v2+json",
# "Accept": "application/vnd.kafka.v2+json",
# }
# sasquatch_rest_proxy_url = (
# "https://data-int.lsst.cloud/sasquatch-rest-proxy"
# )

records = self.source.get_records()

payload = {"value_schema": self.schema, "records": records}

# Temporarily returns payload instead of making full
# POST request.
# TODO: Once complete, delete and uncomment the following
return payload

# response = requests.request("POST",
# url,
# json=payload,
# headers=headers
# headers=headers,
# timeout=10,
# )
# return response.text
2 changes: 1 addition & 1 deletion src/sasquatchbackpack/scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"""Scripts for the CLI"""
"""Scripts for the CLI."""
10 changes: 6 additions & 4 deletions src/sasquatchbackpack/scripts/usgs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Accesses the USGSLibcomcat API."""

from datetime import datetime, timedelta

from libcomcat.search import search
Expand Down Expand Up @@ -26,9 +28,11 @@ def search_api(
max_magnitude (int, optional): maximum earthquake magnitude.
Defaults to 10.
"""
current_dt = datetime.utcnow()
# Linting bypassed, as at the time of writing Libcomcat breaks if provided
# with a timezone-aware datetime object
current_dt = datetime.now(None) # noqa: DTZ005

km_radius_events = search(
return search(
starttime=current_dt - duration,
endtime=current_dt,
maxradiuskm=radius,
Expand All @@ -37,5 +41,3 @@ def search_api(
minmagnitude=magnitude_bounds[0],
maxmagnitude=magnitude_bounds[1],
)

return km_radius_events
Loading

0 comments on commit 0bd8dd8

Please sign in to comment.