Unverified Commit cd205446 authored by Jakob Görgen's avatar Jakob Görgen
Browse files

symphony/cli: reduced code duplication by using generic table print method

parent b2696fc5
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
from typer import Typer, Option from typer import Typer, Option
from typing_extensions import Annotated from typing_extensions import Annotated
from simbricks.client.provider import client_provider from simbricks.client.provider import client_provider
from ..utils import async_cli, print_namespace_table from ..utils import async_cli, print_table_generic
app = Typer(help="SimBricks admin commands.") app = Typer(help="SimBricks admin commands.")
...@@ -34,7 +34,7 @@ async def ns_ls(): ...@@ -34,7 +34,7 @@ async def ns_ls():
"""List all available namespaces.""" """List all available namespaces."""
client = client_provider.admin_client client = client_provider.admin_client
namespaces = await client.get_all_ns() namespaces = await client.get_all_ns()
print_namespace_table(namespaces) print_table_generic("Namespaces", namespaces, "id", "name", "parent_id")
@app.command() @app.command()
...@@ -43,7 +43,7 @@ async def ns_ls_id(ident: int): ...@@ -43,7 +43,7 @@ async def ns_ls_id(ident: int):
"""List namespace with given id ident.""" """List namespace with given id ident."""
client = client_provider.admin_client client = client_provider.admin_client
namespace = await client.get_ns(ns_id=ident) namespace = await client.get_ns(ns_id=ident)
print_namespace_table([namespace]) print_table_generic("Namespace", [namespace], "id", "name", "parent_id")
@app.command() @app.command()
...@@ -52,9 +52,8 @@ async def ns_create(name: str, parent_id: Annotated[int, Option(help="optional p ...@@ -52,9 +52,8 @@ async def ns_create(name: str, parent_id: Annotated[int, Option(help="optional p
"""Create a new namespace.""" """Create a new namespace."""
client = client_provider.admin_client client = client_provider.admin_client
namespace = await client.create_ns(parent_id=parent_id, name=name) namespace = await client.create_ns(parent_id=parent_id, name=name)
ns_id = namespace["id"] print_table_generic("Namespace", [namespace], "id", "name", "parent_id")
print(f"Creating namespace {name} in {client_provider.namespace}. New namespace: {ns_id}")
@app.command() @app.command()
@async_cli() @async_cli()
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
from typer import Typer from typer import Typer
from simbricks.client.provider import client_provider from simbricks.client.provider import client_provider
from ..utils import async_cli from ..utils import async_cli
from ..utils import print_instantiations_table from ..utils import print_table_generic
app = Typer(help="Managing SimBricks Instantiations.") app = Typer(help="Managing SimBricks Instantiations.")
...@@ -33,7 +33,7 @@ app = Typer(help="Managing SimBricks Instantiations.") ...@@ -33,7 +33,7 @@ app = Typer(help="Managing SimBricks Instantiations.")
async def ls(): async def ls():
"""List Instantiations.""" """List Instantiations."""
insts = await client_provider.simbricks_client.get_instantiations() insts = await client_provider.simbricks_client.get_instantiations()
print_instantiations_table(insts) print_table_generic("Instantiations", insts, "id", "simulation_id")
@app.command() @app.command()
...@@ -41,7 +41,7 @@ async def ls(): ...@@ -41,7 +41,7 @@ async def ls():
async def show(inst_id: int): async def show(inst_id: int):
"""Show individual Instantiation.""" """Show individual Instantiation."""
inst = await client_provider.simbricks_client.get_instantiation(instantiation_id=inst_id) inst = await client_provider.simbricks_client.get_instantiation(instantiation_id=inst_id)
print_instantiations_table([inst]) print_table_generic("Instantiations", [inst], "id", "simulation_id")
@app.command() @app.command()
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
from typer import Typer from typer import Typer
from simbricks.client.provider import client_provider from simbricks.client.provider import client_provider
from ..utils import async_cli, print_namespace_table, print_members_table from ..utils import async_cli, print_table_generic, print_members_table
app = Typer(help="Managing SimBricks namespaces.") app = Typer(help="Managing SimBricks namespaces.")
...@@ -34,7 +34,7 @@ async def ls(): ...@@ -34,7 +34,7 @@ async def ls():
client = client_provider.ns_client client = client_provider.ns_client
namespaces = await client.get_all() namespaces = await client.get_all()
print_namespace_table(namespaces) print_table_generic("Namespaces", namespaces, "id", "name", "parent_id")
@app.command() @app.command()
...@@ -44,7 +44,7 @@ async def ls_id(ident: int): ...@@ -44,7 +44,7 @@ async def ls_id(ident: int):
client = client_provider.ns_client client = client_provider.ns_client
namespace = await client.get_ns(ident) namespace = await client.get_ns(ident)
print_namespace_table([namespace]) print_table_generic("Namespace", [namespace], "id", "name", "parent_id")
@app.command() @app.command()
...@@ -54,7 +54,7 @@ async def ls_cur(): ...@@ -54,7 +54,7 @@ async def ls_cur():
client = client_provider.ns_client client = client_provider.ns_client
namespace = await client.get_cur() namespace = await client.get_cur()
print_namespace_table([namespace]) print_table_generic("Namespace", [namespace], "id", "name", "parent_id")
@app.command() @app.command()
...@@ -70,9 +70,7 @@ async def create(name: str): ...@@ -70,9 +70,7 @@ async def create(name: str):
# create the actual namespace # create the actual namespace
namespace = await client.create(parent_id=cur_ns_id, name=name) namespace = await client.create(parent_id=cur_ns_id, name=name)
ns_id = namespace["id"] print_table_generic("Namespace", [namespace], "id", "name", "parent_id")
print(f"Creating namespace {name} in {client_provider.namespace}. New namespace: {ns_id}")
@app.command() @app.command()
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
from typer import Typer, Option from typer import Typer, Option
from typing_extensions import Annotated from typing_extensions import Annotated
from simbricks.client.provider import client_provider from simbricks.client.provider import client_provider
from ..utils import async_cli, print_runner_table, print_table_generic from ..utils import async_cli, print_table_generic
app = Typer(help="Managing SimBricks runners.") app = Typer(help="Managing SimBricks runners.")
...@@ -33,8 +33,8 @@ app = Typer(help="Managing SimBricks runners.") ...@@ -33,8 +33,8 @@ app = Typer(help="Managing SimBricks runners.")
@async_cli() @async_cli()
async def ls(): async def ls():
"""List runners.""" """List runners."""
runs = await client_provider.runner_client.list_runners() runners = await client_provider.runner_client.list_runners()
print_runner_table(runs) print_table_generic("Runners", runners, "id", "label", "tags")
@app.command() @app.command()
...@@ -42,7 +42,7 @@ async def ls(): ...@@ -42,7 +42,7 @@ async def ls():
async def show(runner_id: int): async def show(runner_id: int):
"""Show individual runner.""" """Show individual runner."""
runner = await client_provider.runner_client.get_runner(runner_id=runner_id) runner = await client_provider.runner_client.get_runner(runner_id=runner_id)
print_runner_table([runner]) print_table_generic("Runners", [runner], "id", "label", "tags")
@app.command() @app.command()
...@@ -57,7 +57,7 @@ async def delete(runner_id: int): ...@@ -57,7 +57,7 @@ async def delete(runner_id: int):
async def create(label: str, tags: list[str]): async def create(label: str, tags: list[str]):
"""Update a runner with the the given label and tags.""" """Update a runner with the the given label and tags."""
runner = await client_provider.runner_client.create_runner(label=label, tags=tags) runner = await client_provider.runner_client.create_runner(label=label, tags=tags)
print_runner_table([runner]) print_table_generic("Runner", [runner], "id", "label", "tags")
@app.command() @app.command()
...@@ -106,5 +106,7 @@ async def ls_events( ...@@ -106,5 +106,7 @@ async def ls_events(
limit: Annotated[int | None, Option("--limit", "-l", help="Limit results.")] = None, limit: Annotated[int | None, Option("--limit", "-l", help="Limit results.")] = None,
): ):
"""List runner related events""" """List runner related events"""
events = await client_provider.runner_client.get_events(action=action, run_id=run_id, event_status=event_status, limit=limit) events = await client_provider.runner_client.get_events(
action=action, run_id=run_id, event_status=event_status, limit=limit
)
print_table_generic("Events", events, "id", "runner_id", "action", "run_id", "event_status") print_table_generic("Events", events, "id", "runner_id", "action", "run_id", "event_status")
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
from typer import Typer from typer import Typer
from simbricks.client.provider import client_provider from simbricks.client.provider import client_provider
from ..utils import async_cli from ..utils import async_cli
from ..utils import print_simulations_table from ..utils import print_table_generic
app = Typer(help="Managing SimBricks Simulations.") app = Typer(help="Managing SimBricks Simulations.")
...@@ -33,7 +33,7 @@ app = Typer(help="Managing SimBricks Simulations.") ...@@ -33,7 +33,7 @@ app = Typer(help="Managing SimBricks Simulations.")
async def ls(): async def ls():
"""List Simulations.""" """List Simulations."""
simulations = await client_provider.simbricks_client.get_simulations() simulations = await client_provider.simbricks_client.get_simulations()
print_simulations_table(simulations) print_table_generic("Simulations", simulations, "id", "system_id")
@app.command() @app.command()
...@@ -41,7 +41,7 @@ async def ls(): ...@@ -41,7 +41,7 @@ async def ls():
async def show(sim_id: int): async def show(sim_id: int):
"""Show individual Simulation.""" """Show individual Simulation."""
sim = await client_provider.simbricks_client.get_simulation(simulation_id=sim_id) sim = await client_provider.simbricks_client.get_simulation(simulation_id=sim_id)
print_simulations_table([sim]) print_table_generic("Simulation", [sim], "id", "system_id")
@app.command() @app.command()
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
from typer import Typer from typer import Typer
from simbricks.client.provider import client_provider from simbricks.client.provider import client_provider
from ..utils import async_cli from ..utils import async_cli
from ..utils import print_systems_table from ..utils import print_table_generic
app = Typer(help="Managing SimBricks Systems.") app = Typer(help="Managing SimBricks Systems.")
...@@ -33,15 +33,15 @@ app = Typer(help="Managing SimBricks Systems.") ...@@ -33,15 +33,15 @@ app = Typer(help="Managing SimBricks Systems.")
async def ls(): async def ls():
"""List Systems.""" """List Systems."""
systems = await client_provider.simbricks_client.get_systems() systems = await client_provider.simbricks_client.get_systems()
print_systems_table(systems=systems) print_table_generic("Systems", systems, "id")
@app.command() @app.command()
@async_cli() @async_cli()
async def show(system_id: int): async def show(system_id: int):
"""Show individual System.""" """Show individual System."""
run = await client_provider.simbricks_client.get_system(system_id=system_id) system = await client_provider.simbricks_client.get_system(system_id=system_id)
print_systems_table([run]) print_table_generic("Systems", [system], "id")
@app.command() @app.command()
......
...@@ -41,12 +41,13 @@ def async_cli(): ...@@ -41,12 +41,13 @@ def async_cli():
return decorator_async_cli return decorator_async_cli
def print_table_generic(title: str, to_print, *args): def print_table_generic(title: str, to_print, *args):
table = Table(title=title) table = Table(title=title)
for key in args: for key in args:
table.add_column(key) table.add_column(key)
for val in to_print: for val in to_print:
row = [str(val[key]) for key in args] row = [str(val[key]) for key in args]
table.add_row(*row) table.add_row(*row)
...@@ -54,63 +55,6 @@ def print_table_generic(title: str, to_print, *args): ...@@ -54,63 +55,6 @@ def print_table_generic(title: str, to_print, *args):
console = Console() console = Console()
console.print(table) console.print(table)
def print_namespace_table(namespaces) -> None:
table = Table()
table.add_column("Id")
table.add_column("name")
table.add_column("parent")
for ns in namespaces:
table.add_row(str(ns["id"]), str(ns["name"]), str(ns["parent_id"]))
console = Console()
console.print(table)
def print_systems_table(systems):
table = Table()
table.add_column("Id")
for sys in systems:
table.add_row(str(sys["id"]))
console = Console()
console.print(table)
def print_simulations_table(instantiations):
table = Table()
table.add_column("Id")
table.add_column("SystemId")
for sys in instantiations:
table.add_row(str(sys["id"]), str(sys["system_id"]))
console = Console()
console.print(table)
def print_instantiations_table(instantiations):
table = Table()
table.add_column("Id")
table.add_column("SimulationId")
for sys in instantiations:
table.add_row(str(sys["id"]), str(sys["simulation_id"]))
console = Console()
console.print(table)
def print_runner_table(runners):
table = Table()
table.add_column("Id")
table.add_column("Label")
table.add_column("Tags")
for r in runners:
ts = []
for t in r["tags"]:
ts.append(t["label"])
table.add_row(str(r["id"]), str(r["label"]), ",".join(ts))
console = Console()
console.print(table)
def print_members_table(members: dict[str, list[dict]]): def print_members_table(members: dict[str, list[dict]]):
table = Table() table = Table()
...@@ -120,6 +64,6 @@ def print_members_table(members: dict[str, list[dict]]): ...@@ -120,6 +64,6 @@ def print_members_table(members: dict[str, list[dict]]):
table.add_column("Last") table.add_column("Last")
for r, ms in members.items(): for r, ms in members.items():
for m in ms: for m in ms:
table.add_row(r, m['username'], m['first_name'], m['last_name']) table.add_row(r, m["username"], m["first_name"], m["last_name"])
console = Console() console = Console()
console.print(table) console.print(table)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment