Skip to content

Commit 227787c

Browse files
committed
New functionality implemented for the info command.
1 parent 0e6449d commit 227787c

File tree

6 files changed

+628
-237
lines changed

6 files changed

+628
-237
lines changed

dem/cli/command/info_cmd.py

+115-17
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,147 @@
66
from dem.cli.console import stdout, stderr
77
from dem.core.platform import Platform
88
from rich.table import Table
9+
import typer
910

1011
image_status_messages = {
11-
ToolImage.NOT_AVAILABLE: "[red]Error: Required image is not available![/]",
12-
ToolImage.LOCAL_ONLY: "Image is available locally.",
13-
ToolImage.REGISTRY_ONLY: "Image is available in the registry.",
14-
ToolImage.LOCAL_AND_REGISTRY: "Image is available locally and in the registry.",
12+
ToolImage.NOT_AVAILABLE: "[red]Error: not available![/]",
13+
ToolImage.LOCAL_ONLY: "Local",
14+
ToolImage.REGISTRY_ONLY: "Registry",
15+
ToolImage.LOCAL_AND_REGISTRY: "Local and Registry",
1516
}
1617

17-
def print_info(dev_env: DevEnv) -> None:
18-
""" Print information about the given Development Environment.
18+
def print_local_dev_env_info(dev_env: DevEnv) -> None:
19+
""" Print information about the given local Development Environment.
1920
2021
Args:
2122
dev_env -- the Development Environment to print information about
2223
"""
2324
tool_info_table = Table()
2425
tool_info_table.add_column("Image")
25-
tool_info_table.add_column("Status")
26+
tool_info_table.add_column("Availability")
2627

27-
for tool_image in dev_env.tool_images:
28+
for tool_image in sorted(dev_env.tool_images, key=lambda x: x.name):
2829
tool_info_table.add_row(tool_image.name,
2930
image_status_messages[tool_image.availability])
31+
if dev_env.is_installed:
32+
installation_status = "[green]Installed[/]"
33+
else:
34+
installation_status = "Not installed"
35+
stdout.print(f"\n[bold]Development Environment: {dev_env.name}[/]\n")
36+
stdout.print(f"Installation state: {installation_status}\n")
3037
stdout.print(tool_info_table)
3138

32-
def execute(platform: Platform, arg_dev_env_name: str) -> None:
33-
""" Print information about the given Development Environment.
39+
if dev_env.is_installed and dev_env.get_tool_image_status() == DevEnv.Status.REINSTALL_NEEDED:
40+
stderr.print("\n[red]Error: Incomplete local install! The Dev Env must be reinstalled![/]")
41+
42+
if dev_env.get_tool_image_status() == DevEnv.Status.UNAVAILABLE_IMAGE:
43+
stderr.print("\n[red]Error: Required image could not be found either locally or in the registry![/]")
44+
45+
def local_info(platform: Platform, dev_env_name: str) -> None:
46+
""" Gather and print information about the given local Development Environment.
3447
3548
Args:
3649
platform -- the platform
37-
arg_dev_env_name -- the name of the Development Environment to print information about
50+
dev_env_name -- the name of the Development Environment to print information about
51+
52+
Raises:
53+
typer.Abort -- if the Development Environment is unknown
3854
"""
3955
platform.assign_tool_image_instances_to_all_dev_envs()
4056

41-
dev_env = platform.get_dev_env_by_name(arg_dev_env_name)
57+
dev_env = platform.get_dev_env_by_name(dev_env_name)
4258

