Skip to content

Commit

Permalink
reafactor: remove print_table and print_object utils
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulFarault committed Dec 3, 2024
1 parent 614736b commit 7323656
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
36 changes: 27 additions & 9 deletions tdp/cli/commands/browse.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# Copyright 2022 TOSIT.IO
# SPDX-License-Identifier: Apache-2.0

from collections.abc import Iterable
from typing import Optional

import click
from sqlalchemy import Engine
from tabulate import tabulate

from tdp.cli.params import database_dsn_option
from tdp.cli.utils import (
print_deployment,
print_object,
print_operations,
print_table,
)
from tdp.core.entities.operation import OperationName
from tdp.core.models.deployment_model import DeploymentModel
from tdp.core.models.operation_model import OperationModel
from tdp.dao import Dao


Expand Down Expand Up @@ -141,10 +143,7 @@ def browse_operation(dao: Dao, deployment_id: int, operation: str) -> None:
click.echo("\nUse the operation order to print a specific operation.")
return
if record:
print_object(record.to_dict(filter_out=["logs"]))
if record.logs:
click.secho("\nLogs", bold=True)
click.echo(str(record.logs, "utf-8"))
_print_operation(record)
else:
click.echo(f'Operation "{operation}" not found for deployment {deployment_id}')
click.echo(
Expand All @@ -156,9 +155,28 @@ def browse_operation(dao: Dao, deployment_id: int, operation: str) -> None:
def browse_deployments(dao: Dao, limit: int, offset: int) -> None:
deployments = dao.get_last_deployments(limit=limit, offset=offset)
if len(deployments) > 0:
print_table(
[d.to_dict(filter_out=["options"]) for d in deployments],
)
_print_deployments(deployments)
else:
click.echo("No deployments found.")
click.echo("Create a deployment plan using the `tdp plan` command.")


def _print_operation(operation: OperationModel) -> None:
click.echo(
tabulate(
operation.to_dict(filter_out=["logs"]).items(),
tablefmt="plain",
)
)
if operation.logs:
click.secho("\nLogs", bold=True)
click.echo(str(operation.logs, "utf-8"))


def _print_deployments(deployments: Iterable[DeploymentModel]) -> None:
click.echo(
tabulate(
[d.to_dict(filter_out=["options"]) for d in deployments],
headers="keys",
)
)
9 changes: 6 additions & 3 deletions tdp/cli/commands/status/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import click
from sqlalchemy import Engine
from tabulate import tabulate

from tdp.cli.params import (
collections_option,
Expand All @@ -22,7 +23,6 @@
from tdp.cli.utils import (
check_services_cleanliness,
print_hosted_entity_status_log,
print_table,
)
from tdp.core.models.sch_status_log_model import SCHStatusLogModel
from tdp.core.variables import ClusterVariables
Expand Down Expand Up @@ -119,6 +119,9 @@ def show(


def _print_sch_status_logs(sch_status: Iterable[SCHStatusLogModel]) -> None:
print_table(
[status.to_dict(filter_out=["id", "timestamp"]) for status in sch_status],
click.echo(
tabulate(
[status.to_dict(filter_out=["id", "timestamp"]) for status in sch_status],
headers="keys",
)
)
35 changes: 10 additions & 25 deletions tdp/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ def print_deployment(
) -> None:
# Print general deployment infos
click.secho("Deployment details", bold=True)
click.echo(print_object(deployment.to_dict(filter_out=filter_out)))
click.echo(
tabulate(
deployment.to_dict(filter_out=filter_out).items(),
tablefmt="plain",
)
)

# Print deployment operations
click.secho("\nOperations", bold=True)
Expand All @@ -63,43 +68,23 @@ def print_operations(
Args:
operations: List of operations to print.
"""
print_table(
[o.to_dict(filter_out=filter_out) for o in operations],
)


def print_object(obj: dict) -> None:
"""Print an object in a human readable format.
Args:
obj: Object to print.
"""
click.echo(
tabulate(
obj.items(),
tablefmt="plain",
[o.to_dict(filter_out=filter_out) for o in operations],
headers="keys",
)
)


def print_table(rows) -> None:
"""Print a list of rows in a human readable format.
Args:
rows: List of rows to print.
"""
def print_hosted_entity_status_log(sch_status: Iterable[HostedEntityStatus]) -> None:
click.echo(
tabulate(
rows,
[status.export_tabulate() for status in sch_status],
headers="keys",
)
)


def print_hosted_entity_status_log(sch_status: Iterable[HostedEntityStatus]) -> None:
print_table([status.export_tabulate() for status in sch_status])


def _parse_line(line: str) -> tuple[str, Optional[str], Optional[list[str]]]:
"""Parses a line which contains an operation, and eventually a host and extra vars.
Expand Down

0 comments on commit 7323656

Please sign in to comment.