launch.substitutions package
Submodules
- launch.substitutions.anon_name module
- launch.substitutions.boolean_substitution module
- launch.substitutions.command module
- launch.substitutions.environment_variable module
- launch.substitutions.equals_substitution module
- launch.substitutions.file_content module
- launch.substitutions.find_executable module
- launch.substitutions.for_loop_var module
- launch.substitutions.if_else_substitution module
- launch.substitutions.launch_configuration module
- launch.substitutions.launch_log_dir module
- launch.substitutions.local_substitution module
- launch.substitutions.not_equals_substitution module
- launch.substitutions.path_join_substitution module
- launch.substitutions.python_expression module
- launch.substitutions.string_join_substitution module
- launch.substitutions.substitution_failure module
- launch.substitutions.text_substitution module
- launch.substitutions.this_launch_file module
- launch.substitutions.this_launch_file_dir module
Module contents
Package for substitutions.
- class launch.substitutions.AllSubstitution[source]
Bases:
SubstitutionSubstitutes to the string ‘true’ if all of the input arguments evaluate to true.
If any of the arguments evaluates to false, then this substitution returns the string ‘false’.
- __init__(*args: str | Path | Substitution | Iterable[str | Path | Substitution], container: Iterable[str | Path | Substitution | Iterable[str | Path | Substitution]] | None = None) None[source]
Create an AllSubstitution substitution.
The following string arguments evaluate to true: ‘1’, ‘true’, ‘True’, ‘on’ The following string arguments evaluate to false: ‘0’, ‘false’, ‘False’, ‘off’
- property args: List[List[Substitution]]
Getter for args.
- classmethod parse(data: Iterable[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse AllSubstitution substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution.
- class launch.substitutions.AndSubstitution[source]
Bases:
SubstitutionSubstitution that returns ‘and’ of the input boolean values.
- __init__(left: str | Path | Substitution | Iterable[str | Path | Substitution], right: str | Path | Substitution | Iterable[str | Path | Substitution]) None[source]
Create an AndSubstitution substitution.
- property left: List[Substitution]
Getter for left.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse AndSubstitution substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution.
- property right: List[Substitution]
Getter for right.
- class launch.substitutions.AnonName[source]
Bases:
SubstitutionGenerates an anonymous id based on name.
Name itself is a unique identifier: multiple uses of anon with the same parameter name will create the same “anonymized” name
- __init__(name: str | Path | Substitution | Iterable[str | Path | Substitution]) None[source]
Construct an AnonName substitution.
- property name: List[Substitution]
Getter for name.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse AnonName substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution by creating/getting the anonymous name.
- class launch.substitutions.AnySubstitution[source]
Bases:
SubstitutionSubstitutes to the string ‘true’ if at least one of the input arguments evaluates to true.
If none of the arguments evaluate to true, then this substitution returns the string ‘false’.
- __init__(*args: str | Path | Substitution | Iterable[str | Path | Substitution], container: Iterable[str | Path | Substitution | Iterable[str | Path | Substitution]] | None = None) None[source]
Create an AnySubstitution substitution.
The following string arguments evaluate to true: ‘1’, ‘true’, ‘True’, ‘on’
- property args: List[List[Substitution]]
Getter for args.
- classmethod parse(data: Iterable[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse AnySubstitution substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution.
- class launch.substitutions.Command[source]
Bases:
SubstitutionSubstitution that gets the output of a command as a string.
If the command is not found or fails a SubstitutionFailure error is raised. Behavior on stderr output is configurable, see constructor.
- __init__(command: str | Path | Substitution | Iterable[str | Path | Substitution], *, on_stderr: str | Path | Substitution | Iterable[str | Path | Substitution] = 'fail') None[source]
Construct a command substitution.
- Parameters:
command – command to be executed. The substitutions will be performed, and shlex.split will be used on the result.
on_stderr – specifies what to do when there is stderr output. Can be one of: - ‘fail’: raises SubstitutionFailere when stderr output is detected. - ‘ignore’: stderr output is ignored. - ‘warn’: The stderr output is ignored, but a warning is logged if detected. - ‘capture’: The stderr output will be captured, together with stdout. It can also be a substitution, that results in one of those four options.
- property command: List[Substitution]
Getter for command.
- property on_stderr: List[Substitution]
Getter for on_stderr.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse Command substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution by running the command and capturing its output.
- class launch.substitutions.EnvironmentVariable[source]
Bases:
SubstitutionSubstitution that gets an environment variable value as a string.
If the environment variable is not found, it returns empty string.
Note that the environment variable is resolved based on the launch context’s environment (i.e.,
context.environment) even though the substitution may be associated with an entity that might have a different set of environment variables.- __init__(name: str | Path | Substitution | Iterable[str | Path | Substitution], *, default_value: str | Path | Substitution | Iterable[str | Path | Substitution] | None = None) None[source]
Construct an environment variable substitution.
- Parameters:
name – name of the environment variable.
default_value – used when the environment variable doesn’t exist. If None, the substitution is not optional.
- Raises:
launch.substitutions.substitution_failure.SubstitutionFailure – If the environment variable doesn’t exist and default_value is None.
- property default_value: List[Substitution] | None
Getter for default_value.
- property name: List[Substitution]
Getter for name.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse EnviromentVariable substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution by looking up the environment variable.
- class launch.substitutions.EqualsSubstitution[source]
Bases:
SubstitutionSubstitution that checks if two inputs are equal.
Returns ‘true’ or ‘false’ strings depending on the result.
- __init__(left: Any | Iterable[Any] | None, right: Any | Iterable[Any] | None) None[source]
Create an EqualsSubstitution substitution.
- property left: List[Substitution]
Getter for left.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse EqualsSubstitution substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution.
- property right: List[Substitution]
Getter for right.
- class launch.substitutions.FileContent[source]
Bases:
SubstitutionSubstitution that reads the contents of a file.
If the file is not found a SubstitutionFailure error is raised.
- __init__(path: str | Path | Substitution | Iterable[str | Path | Substitution]) None[source]
Create a FileContent substitution.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse FileContent substitution.
- property path: List[Substitution]
Getter for path.
- perform(context: LaunchContext) str[source]
Perform the substitution by evaluating the expression.
- class launch.substitutions.FindExecutable[source]
Bases:
SubstitutionSubstitution that tries to locate an executable on the PATH.
- Raise:
SubstitutionFailure when executable not found
- __init__(*, name: str | Path | Substitution | Iterable[str | Path | Substitution]) None[source]
Create a FindExecutable substitution.
- property name: List[Substitution]
Getter for name.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse FindExecutable substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution by locating the executable on the PATH.
- class launch.substitutions.ForEachVar[source]
Bases:
SubstitutionSubstitution for a
launch.actions.ForEachiteration variable value.- __init__(name: str | Path | Substitution | Iterable[str | Path | Substitution], *, default_value: str | Path | Substitution | Iterable[str | Path | Substitution] | None = None) None[source]
Create a ForEachVar.
- Parameters:
name – the name of the
launch.actions.ForEachiteration variabledefault_value – a default value for the variable if a value is not available for a given iteration
- property default_value: List[Substitution] | None
- describe() str[source]
Return a description of this substitution as a string.
When inherited from, calling this base class’s default method is not required.
- property name: List[Substitution]
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
- perform(context: LaunchContext) str[source]
Perform the substitution, given the launch context, and return it as a string.
This should be overridden by the derived classes, and the default raises NotImplementedError.
- Raises:
NotImplementedError
- class launch.substitutions.ForLoopIndex[source]
Bases:
ForEachVarSubstitution for a
launch.actions.ForLoopiteration index value.- __init__(name: str | Path | Substitution | Iterable[str | Path | Substitution], **kwargs) None[source]
Create a ForLoopIndex.
- Parameters:
name – the name of the
launch.actions.ForLoopindex which this substitution is part of
- class launch.substitutions.IfElseSubstitution[source]
Bases:
SubstitutionSubstitution that conditionally returns one of two substitutions.
Depending on whether the condition substitution evaluates to true, either it returns the if_value substitution or the else_value substitution.
Example with a boolean launch configuration:
>>> from launch.substitutions import LaunchConfiguration >>> subst = IfElseSubstitution( ... LaunchConfiguration("arg"), ... if_value="arg_evaluated_to_true", ... else_value="arg_evaluated_to_false")
Combine with boolean substitutions to create more complex conditions. Example with multiple boolean launch configurations:
>>> from launch.substitutions import AllSubstitution >>> from launch.substitutions import EqualsSubstitution >>> from launch.substitutions import LaunchConfiguration >>> from launch.substitutions import NotSubstitution >>> subst = IfElseSubstitution( ... AllSubstitution(EqualsSubstitution(LaunchConfiguration("arg1"), ... LaunchConfiguration("arg2")), ... NotSubstitution(LaunchConfiguration("arg3"))), ... if_value="all_args_evaluated_to_true", ... else_value="at_least_one_arg_evaluated_to_false")
- __init__(condition: str | Path | Substitution | Iterable[str | Path | Substitution], if_value: str | Path | Substitution | Iterable[str | Path | Substitution] = '', else_value: str | Path | Substitution | Iterable[str | Path | Substitution] = '') None[source]
Create a IfElseSubstitution substitution.
- property condition: List[Substitution]
Getter for condition.
- property else_value: List[Substitution]
Getter for else value.
- property if_value: List[Substitution]
Getter for if value.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse IfElseSubstitution substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution by evaluating the condition.
- class launch.substitutions.LaunchConfiguration[source]
Bases:
SubstitutionSubstitution that can access launch configuration variables.
- __init__(variable_name: str | Path | Substitution | Iterable[str | Path | Substitution], *, default: Any | Iterable[Any] | None = None) None[source]
Create a LaunchConfiguration substitution.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse FindExecutable substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution by retrieving the launch configuration, as a string.
If the launch configuration is not found and a default has been set, the default will be returned, as a string.
- property variable_name: List[Substitution]
Getter for variable_name.
- class launch.substitutions.LaunchLogDir[source]
Bases:
PathSubstitutionSubstitution that returns the absolute path to the current launch log directory.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse LaunchLogDir substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution by returning the path to the current launch log directory.
- class launch.substitutions.LocalSubstitution[source]
Bases:
SubstitutionSubstitution that can access contextual local variables.
- perform(context: LaunchContext) str[source]
Perform the substitution by retrieving the local variable.
- class launch.substitutions.NotEqualsSubstitution[source]
Bases:
EqualsSubstitutionSubstitution that checks if two inputs are not equal.
Returns ‘true’ or ‘false’ strings depending on the result.
- __init__(left: Any | Iterable[Any] | None, right: Any | Iterable[Any] | None) None[source]
Create a NotEqualsSubstitution substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution.
- class launch.substitutions.NotSubstitution[source]
Bases:
SubstitutionSubstitution that returns ‘not’ of the input boolean value.
- __init__(value: str | Path | Substitution | Iterable[str | Path | Substitution]) None[source]
Create a NotSubstitution substitution.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse NotSubstitution substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution.
- property value: List[Substitution]
Getter for value.
- class launch.substitutions.OrSubstitution[source]
Bases:
SubstitutionSubstitution that returns ‘or’ of the input boolean values.
- __init__(left: str | Path | Substitution | Iterable[str | Path | Substitution], right: str | Path | Substitution | Iterable[str | Path | Substitution]) None[source]
Create an OrSubstitution substitution.
- property left: List[Substitution]
Getter for left.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse OrSubstitution substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution.
- property right: List[Substitution]
Getter for right.
- class launch.substitutions.PathJoinSubstitution[source]
Bases:
SubstitutionSubstitution that join paths, in a platform independent way.
This takes in a list of path components as substitutions. The substitutions for each path component are performed and concatenated, and then all path components are joined.
For example:
PathJoinSubstitution([ EnvironmentVariable('SOME_DIR'), 'cfg', ['config_', LaunchConfiguration('map'), '.yml'] ])
Or:
cfg_dir = PathJoinSubstitution([EnvironmentVariable('SOME_DIR'), 'cfg']) cfg_file = cfg_dir / ['config_', LaunchConfiguration('map'), '.yml']
If the
SOME_DIRenvironment variable was set to/home/user/dirand themaplaunch configuration was set tomy_map, this would result in a path equal equivalent to (depending on the platform):'/home/user/dir/cfg/config_my_map.yml'- __init__(substitutions: Iterable[str | Path | Substitution | Iterable[str | Path | Substitution]]) None[source]
Create a PathJoinSubstitution.
- Parameters:
substitutions – the list of path component substitutions to join
- perform(context: LaunchContext) str[source]
Perform the substitutions and join into a path.
- property substitutions: List[List[Substitution]]
Getter for substitutions.
- class launch.substitutions.PathSubstitution[source]
Bases:
PathJoinSubstitutionThin wrapper on PathJoinSubstitution for more pathlib.Path-like construction.
PathSubstitution(LaunchConfiguration('base_dir')) / 'sub_dir' / 'file_name'
Which, for
base_dir:=/my_dir, results in (depending on the platform):/my_dir/sub_dir/file_name
- __init__(path: str | Path | Substitution | Iterable[str | Path | Substitution])[source]
Create a PathSubstitution.
- Parameters:
path – May be a single text or Substitution element,
or an Iterable of them which are then joined
- class launch.substitutions.PythonExpression[source]
Bases:
SubstitutionSubstitution that can access contextual local variables.
The expression may contain Substitutions, but must return something that can be converted to a string with str(). It also may contain math symbols and functions.
- __init__(expression: str | Path | Substitution | Iterable[str | Path | Substitution], python_modules: str | Path | Substitution | Iterable[str | Path | Substitution] = ['math']) None[source]
Create a PythonExpression substitution.
- property expression: List[Substitution]
Getter for expression.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse PythonExpression substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution by evaluating the expression.
- property python_modules: List[Substitution]
Getter for python modules.
- class launch.substitutions.StringJoinSubstitution[source]
Bases:
SubstitutionSubstitution that joins strings and/or other substitutions.
This takes in a list of string components as substitutions. The substitutions for each string component are performed and concatenated, and then all string components are joined with a specified delimiter as seperation.
For example:
StringJoinSubstitution( [['https', '://'], LaunchConfiguration('subdomain')], 'ros', 'org'], delimiter='.' )
If the
subdomainlaunch configuration was set todocsand thedelimiterto., this would result in a string equal to'https://docs.ros.org'- __init__(substitutions: Iterable[str | Path | Substitution | Iterable[str | Path | Substitution]], delimiter: str | Path | Substitution | Iterable[str | Path | Substitution] = '') None[source]
Create a StringJoinSubstitution.
- Parameters:
substitutions – the list of string component substitutions to join
delimiter – the text inbetween two consecutive components (default no text)
- property delimiter: List[Substitution]
Getter for delimiter.
- perform(context: LaunchContext) str[source]
Perform the substitution by retrieving the local variable.
- property substitutions: List[List[Substitution]]
Getter for substitutions.
- exception launch.substitutions.SubstitutionFailure[source]
Bases:
RuntimeErrorRaised when a Substitution fails.
- class launch.substitutions.TextSubstitution[source]
Bases:
SubstitutionSubstitution that wraps a single string text.
- perform(context: LaunchContext) str[source]
Perform the substitution by returning the string itself.
- class launch.substitutions.ThisLaunchFile[source]
Bases:
SubstitutionSubstitution that returns the absolute path to the current launch file.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse ThisLaunchFile substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution by returning the path to the current launch file.
If there is no current launch file, i.e. if run from a script, then an error is raised.
- Raises:
SubstitutionFailure if not in a launch file
- class launch.substitutions.ThisLaunchFileDir[source]
Bases:
PathSubstitutionSubstitution that returns the absolute path to the current launch file.
- classmethod parse(data: Sequence[str | Path | Substitution | Iterable[str | Path | Substitution]])[source]
Parse ThisLaunchFileDir substitution.
- perform(context: LaunchContext) str[source]
Perform the substitution by returning the path to the current launch file.
If there is no current launch file, i.e. if run from a script, then an error is raised.
- Raises:
launch.substitutions.substitution_failure.SubstitutionFailure – if not in a launch file