From 7c9991672c6810e66475e005b53d7afad26b55ed Mon Sep 17 00:00:00 2001 From: Daniel Azanov Date: Tue, 12 Dec 2023 15:21:54 +0100 Subject: [PATCH] show ws names for rtw ws and rtw workspace use --- rtwcli/rtw_cmds/rtw_cmds/aliases.py | 7 ++- rtwcli/rtw_cmds/rtw_cmds/workspace/verbs.py | 62 ++++++++++++--------- rtwcli/rtwcli/setup.py | 2 +- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/rtwcli/rtw_cmds/rtw_cmds/aliases.py b/rtwcli/rtw_cmds/rtw_cmds/aliases.py index 13e55e41..dbcf622c 100644 --- a/rtwcli/rtw_cmds/rtw_cmds/aliases.py +++ b/rtwcli/rtw_cmds/rtw_cmds/aliases.py @@ -12,8 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import argparse from rtwcli.command import CommandExtension -from rtw_cmds.workspace.verbs import UseVerb +from rtw_cmds.workspace.verbs import UseVerb, add_cli_workspace_arg class WSAlias(CommandExtension): @@ -22,8 +23,8 @@ class WSAlias(CommandExtension): def __init__(self): super().__init__() - def add_arguments(self, parser, cli_name): - pass + def add_arguments(self, parser: argparse.ArgumentParser, cli_name: str): + add_cli_workspace_arg(parser) def main(self, *, parser, args): use_verb = UseVerb() diff --git a/rtwcli/rtw_cmds/rtw_cmds/workspace/verbs.py b/rtwcli/rtw_cmds/rtw_cmds/workspace/verbs.py index 27488ce4..2542b4cc 100644 --- a/rtwcli/rtw_cmds/rtw_cmds/workspace/verbs.py +++ b/rtwcli/rtw_cmds/rtw_cmds/workspace/verbs.py @@ -38,7 +38,7 @@ - /home/daniela/workspace/mojin/pid_ws -> hdw_mojin__pid_ws """ -import copy +import argparse import dataclasses import datetime import os @@ -333,35 +333,46 @@ def main(self, *, args): print("Not implemented yet") +def get_workspace_names() -> List[str]: + """Retrieve a list of workspace names from the YAML file.""" + if not os.path.isfile(WORKSPACES_PATH): + return [] + workspaces_config = load_workspaces_config_from_yaml_file(WORKSPACES_PATH) + return workspaces_config.get_ws_names() + + +def workspace_name_completer(**kwargs) -> List[str]: + """Callable returning a list of workspace names.""" + ws_names = get_workspace_names() + if not ws_names: + return ["NO_WORKSPACES_FOUND"] + return ws_names + + +def add_cli_workspace_arg(parser: argparse.ArgumentParser): + arg = parser.add_argument( + "workspace_name", + help="The workspace name", + ) + arg.completer = workspace_name_completer + + class UseVerb(VerbExtension): """Select and source an existing ROS workspace.""" + def add_arguments(self, parser: argparse.ArgumentParser, cli_name: str): + add_cli_workspace_arg(parser) + def main(self, *, args): - if not os.path.isfile(WORKSPACES_PATH): - print( - "No workspaces are available as the workspaces config file " - f"'{WORKSPACES_PATH}' does not exist" - ) - return + ws_name = args.workspace_name + if not ws_name: + return "No workspace name provided." workspaces_config = load_workspaces_config_from_yaml_file(WORKSPACES_PATH) - if not workspaces_config.workspaces: - print(f"No workspaces found in config file '{WORKSPACES_PATH}'") - return - - ws_names = workspaces_config.get_ws_names() - ws_name = questionary.autocomplete( - "Choose workspace", - ws_names, - qmark="'Tab' to see all workspaces\n", - meta_information=copy.deepcopy(workspaces_config.to_dict()[WORKSPACES_KEY]), - validate=lambda ws_choice: ws_choice in ws_names, - style=questionary.Style([("answer", "bg:ansiwhite")]), - ).ask() - if not ws_name: # Cancelled by user - return + workspace = workspaces_config.workspaces.get(ws_name) + if not workspace: + return f"Workspace '{ws_name}' not found." - workspace = workspaces_config.workspaces[ws_name] print(f"Workspace data: {workspace}") script_content = create_bash_script_content_for_using_ws( @@ -370,8 +381,9 @@ def main(self, *, args): tmp_file = f"/tmp/ros_team_workspace/wokspace_{os.getppid()}.bash" print(f"Following text will be written into file '{tmp_file}':\n{script_content}") if not create_file_and_write(tmp_file, content=script_content): - print(f"Failed to write workspace data to a file {tmp_file}.") - return + return f"Failed to write workspace data to a file {tmp_file}." + + print(f"Using workspace '{ws_name}'") class PortAllVerb(VerbExtension): diff --git a/rtwcli/rtwcli/setup.py b/rtwcli/rtwcli/setup.py index b0de2066..5379beee 100644 --- a/rtwcli/rtwcli/setup.py +++ b/rtwcli/rtwcli/setup.py @@ -38,7 +38,7 @@ tests_require=["pytest"], entry_points={ "rtwcli.command": [ - "extension_points =" " rtwcli.command.extension_points:ExtensionPointsCommand", + "extension_points = rtwcli.command.extension_points:ExtensionPointsCommand", "extensions = rtwcli.command.extensions:ExtensionsCommand", "info = rtwcli.command.info:InfoCommand", ],