Skip to content

Commit

Permalink
Replace typer.echo with logging.info
Browse files Browse the repository at this point in the history
  • Loading branch information
Navarro-Jonathan committed Nov 8, 2023
1 parent 29d3e17 commit 2fbbef1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 44 deletions.
7 changes: 4 additions & 3 deletions src/aerie_cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
get_active_session_client,
)
from aerie_cli.utils.configurations import find_configuration
from aerie_cli.utils.logger import console_logger

app = typer.Typer()
app.add_typer(plans.plans_app, name="plans")
Expand Down Expand Up @@ -138,14 +139,14 @@ def change_role(
client = get_active_session_client()

if role is None:
typer.echo(f"Active Role: {client.aerie_host.active_role}")
logging.info(f"Active Role: {client.aerie_host.active_role}")
role = select_from_list(client.aerie_host.aerie_jwt.allowed_roles)

client.aerie_host.change_role(role)

PersistentSessionManager.set_active_session(client.aerie_host)

typer.echo(f"Changed role to: {client.aerie_host.active_role}")
logging.info(f"Changed role to: {client.aerie_host.active_role}")


@app.command("status")
Expand All @@ -159,4 +160,4 @@ def print_status():
if client.aerie_host.configuration_name:
logging.info(f"Active configuration: {client.aerie_host.configuration_name}")

typer.echo(f"Active role: {client.aerie_host.active_role}")
logging.info(f"Active role: {client.aerie_host.active_role}")
4 changes: 2 additions & 2 deletions src/aerie_cli/commands/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import typer
import logging
import json
from pathlib import Path
from rich.console import Console
Expand Down Expand Up @@ -98,8 +99,7 @@ def list_configurations():
except NoActiveSessionError:
active_config = None

typer.echo(f"Configuration file location: {CONFIGURATION_FILE_PATH}")
typer.echo()
logging.info(f"Configuration file location: {CONFIGURATION_FILE_PATH}")

table = Table(title='Aerie Host Configurations',
caption='Active configuration in red')
Expand Down
9 changes: 5 additions & 4 deletions src/aerie_cli/commands/constraints.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

import arrow
import logging
import typer
from rich.console import Console
from rich.table import Table
Expand Down Expand Up @@ -34,7 +35,7 @@ def upload(
"definition": str_contents
}
constraint_id = client.upload_constraint(constraint)
typer.echo(f"Created constraint: {constraint_id}")
logging.info(f"Created constraint: {constraint_id}")

@app.command()
def delete(
Expand All @@ -44,7 +45,7 @@ def delete(

client = CommandContext.get_client()
client.delete_constraint(id)
typer.echo(f"Successfully deleted constraint {id}")
logging.info(f"Successfully deleted constraint {id}")

@app.command()
def update(
Expand All @@ -66,7 +67,7 @@ def update(
"definition": str_contents
}
constraint_id = client.update_constraint(id, constraint)
typer.echo(f"Updated constraint: {constraint_id}")
logging.info(f"Updated constraint: {constraint_id}")

@app.command()
def violations(
Expand All @@ -75,7 +76,7 @@ def violations(

client = CommandContext.get_client()
constraint_violations = client.get_constraint_violations(plan_id)
typer.echo(f"Constraint violations: {constraint_violations}")
logging.info(f"Constraint violations: {constraint_violations}")



7 changes: 4 additions & 3 deletions src/aerie_cli/commands/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import arrow
import typer
import logging
from rich.console import Console
from rich.table import Table

Expand Down Expand Up @@ -30,7 +31,7 @@ def upload(
contents = in_file.read()
schema_data = json.loads(contents)
result = client.add_directive_metadata_schemas(schema_data["schemas"])
typer.echo(f"{len(schema_data['schemas'])} new schema have been added.")
logging.info(f"{len(schema_data['schemas'])} new schema have been added.")


@app.command()
Expand All @@ -41,7 +42,7 @@ def delete(
):
"""Delete a metadata schema by its name."""
resp = CommandContext.get_client().delete_directive_metadata_schema(schema_name)
typer.echo(f"Schema `{resp}` has been removed.")
logging.info(f"Schema `{resp}` has been removed.")


@app.command()
Expand Down Expand Up @@ -72,4 +73,4 @@ def clean():
for schema in resp:
client.delete_directive_metadata_schema(schema["key"])

typer.echo(f"All metadata schemas have been deleted")
logging.info(f"All metadata schemas have been deleted")
9 changes: 5 additions & 4 deletions src/aerie_cli/commands/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

import arrow
import logging
import typer
from rich.console import Console
from rich.table import Table
Expand Down Expand Up @@ -63,9 +64,9 @@ def upload(

# Attach sim template to model
client.upload_sim_template(model_id=model_id, args=json_obj, name=name)
typer.echo(f"Attached simulation template to model {model_id}.")
logging.info(f"Attached simulation template to model {model_id}.")

typer.echo(f"Created new mission model: {model_name} with Model ID: {model_id}")
logging.info(f"Created new mission model: {model_name} with Model ID: {model_id}")


@app.command()
Expand All @@ -77,7 +78,7 @@ def delete(
"""Delete a mission model by its model id."""

model_name = CommandContext.get_client().delete_mission_model(model_id)
typer.echo(f"Mission Model `{model_name}` with ID: {model_id} has been removed.")
logging.info(f"Mission Model `{model_name}` with ID: {model_id} has been removed.")


@app.command()
Expand All @@ -89,7 +90,7 @@ def clean():
for api_mission_model in resp:
client.delete_mission_model(api_mission_model.id)

typer.echo(f"All mission models have been deleted")
logging.info(f"All mission models have been deleted")


@app.command()
Expand Down
41 changes: 21 additions & 20 deletions src/aerie_cli/commands/plans.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import arrow
import pandas as pd
import typer
import logging
from rich.console import Console
from rich.table import Table

Expand All @@ -29,7 +30,7 @@ def download(
plan = CommandContext.get_client().get_activity_plan_by_id(id, full_args)
with open(output, "w") as out_file:
out_file.write(plan.to_json(indent=2))
typer.echo(f"Wrote activity plan to {output}")
logging.info(f"Wrote activity plan to {output}")


@plans_app.command()
Expand All @@ -48,7 +49,7 @@ def download_simulation(
simulated_activities = client.get_simulation_results(sim_id)
with open(output, "w") as out_file:
out_file.write(json.dumps(simulated_activities, indent=2))
typer.echo(f"Wrote activity plan to {output}")
logging.info(f"Wrote activity plan to {output}")


@plans_app.command()
Expand Down Expand Up @@ -141,7 +142,7 @@ def download_resources(
# write to file
with open(output, "w") as out_file:
df.to_csv(out_file, index=False, header=field_name)
typer.echo(f"Wrote resource timelines to {output}")
logging.info(f"Wrote resource timelines to {output}")

else:
if absolute_time:
Expand All @@ -157,7 +158,7 @@ def download_resources(
# write to file
with open(output, "w") as out_file:
out_file.write(json.dumps(resources, indent=2))
typer.echo(f"Wrote resource timelines to {output}")
logging.info(f"Wrote resource timelines to {output}")


@plans_app.command()
Expand All @@ -179,7 +180,7 @@ def upload(
if time_tag:
plan_to_create.name += arrow.utcnow().format("YYYY-MM-DDTHH-mm-ss")
plan_id = client.create_activity_plan(model_id, plan_to_create)
typer.echo(f"Created plan ID: {plan_id}")
logging.info(f"Created plan ID: {plan_id}")


@plans_app.command()
Expand All @@ -196,7 +197,7 @@ def duplicate(
plan_to_duplicate = ActivityPlanCreate.from_plan_read(plan)
plan_to_duplicate.name = duplicated_plan_name
duplicated_plan_id = client.create_activity_plan(plan.model_id, plan_to_duplicate)
typer.echo(f"Duplicate activity plan created with ID: {duplicated_plan_id}")
logging.info(f"Duplicate activity plan created with ID: {duplicated_plan_id}")


@plans_app.command()
Expand All @@ -218,12 +219,12 @@ def simulate(
end_time = arrow.utcnow()
res = client.get_simulation_results(sim_dataset_id)
total_sim_time = end_time - start_time
typer.echo(f"Simulation completed in " + str(total_sim_time))
logging.info(f"Simulation completed in " + str(total_sim_time))

if output:
with open(output, "w") as out_file:
out_file.write(json.dumps(res, indent=2))
typer.echo(f"Wrote simulation results to {output}")
logging.info(f"Wrote simulation results to {output}")


@plans_app.command()
Expand Down Expand Up @@ -273,9 +274,9 @@ def create_config(

resp = CommandContext.get_client().create_config_args(plan_id=plan_id, args=json_obj)

typer.echo(f"Configuration Arguments for Plan ID: {plan_id}")
logging.info(f"Configuration Arguments for Plan ID: {plan_id}")
for arg in resp:
typer.echo(f"(*) {arg}: {resp[arg]}")
logging.info(f"(*) {arg}: {resp[arg]}")


@plans_app.command()
Expand All @@ -292,9 +293,9 @@ def update_config(

resp = CommandContext.get_client().update_config_args(plan_id=plan_id, args=json_obj)

typer.echo(f"Configuration Arguments for Plan ID: {plan_id}")
logging.info(f"Configuration Arguments for Plan ID: {plan_id}")
for arg in resp:
typer.echo(f"(*) {arg}: {resp[arg]}")
logging.info(f"(*) {arg}: {resp[arg]}")


@plans_app.command()
Expand All @@ -304,7 +305,7 @@ def delete(
"""Delete an activity plan by its id."""

plan_name = CommandContext.get_client().delete_plan(plan_id)
typer.echo(f"Plan `{plan_name}` with ID: {plan_id} has been removed.")
logging.info(f"Plan `{plan_name}` with ID: {plan_id} has been removed.")


@plans_app.command()
Expand All @@ -316,7 +317,7 @@ def clean():
for activity_plan in resp:
client.delete_plan(activity_plan.id)

typer.echo(f"All activity plans have been deleted")
logging.info(f"All activity plans have been deleted")

@collaborators_app.command("list")
def list_collaborators(
Expand All @@ -328,9 +329,9 @@ def list_collaborators(

collaborators = client.list_plan_collaborators(plan_id)
if len(collaborators):
typer.echo("\n".join(collaborators))
logging.info("\n".join(collaborators))
else:
typer.echo("No collaborators")
logging.info("No collaborators")


@collaborators_app.command("add")
Expand All @@ -346,9 +347,9 @@ def add_collaborator(

client.add_plan_collaborator(plan_id, user)
if user in client.list_plan_collaborators(plan_id):
typer.echo(f"Successfully added collaborator: {user}")
logging.info(f"Successfully added collaborator: {user}")
else:
typer.echo(f"Failed to add collaborator")
logging.info(f"Failed to add collaborator")


@collaborators_app.command("delete")
Expand All @@ -368,6 +369,6 @@ def delete_collaborator(
client.delete_plan_collaborator(plan_id, user)

if user not in client.list_plan_collaborators(plan_id):
typer.echo("Successfully deleted collaborator")
logging.info("Successfully deleted collaborator")
else:
typer.echo("Failed to delete collaborator")
logging.info("Failed to delete collaborator")
14 changes: 7 additions & 7 deletions src/aerie_cli/commands/scheduling.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import typer
import logging

from aerie_cli.commands.command_context import CommandContext

Expand Down Expand Up @@ -31,7 +32,7 @@ def upload(

resp = client.upload_scheduling_goals(upload_obj)

typer.echo(f"Uploaded scheduling goals to venue.")
logging.info(f"Uploaded scheduling goals to venue.")

uploaded_ids = [kv["id"] for kv in resp]

Expand All @@ -43,7 +44,7 @@ def upload(

client.add_goals_to_specifications(upload_to_spec)

typer.echo(f"Assigned goals in priority order to plan ID {plan_id}.")
logging.info(f"Assigned goals in priority order to plan ID {plan_id}.")


@app.command()
Expand All @@ -56,7 +57,7 @@ def delete(
client = CommandContext.get_client()

resp = client.delete_scheduling_goal(goal_id)
typer.echo("Successfully deleted Goal ID: " + str(resp))
logging.info("Successfully deleted Goal ID: " + str(resp))

@app.command()
def delete_all_goals_for_plan(
Expand All @@ -71,14 +72,13 @@ def delete_all_goals_for_plan(
clear_goals = client.get_scheduling_goals_by_specification(specification) #response is in asc order

if len(clear_goals) == 0: #no goals to clear
typer.echo("No goals to delete.")
logging.info("No goals to delete.")
return

typer.echo("Deleting goals for Plan ID {plan}: ".format(plan=plan_id), nl=False)
logging.info("Deleting goals for Plan ID {plan}: ".format(plan=plan_id), nl=False)
goal_ids = []
for goal in clear_goals:
goal_ids.append(goal["goal"]["id"])
typer.echo(str(goal["goal"]["id"]) + " ", nl=False)
typer.echo()
logging.info(str(goal["goal"]["id"]) + " ", nl=False)

client.delete_scheduling_goals(goal_ids)
5 changes: 4 additions & 1 deletion src/aerie_cli/utils/prompts.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from typing import List

import typer

import logging

def select_from_list(options: List[str], prompt: str = 'Select an option'):
while True:
for i, c in enumerate(options):
print(f"\t{i+1}) {c}")
choice_id = typer.prompt(prompt)
logging.debug(f"Prompt: {prompt}")
logging.debug(f"Options: {options}")
logging.debug(f"Selected {choice_id}")
try:
return options[int(choice_id)-1]
except (KeyError, ValueError):
Expand Down

0 comments on commit 2fbbef1

Please sign in to comment.