4359
if dev_env is None:
44-
for catalog in platform.dev_env_catalogs.catalogs:
60+
stderr.print(f"[red]Error: Unknown Development Environment: {dev_env_name}[/]\n")
61+
raise typer.Abort()
62+
63+
print_local_dev_env_info(dev_env)
64+
65+
def print_cat_dev_env_info(dev_env: DevEnv, cat_name: str) -> None:
66+
""" Print information about the given catalog Development Environment.
67+
68+
Args:
69+
dev_env -- the Development Environment to print information about
70+
cat_name -- the name of the catalog the Development Environment belongs to
71+
"""
72+
tool_info_table = Table()
73+
tool_info_table.add_column("Image")
74+
tool_info_table.add_column("Availability")
75+
76+
for tool_image in sorted(dev_env.tool_images, key=lambda x: x.name):
77+
tool_info_table.add_row(tool_image.name,
78+
image_status_messages[tool_image.availability])
79+
stdout.print(f"\n[bold]Development Environment: {dev_env.name}[/]\n")
80+
stdout.print(f"Catalog: {cat_name}\n")
81+
stdout.print(tool_info_table)
82+
83+
if dev_env.get_tool_image_status() == DevEnv.Status.UNAVAILABLE_IMAGE:
84+
stderr.print("\n[red]Error: Required image could not be found in the registry![/]")
85+
86+
def cat_dev_env_info(platform: Platform, dev_env_name: str, selected_cats: list[str]) -> None:
87+
""" Gather and print information about the given catalog Development Environment.
88+
89+
Args:
90+
platform -- the platform
91+
dev_env_name -- the name of the Development Environment to print information about
92+
selected_cats -- the selected catalog names, empty list means all catalogs
93+
"""
94+
for catalog in platform.dev_env_catalogs.catalogs:
95+
if catalog.name in selected_cats or not selected_cats:
4596
catalog.request_dev_envs()
46-
dev_env = catalog.get_dev_env_by_name(arg_dev_env_name)
97+
dev_env = catalog.get_dev_env_by_name(dev_env_name)
4798
if dev_env:
4899
dev_env.assign_tool_image_instances(platform.tool_images)
100+
print_cat_dev_env_info(dev_env, catalog.name)
49101
break
102+
else:
103+
stderr.print(f"[red]Error: Unknown Development Environment: {dev_env_name}[/]\n")
50104

51-
if dev_env is None:
52-
stderr.print(f"[red]Error: Unknown Development Environment: {arg_dev_env_name}[/]\n")
105+
def selected_cats_info(platform: Platform, dev_env_name: str, selected_cats: list[str]) -> None:
106+
""" Print information about the given Development Environment from the selected catalogs.
107+
108+
Args:
109+
platform -- the platform
110+
dev_env_name -- the name of the Development Environment to print information about
111+
selected_cats -- the selected catalog names
112+
113+
Raises:
114+
typer.Abort -- if the selected catalog is unknown
115+
"""
116+
available_cats = set([cat.name for cat in platform.dev_env_catalogs.catalogs])
117+
selected_cats = set(selected_cats)
118+
119+
if not selected_cats.issubset(available_cats):
120+
stderr.print(f"[red]Error: Unknown catalog(s): {', '.join(selected_cats - available_cats)}[/]\n")
121+
raise typer.Abort()
122+
123+
cat_dev_env_info(platform, dev_env_name, selected_cats)
124+
125+
def all_cats_info(platform: Platform, dev_env_name: str) -> None:
126+
""" Print information about the given Development Environment from all catalogs.
127+
128+
Args:
129+
platform -- the platform
130+
dev_env_name -- the name of the Development Environment to print information about
131+
"""
132+
cat_dev_env_info(platform, dev_env_name, [])
133+
134+
def execute(platform: Platform, arg_dev_env_name: str, cat: bool, selected_cats: list[str]) -> None:
135+
""" Print information about the given Development Environment.
136+
137+
Args:
138+
platform -- the platform
139+
arg_dev_env_name -- the name of the Development Environment to print information about
140+
cat -- the flag to print information about the Development Environment from the catalogs
141+
selected_cats -- the selected catalog names
142+
143+
Raises:
144+
typer.Abort -- if the Development Environment is unknown or if the selected catalog is
145+
unknown
146+
"""
147+
if not cat:
148+
local_info(platform, arg_dev_env_name)
149+
elif selected_cats:
150+
selected_cats_info(platform, arg_dev_env_name, selected_cats)
53151
else:
54-
print_info(dev_env)
152+
all_cats_info(platform, arg_dev_env_name)

