Skip to content

Commit

Permalink
show ws names for rtw ws and rtw workspace use
Browse files Browse the repository at this point in the history
  • Loading branch information
muritane committed Dec 12, 2023
1 parent 8bc082c commit 7c99916
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
7 changes: 4 additions & 3 deletions rtwcli/rtw_cmds/rtw_cmds/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down
62 changes: 37 additions & 25 deletions rtwcli/rtw_cmds/rtw_cmds/workspace/verbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
- /home/daniela/workspace/mojin/pid_ws -> hdw_mojin__pid_ws
"""

import copy
import argparse
import dataclasses
import datetime
import os
Expand Down Expand Up @@ -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(
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion rtwcli/rtwcli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down

0 comments on commit 7c99916

Please sign in to comment.