Skip to content

Commit

Permalink
Add json option for all resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonmeso committed Jun 10, 2024
1 parent 271ad13 commit ec42795
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 6 deletions.
16 changes: 16 additions & 0 deletions giza/cli/commands/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DEBUG_OPTION,
DESCRIPTION_OPTION,
ENDPOINT_OPTION,
JSON_OPTION,
MODEL_OPTION,
NAME_OPTION,
VERSION_OPTION,
Expand Down Expand Up @@ -43,8 +44,13 @@ def create(
endpoint_id: int = ENDPOINT_OPTION,
name: Optional[str] = NAME_OPTION,
description: Optional[str] = DESCRIPTION_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:

if json:
echo.set_log_file()

echo("Creating agent ✅ ")

if not model_id and not version_id and not endpoint_id:
Expand Down Expand Up @@ -122,8 +128,12 @@ def list(
parameters: Optional[List[str]] = typer.Option(
None, "--parameters", "-p", help="The parameters of the agent"
),
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()

echo("Listing agents ✅ ")
with ExceptionHandler(debug=debug):
client = AgentsClient(API_HOST)
Expand All @@ -148,8 +158,11 @@ def list(
)
def get(
agent_id: int = AGENT_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Getting agent {agent_id} ✅ ")
with ExceptionHandler(debug=debug):
client = AgentsClient(API_HOST)
Expand Down Expand Up @@ -195,8 +208,11 @@ def update(
parameters: Optional[List[str]] = typer.Option(
None, "--parameters", "-p", help="The parameters of the agent"
),
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Updating agent {agent_id} ✅ ")
with ExceptionHandler(debug=debug):
client = AgentsClient(API_HOST)
Expand Down
19 changes: 19 additions & 0 deletions giza/cli/commands/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DEBUG_OPTION,
ENDPOINT_OPTION,
FRAMEWORK_OPTION,
JSON_OPTION,
MODEL_OPTION,
VERSION_OPTION,
)
Expand Down Expand Up @@ -76,8 +77,11 @@ def list(
only_active: bool = typer.Option(
False, "--only-active", "-a", help="Only list active endpoints"
),
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo("Listing endpoints ✅ ")
params = {}
try:
Expand Down Expand Up @@ -126,8 +130,11 @@ def list(
)
def get(
endpoint_id: int = ENDPOINT_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Getting endpoint {endpoint_id} ✅ ")
try:
client = EndpointsClient(API_HOST)
Expand Down Expand Up @@ -190,8 +197,11 @@ def delete_endpoint(
)
def list_proofs(
endpoint_id: int = ENDPOINT_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Getting proofs from endpoint {endpoint_id} ✅ ")
try:
client = EndpointsClient(API_HOST)
Expand Down Expand Up @@ -236,8 +246,11 @@ def get_proof(
proof_id: str = typer.Option(
None, "--proof-id", "-p", help="The ID or request id of the proof"
),
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Getting proof from endpoint {endpoint_id} ✅ ")
try:
client = EndpointsClient(API_HOST)
Expand Down Expand Up @@ -333,8 +346,11 @@ def download_proof(
)
def list_jobs(
endpoint_id: int = ENDPOINT_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Getting jobs from endpoint {endpoint_id} ✅ ")
with ExceptionHandler(debug=debug):
client = EndpointsClient(API_HOST)
Expand All @@ -356,8 +372,11 @@ def verify(
proof_id: str = typer.Option(
None, "--proof-id", "-p", help="The ID or request id of the proof"
),
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Verifying proof from endpoint {endpoint_id} ✅ ")
with ExceptionHandler(debug=debug):
client = EndpointsClient(API_HOST)
Expand Down
12 changes: 10 additions & 2 deletions giza/cli/commands/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from giza.cli import API_HOST
from giza.cli.client import ModelsClient
from giza.cli.options import DEBUG_OPTION, DESCRIPTION_OPTION, MODEL_OPTION
from giza.cli.options import DEBUG_OPTION, DESCRIPTION_OPTION, JSON_OPTION, MODEL_OPTION
from giza.cli.schemas.models import ModelCreate
from giza.cli.utils import echo, get_response_info

Expand All @@ -27,6 +27,7 @@
)
def get(
model_id: int = MODEL_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
"""
Expand All @@ -40,6 +41,8 @@ def get(
ValidationError: input fields are validated, if these are not suitable the exception is raised
HTTPError: request error to the API, 4XX or 5XX
"""
if json:
echo.set_log_file()
echo("Retrieving model information ✅ ")
try:
client = ModelsClient(API_HOST)
Expand Down Expand Up @@ -81,6 +84,7 @@ def get(
""",
)
def list(
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
"""
Expand All @@ -93,7 +97,8 @@ def list(
ValidationError: input fields are validated, if these are not suitable the exception is raised
HTTPError: request error to the API, 4XX or 5XX
"""

if json:
echo.set_log_file()
echo("Listing models ✅ ")
try:
client = ModelsClient(API_HOST)
Expand Down Expand Up @@ -138,6 +143,7 @@ def create(
..., "--name", "-n", help="Name of the model to be created"
),
description: str = DESCRIPTION_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
"""
Expand All @@ -151,6 +157,8 @@ def create(
ValidationError: input fields are validated, if these are not suitable the exception is raised
HTTPError: request error to the API, 4XX or 5XX
"""
if json:
echo.set_log_file()
if name is None or name == "":
echo.error("Name is required")
sys.exit(1)
Expand Down
13 changes: 13 additions & 0 deletions giza/cli/commands/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
DESCRIPTION_OPTION,
FRAMEWORK_OPTION,
INPUT_OPTION,
JSON_OPTION,
MODEL_OPTION,
OUTPUT_PATH_OPTION,
VERSION_OPTION,
Expand Down Expand Up @@ -49,8 +50,11 @@ def update_sierra(model_id: int, version_id: int, model_path: str):
def get(
model_id: int = MODEL_OPTION,
version_id: int = VERSION_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: bool = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
if any([model_id is None, version_id is None]):
echo.error("⛔️Model ID and version ID are required⛔️")
sys.exit(1)
Expand Down Expand Up @@ -79,6 +83,7 @@ def transpile(
"--download-sierra",
help="Download the siera file is the modle is fully compatible. CAIRO only.",
),
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if framework == Framework.CAIRO:
Expand All @@ -90,6 +95,7 @@ def transpile(
output_path=output_path,
download_model=download_model,
download_sierra=download_sierra,
json=json,
debug=debug,
)
elif framework == Framework.EZKL:
Expand Down Expand Up @@ -145,8 +151,12 @@ def update(
model_path: str = typer.Option(
None, "--model-path", "-M", help="Path of the model to update"
),
json: Optional[bool] = JSON_OPTION,
debug: bool = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()

if any([model_id is None, version_id is None]):
echo.error("⛔️Model ID and version ID are required to update the version⛔️")
sys.exit(1)
Expand Down Expand Up @@ -183,8 +193,11 @@ def update(
)
def list(
model_id: int = MODEL_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: bool = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
if model_id is None:
echo.error("⛔️Model ID is required⛔️")
sys.exit(1)
Expand Down
5 changes: 4 additions & 1 deletion giza/cli/frameworks/cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def transpile(
output_path: str,
download_model: bool,
download_sierra: bool,
json: Optional[bool],
debug: Optional[bool],
) -> None:
"""
Expand Down Expand Up @@ -256,7 +257,7 @@ def transpile(
ValidationError: If there is a validation error with the model or version.
HTTPError: If there is an HTTP error while communicating with the server.
"""
echo = Echo(debug=debug)
echo = Echo(debug=debug, output_json=json)
if model_path is None:
echo.error("No model name provided, please provide a model path ⛔️")
sys.exit(1)
Expand Down Expand Up @@ -400,6 +401,8 @@ def transpile(
if debug:
raise zip_error
sys.exit(1)
echo.print_model(model, title="Model")
echo.print_model(version, title="Version")


def verify(
Expand Down
2 changes: 1 addition & 1 deletion giza/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
)
NAME_OPTION = typer.Option(None, "--name", "-n", help="The name of the resource")
JSON_OPTION = typer.Option(
None,
False,
"--json",
"-j",
help="Whether to print the output as JSON. This will make that the only ouput is the json and the logs will be saved to `giza.log`",
Expand Down
5 changes: 3 additions & 2 deletions giza/cli/utils/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Echo:
LOG_FILE: str = "giza.log"

def __init__(
self, debug: Optional[bool] = False, output_json: bool = False
self, debug: Optional[bool] = False, output_json: bool | None = False
) -> None:
self._debug = debug
self._json = output_json
Expand Down Expand Up @@ -205,8 +205,9 @@ def print_model(self, model: Union[BaseModel, RootModel], title=""):
model (Union[BaseModel, RootModel]): The model or list of models to print
title (str, optional): Title of the table. Defaults to "".
"""
if self._json:
if self._json and self._file is not None:
print_json(model.model_dump_json())
self._file.write(model.model_dump_json(indent=4))
return

table = Table(title=title)
Expand Down

0 comments on commit ec42795

Please sign in to comment.