Skip to content

Commit

Permalink
Change logging.info with rich Panels in start/clean (#269)
Browse files Browse the repository at this point in the history
Co-Authored-By: Tommaso Caiazzi <tommasocaiazzi@gmail.com>
  • Loading branch information
Skazza94 and tcaiazzi committed Jan 30, 2024
1 parent 1c08ff5 commit 0d7d67f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
14 changes: 8 additions & 6 deletions src/Kathara/cli/command/LcleanCommand.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import argparse
import logging
from typing import List

from ..ui.utils import format_headers
from rich import print as rich_print

from ..ui.utils import create_panel
from ... import utils
from ...foundation.cli.command.Command import Command
from ...manager.Kathara import Kathara
Expand Down Expand Up @@ -52,8 +53,9 @@ def run(self, current_path: str, argv: List[str]) -> None:
except (Exception, IOError):
lab = Lab(None, path=lab_path)

logging.info(format_headers("Stopping Network Scenario"))
rich_print(create_panel("Stopping Network Scenario", style="blue bold", justify="center"))

Kathara.get_instance().undeploy_lab(lab_hash=lab.hash,
selected_machines=set(args['machine_names']) if args['machine_names']
else None)
Kathara.get_instance().undeploy_lab(
lab_hash=lab.hash,
selected_machines=set(args['machine_names']) if args['machine_names'] else None
)
18 changes: 10 additions & 8 deletions src/Kathara/cli/command/LstartCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import sys
from typing import List

from rich import print as rich_print

from ..ui.utils import create_panel
from ..ui.utils import create_table
from ..ui.utils import format_headers
from ... import utils
from ...exceptions import PrivilegeError, EmptyLabError
from ...foundation.cli.command.Command import Command
Expand Down Expand Up @@ -155,10 +157,12 @@ def run(self, current_path: str, argv: List[str]) -> Lab:
logging.warning("Running devices with privileged capabilities, terminals won't open!")
Setting.get_instance().open_terminals = False

if args['dry_mode']:
logging.info(format_headers("Checking Network Scenario"))
else:
logging.info(format_headers("Starting Network Scenario"))
rich_print(
create_panel(
"Checking Network Scenario" if args['dry_mode'] else "Starting Network Scenario",
style="blue bold", justify="center"
)
)

try:
lab = LabParser.parse(lab_path)
Expand All @@ -174,10 +178,8 @@ def run(self, current_path: str, argv: List[str]) -> Lab:
lab.apply_dependencies(dependencies)

lab_meta_information = str(lab)

if lab_meta_information:
logging.info("\n" + lab_meta_information)
logging.info(format_headers())
rich_print(create_panel(lab_meta_information))

if len(lab.machines) <= 0:
raise EmptyLabError()
Expand Down
9 changes: 6 additions & 3 deletions src/Kathara/cli/command/VcleanCommand.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import argparse
import logging
from typing import List

from rich import print as rich_print

from ..ui.utils import create_panel
from ...foundation.cli.command.Command import Command
from ...manager.Kathara import Kathara
from ...model.Lab import Lab
Expand Down Expand Up @@ -38,6 +40,7 @@ def run(self, current_path: str, argv: List[str]) -> None:
args = self.get_args()

lab = Lab("kathara_vlab")
Kathara.get_instance().undeploy_lab(lab_name=lab.name, selected_machines={args['name']})

logging.info("Device `%s` deleted successfully!" % args['name'])
rich_print(create_panel(f"Stopping Device `{args['name']}`", style="blue bold", justify="center"))

Kathara.get_instance().undeploy_lab(lab_name=lab.name, selected_machines={args['name']})
10 changes: 6 additions & 4 deletions src/Kathara/cli/command/VstartCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import sys
from typing import List

from ..ui.utils import format_headers, interface_cd_mac
from rich import print as rich_print

from ..ui.utils import create_panel, interface_cd_mac
from ... import utils
from ...exceptions import PrivilegeError
from ...foundation.cli.command.Command import Command
Expand Down Expand Up @@ -165,11 +167,13 @@ def run(self, current_path: str, argv: List[str]) -> None:
self.parse_args(argv)
args = self.get_args()

name = args.pop('name')

if args['dry_mode']:
logging.info("Device configuration is correct. Exiting...")
sys.exit(0)
else:
logging.info(format_headers("Starting Device"))
rich_print(create_panel(f"Starting Device `{name}`", style="blue bold", justify="center"))

Setting.get_instance().open_terminals = args['terminals'] if args['terminals'] is not None \
else Setting.get_instance().open_terminals
Expand All @@ -188,8 +192,6 @@ def run(self, current_path: str, argv: List[str]) -> None:
lab.add_option('shared_mount', False)
lab.add_option('privileged_machines', args['privileged'])

name = args.pop('name')

device = lab.get_or_new_machine(name, **args)

if args['eths']:
Expand Down
23 changes: 13 additions & 10 deletions src/Kathara/cli/ui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from typing import Any, Dict, Generator
from typing import Callable

from rich import box
from rich.panel import Panel
from rich.text import Text
from terminaltables import DoubleTable

from ... import utils
Expand All @@ -28,16 +31,16 @@ def confirmation_prompt(prompt_string: str, callback_yes: Callable, callback_no:
return callback_yes()


def format_headers(message: str = "") -> str:
footer = "=============================="
half_message = int((len(message) / 2) + 1)
second_half_message = half_message

if len(message) % 2 == 0:
second_half_message -= 1

message = " " + message + " " if message != "" else "=="
return footer[half_message:] + message + footer[second_half_message:]
def create_panel(message: str = "", **kwargs) -> Panel:
return Panel(
Text(
message,
style=kwargs['style'] if 'style' in kwargs else "none",
justify=kwargs['justify'] if 'justify' in kwargs else None,
),
title=kwargs['title'] if 'title' in kwargs else None,
title_align="center", box=box.SQUARE
)


def create_table(streams: Generator[Dict[str, IMachineStats], None, None]) -> \
Expand Down

0 comments on commit 0d7d67f

Please sign in to comment.