Skip to content

Commit

Permalink
feat(cli): added click cli support
Browse files Browse the repository at this point in the history
  • Loading branch information
shinybrar committed May 10, 2024
1 parent f085e07 commit bc6f784
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 37 deletions.
7 changes: 3 additions & 4 deletions frbvoe/backend/voe.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""VOEvent Server Blueprint."""

import picologging as logging
from pymongo.errors import PyMongoError
from sanic import Blueprint
from sanic.log import logger
from sanic.request import Request
from sanic.response import json as json_response
from sanic_ext import openapi

import picologging as logging
logging.basicConfig()
log = logging.getLogger()

Expand All @@ -17,11 +17,12 @@

voe = Blueprint("voe", url_prefix="/")


# Post at /create_voe
@voe.post("create_voe")
@openapi.response(201, description="Validates VOEvent data from a host observatory.")
# Add the validated payload to the MongoDB Database
async def create_voe(request: Request): #TODO: Shiny, should I add voe_event: VOEvent?
async def create_voe(request: Request): # TODO: Shiny, should I add voe_event: VOEvent?
"""Process a VOEvent.
Args:
Expand All @@ -40,8 +41,6 @@ async def create_voe(request: Request): #TODO: Shiny, should I add voe_event: VO
log.info("Processing VOEvent")
voe = VOEvent(**request.json)
print(voe.json())



# Send VOEvent to Comet
# comet_report = Comet(**request.json)
Expand Down
Empty file added frbvoe/cli/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions frbvoe/cli/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""FRB VOE CLI."""

import click
from frbvoe.cli.voe import voe


@click.group()
def cli():
"""FRB VOE Command Line Interface."""
pass

@cli.command("version", help="FRB VOE version.")
def version():
"""FRB VOE version."""
click.echo("VOEvent Tools v0.1.0")

cli.add_command(voe)
20 changes: 20 additions & 0 deletions frbvoe/cli/voe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""VOE CLI."""

import click

@click.group(name="voe", help="VOEvent Tools.")
def voe():
"""Manage workflow pipelines."""
pass

@voe.command("send", help="Send VOEvent.")
@click.option(
"--hostname",
default="localhost",
help="Destination to send the VOE."
)
@click.option("--port", default=8098, help="Port to send the VOE.")
@click.option("--file", default="./voe",type=click.File("r"), help="VOEvent file.", show_default=True)
def send(hostname, port):
"""Send VOEvent."""
click.echo(f"Send VOEvent.{hostname}:{port}")
24 changes: 8 additions & 16 deletions frbvoe/models/voe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from typing import Literal, Optional

import picologging as logging
from pydantic import EmailStr, Field, StrictFloat, StrictInt, StrictStr, BaseModel
from pydantic import BaseModel, EmailStr, Field, StrictFloat, StrictInt, StrictStr
from pydantic_settings import BaseSettings, SettingsConfigDict
from sanic import Request

logging.basicConfig()
log = logging.getLogger()


class VOEvent(BaseModel): #BaseSettings
class VOEvent(BaseModel): # BaseSettings
"""VOEvent Object.
Args:
Expand Down Expand Up @@ -70,23 +70,17 @@ class VOEvent(BaseModel): #BaseSettings
"subsequent",
"retraction",
"update",
] = Field(
...,
description="Which kind of VOEvent. Required.",
example="detection"
)
] = Field(..., description="Which kind of VOEvent. Required.", example="detection")
observatory_name: StrictStr = Field(
...,
description="Name of the host observatory. Required.",
example="CHIME"
..., description="Name of the host observatory. Required.", example="CHIME"
)
# date: datetime = Field(
# ...,
# gt=datetime(2024, 1, 1), # release date of frb-voe
# description="Detection time of the FRB. Required.",
# example="2020-01-13 16:55:08.844845",
# )
date : StrictStr = Field(
date: StrictStr = Field(
...,
# release date of frb-voe
description="Detection time of the FRB. Required.",
Expand Down Expand Up @@ -179,9 +173,7 @@ class VOEvent(BaseModel): #BaseSettings
example=13.8,
)
flux: float = Field(
default=None,
description="Flux of the FRB in Jy. Optional.",
example=4.9
default=None, description="Flux of the FRB in Jy. Optional.", example=4.9
)
right_ascension: float = Field(
default=None,
Expand Down Expand Up @@ -214,7 +206,7 @@ class VOEvent(BaseModel): #BaseSettings
website: Optional[StrictStr] = Field(
default=None,
description="Link to the host observatory website. Optional.",
example="https://host_observatory.com/"
example="https://host_observatory.com/",
)
tns_name: Optional[StrictStr] = Field(
default=None,
Expand All @@ -232,7 +224,7 @@ def payload(self):
log.info("Returning VOEvent payload")
return self.dict()

@staticmethod #TODO: Shiny what's this for?
@staticmethod # TODO: Shiny what's this for?
async def compile(request: Request):
"""Extracts data from request and returns object.
Expand Down
7 changes: 3 additions & 4 deletions frbvoe/utilities/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def send_email(email_report: Dict[str, Any]):
message["To"] = receiver_email
message["Subject"] = subject
message.attach(MIMEText(email_message, "plain"))
#print email message

# print email message
print(email_message)

# # Connect to the SMTP server
Expand All @@ -125,7 +125,6 @@ def send_email(email_report: Dict[str, Any]):
return status



# \t\tbackend: {email_report['backend']}\n
# \n
# \tevent parameters:\n
Expand Down Expand Up @@ -154,4 +153,4 @@ def send_email(email_report: Dict[str, Any]):
# \n\n
# WHY\n
# \tImportance: {email_report['importance']}\n
# \n\n
# \n\n
29 changes: 16 additions & 13 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ comet = "^3.1.0"
sanic = {extras = ["ext"], version = "^23.12"}
motor = "^3.4.0"
numpy = "^1.26.4"
click = "^8.1.7"


[tool.poetry.group.dev.dependencies]
pytest = "^8.1.1"
pre-commit = "^3.7.0"
pytest-cov = "^5.0.0"

[tool.poetry.scripts]
frbvoe = "frbvoe.cli.main:cli"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

0 comments on commit bc6f784

Please sign in to comment.