Skip to content

Commit 913eae2

Browse files
authored
Add file-content launch substitution (#708)
This substitution implements a portable mechanism to read the contents of a file on disk. This procedure is used in various scenarios throughout ROS 2, and currently relies on `exec` and `command` substitutions to read files. Signed-off-by: Scott K Logan <[email protected]>
1 parent 05579ba commit 913eae2

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

launch/launch/substitutions/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .command import Command
2424
from .environment_variable import EnvironmentVariable
2525
from .equals_substitution import EqualsSubstitution
26+
from .file_content import FileContent
2627
from .find_executable import FindExecutable
2728
from .launch_configuration import LaunchConfiguration
2829
from .launch_log_dir import LaunchLogDir
@@ -43,6 +44,7 @@
4344
'Command',
4445
'EqualsSubstitution',
4546
'EnvironmentVariable',
47+
'FileContent',
4648
'FindExecutable',
4749
'LaunchConfiguration',
4850
'LaunchLogDir',
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2023 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Module for the FileContent substitution."""
16+
17+
from typing import List
18+
from typing import Sequence
19+
from typing import Text
20+
21+
from .substitution_failure import SubstitutionFailure
22+
from ..frontend import expose_substitution
23+
from ..launch_context import LaunchContext
24+
from ..some_substitutions_type import SomeSubstitutionsType
25+
from ..substitution import Substitution
26+
27+
28+
@expose_substitution('file-content')
29+
class FileContent(Substitution):
30+
"""
31+
Substitution that reads the contents of a file.
32+
33+
If the file is not found a `SubstitutionFailure` error is raised.
34+
"""
35+
36+
def __init__(self, path: SomeSubstitutionsType) -> None:
37+
"""Create a FileContent substitution."""
38+
super().__init__()
39+
40+
from ..utilities import normalize_to_list_of_substitutions
41+
self.__path = normalize_to_list_of_substitutions(path)
42+
43+
@classmethod
44+
def parse(cls, data: Sequence[SomeSubstitutionsType]):
45+
"""Parse `FileContent` substitution."""
46+
if not data or len(data) != 1:
47+
raise AttributeError('file content substitutions expect 1 argument')
48+
kwargs = {'path': data[0]}
49+
return cls, kwargs
50+
51+
@property
52+
def path(self) -> List[Substitution]:
53+
"""Getter for path."""
54+
return self.__path
55+
56+
def describe(self) -> Text:
57+
"""Return a description of this substitution as a string."""
58+
return 'FileContent({})'.format(
59+
', '.join([sub.describe() for sub in self.path]))
60+
61+
def perform(self, context: LaunchContext) -> Text:
62+
"""Perform the substitution by evaluating the expression."""
63+
from ..utilities import perform_substitutions
64+
path = perform_substitutions(context, self.path)
65+
try:
66+
with open(path, 'r') as f:
67+
return f.read()
68+
except FileNotFoundError:
69+
raise SubstitutionFailure('File not found: {}'.format(path))
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2023 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Tests the FileContent substitution."""
16+
17+
import pathlib
18+
19+
from launch.launch_context import LaunchContext
20+
from launch.substitutions import FileContent
21+
from launch.substitutions.substitution_failure import SubstitutionFailure
22+
23+
import pytest
24+
25+
26+
@pytest.fixture(scope='module')
27+
def files():
28+
this_dir = pathlib.Path(__file__).parent
29+
30+
files = {
31+
'foo': str(this_dir / 'test_file_content' / 'foo.txt'),
32+
'bar': str(this_dir / 'test_file_content' / 'bar.txt'),
33+
}
34+
35+
return files
36+
37+
38+
def test_file_content(files):
39+
"""Test a simple file."""
40+
context = LaunchContext()
41+
file_content = FileContent(files['foo'])
42+
output = file_content.perform(context)
43+
assert output == 'Foo\n'
44+
45+
46+
def test_missing_command_raises(files):
47+
"""Test that a file that doesn't exist raises."""
48+
context = LaunchContext()
49+
file_content = FileContent(files['bar'])
50+
with pytest.raises(SubstitutionFailure) as ex:
51+
file_content.perform(context)
52+
ex.match('File not found: .*')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Foo

0 commit comments

Comments
 (0)