dem/cli/command/list_tools_cmd.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ def list_local_tools(platform: Platform) -> None:
2929
# tool_image[0] is the name of the tool image and tool_image[1] is the ToolImage instance
3030
stdout.print(f" {tool_image[0]}")
3131

32-
def update_tools_from_selected_regs(platform: Platform, specified_regs: list[str]) -> None:
32+
def update_tools_from_selected_regs(platform: Platform, selected_regs: list[str]) -> None:
3333
""" Update the tools from the selected registries only.
3434
3535
Args:
3636
platform -- the Platform
37-
specified_regs -- the selected registry names
37+
selected_regs -- the selected registry names
3838
3939
Exceptions:
4040
typer.Abort -- if an unknown registry is specified
@@ -44,14 +44,14 @@ def update_tools_from_selected_regs(platform: Platform, specified_regs: list[str
4444
platform.disable_tool_update = True
4545

4646
available_regs = set([reg["name"] for reg in platform.config_file.registries])
47-
specified_regs = set(specified_regs)
47+
selected_regs = set(selected_regs)
4848

49-
if not specified_regs.issubset(available_regs):
50-
for unkown_reg in specified_regs - available_regs:
49+
if not selected_regs.issubset(available_regs):
50+
for unkown_reg in selected_regs - available_regs:
5151
stderr.print(f"[red]Error: Registry {unkown_reg} is not available![/]")
5252
raise typer.Abort()
5353

54-
platform.tool_images.update(reg_selection=specified_regs)
54+
platform.tool_images.update(reg_selection=selected_regs)
5555

5656
def list_tools_from_regs(platform: Platform, table: Table) -> None:
5757
""" List the available tools from the registries.

dem/cli/main.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,23 @@ def list_tools(reg: Annotated[bool, typer.Option(help="List the available tools
112112
else:
113113
raise InternalError("Error: The platform hasn't been initialized properly!")
114114

115-
@typer_cli.command()
115+
@typer_cli.command(context_settings={"allow_extra_args": True})
116116
def info(dev_env_name: Annotated[str, typer.Argument(help="Name of the Development Environment to get info about.",
117-
autocompletion=autocomplete_dev_env_name)]) -> None:
117+
autocompletion=autocomplete_dev_env_name)],
118+
cat: Annotated[bool, typer.Option(help="Get the Dev Env from the catalogs")] = False,
119+
ctx: Annotated[typer.Context, typer.Option()] = None) -> None:
118120
"""
119121
Get information about the specified Development Environment available locally or in the catalogs.
120122
123+
--cat: DEM will search for the Dev Env in the catalogs and will print the details of the first
124+
match. You can specifiy the catalogs' name to search in after this option. If no catalog is
125+
specified, all the available catalogs will be used. If the Dev Env is not found in the catalogs,
126+
an error message will be printed.
127+
121128
Note: Autocomplete only works with the locally avialable Dev Envs.
122129
"""
123-
if platform:
124-
info_cmd.execute(platform, dev_env_name)
130+
if platform and ctx:
131+
info_cmd.execute(platform, dev_env_name, cat, ctx.args)
125132
else:
126133
raise InternalError("Error: The platform hasn't been initialized properly!")
127134

dem/core/dev_env.py

-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ def assign_tool_image_instances(self, tool_images: ToolImages) -> None:
7171
def get_tool_image_status(self) -> Status:
7272
""" Get the status of the Tool Images.
7373
74-
Can only be used if the Dev Env is insalled.
75-
7674
This method checks the availability of the assigned Tool Images.
7775
If at least one of the Tool Images is unkonwn: NOT_AVAILABLE.
7876
If at least one of the Tool Images is only available in the registry: REINSTALL_NEEDED.

0 commit comments

Comments
 (0)