Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substitutions for string manipulations and xacro #741

Open
nlamprian opened this issue Nov 4, 2023 · 4 comments · May be fixed by #838
Open

Substitutions for string manipulations and xacro #741

nlamprian opened this issue Nov 4, 2023 · 4 comments · May be fixed by #838
Assignees

Comments

@nlamprian
Copy link
Contributor

Feature request

Feature description

Coming back with another request for a substitution... I tried using a path join substitution with a filename that depends on a launch configuration. The path join substitution does not allow one to concatenate parts of a filename. It ended up using a python expression.

xacro_filename = PythonExpression(["'", LaunchConfiguration("model"), "' + '.xacro'"])
xacro_filepath = PathJoinSubstitution(["robot_description", "urdf", xacro_filename])

Implementation considerations

I would have hoped that text substitution could take multiple texts so that I could do

TextSubstitution(LaunchConfiguration("model"), ".xacro")

But this is not compatible, given how text substitution was put together.

So, how about having a substitution that concatenates strings and then also having the separator parameterized?

In the simple case, one would do

StringSubstitution([LaunchConfiguration("model"), ".xacro"])  # -> "model_value.xacro"

And then, this could be used in other practical ways, for example, to create a xacro substitution

class XacroSubstitution(Command):
    def __init__(
        self, 
        filepath: SomeSubstitutionsType, 
        arg_str: SomeSubstitutionsType = "",  # xacro arguments that are passed as a launch argument
        arg_dict: Dict = None,  # xacro arguments that are defined in the launch file
    ):
        command = [FindExecutable(name="xacro"), " ", filepath, " "]
        arg_str, arg_dict = normalize_to_list_of_substitutions(arg_str), arg_dict or {}
        arg_list = [StringSubstitution([key, value], sep=":=") for key, value in arg_dict.items()]
        command.extend(StringSubstitution(arg_list + arg_str, sep=" "))
        super().__init__(command)
@matosinho
Copy link

I did the same with Python expression for ".sdf". That was the exact thing I tried, to use a list with TextSubstitution, but then I discovered that it was not possible. Nice suggestion!

@christophebedard
Copy link
Member

christophebedard commented Mar 16, 2025

I wonder if we could make SomeSubstitutionsType accept lists of lists of text/substitutions, similar to what @dvdmc suggested in #835 (comment). Something like:

SomeSubstitutionsType = Union[
    Text,
    Substitution,
    Iterable[Union[Text, Substitution, Iterable[Text, Substitution]]],
]

I don't know if this is doable in practice or if it has any unforeseen implications. If anyone could give it a try and find out, it would be appreciated! Or maybe adding a substitution like #768 does helps prevent us from opening a can of worms?

@christophebedard
Copy link
Member

christophebedard commented Mar 16, 2025

Wait, PathJoinSubstitution currently takes in an Iterable[Union[Text, Substitution]], not a SomeSubstitutionsType:

def __init__(self, substitutions: Iterable[Union[Text, Substitution]]) -> None:

If it took in an Iterable[SomeSubstitutionsType], then the example in #835 (comment) might be possible. That makes more sense for PathJoinSubstitution, since it joins strings into a path.

@christophebedard
Copy link
Member

@dvdmc @nlamprian @matosinho: please see #838. It improves PathJoinSubstitution to allow something like:

PathJoinSubstitution(['robot_description', 'urdf', [LaunchConfiguration('model'), '.xacro']])

@christophebedard christophebedard marked this as a duplicate of #844 Mar 23, 2025
@christophebedard christophebedard self-assigned this Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants