Skip to content

Commit

Permalink
[backport fumble] Allow passing storage config as commandline contents (
Browse files Browse the repository at this point in the history
#12) (#14)

* Allow passing storage config as commandline contents (#12)

Signed-off-by: Emerson Knapp <emerson.b.knapp@gmail.com>
  • Loading branch information
emersonknapp authored Nov 9, 2023
1 parent 280fedc commit eb365f5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
37 changes: 36 additions & 1 deletion ros2bag/ros2bag/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from argparse import ArgumentParser, ArgumentTypeError
from argparse import (
ArgumentParser,
ArgumentTypeError,
FileType,
Namespace,
)
import os
import tempfile
from typing import Any
from typing import Dict
from typing import Optional
Expand Down Expand Up @@ -129,3 +135,32 @@ def add_standard_reader_args(parser: ArgumentParser) -> None:
'-s', '--storage', default='', choices=reader_choices,
help='Storage implementation of bag. '
'By default attempts to detect automatically - use this argument to override.')
config_group = parser.add_mutually_exclusive_group()
config_group.add_argument(
'--storage-config-file',
type=FileType('r'),
help='Path to a file defining storage-specific configuration. '
'See documentation for each storage implementation for details on the structure '
'of this file.'
)
config_group.add_argument(
'--storage-config',
type=str,
help='Storage configuration as a string, which will be placed in a temporary file '
'and passed to storage as if it were --storage-config-file.'
)


def storage_config_file_from_args(args: Namespace):
"""Return a file object to pass to storage plugins, based on args."""
if args.storage_config_file:
return args.storage_config_file
elif args.storage_config:
# Create a temporary file to pass to storage plugins
storage_config_file = tempfile.NamedTemporaryFile(
prefix='rosbag2_storage_config_')
storage_config_file.write(args.storage_config.encode('utf-8'))
storage_config_file.flush()
storage_config_file.seek(0)
return storage_config_file
return None
15 changes: 3 additions & 12 deletions ros2bag/ros2bag/verb/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ros2bag.api import check_positive_float
from ros2bag.api import convert_yaml_to_qos_profile
from ros2bag.api import print_error
from ros2bag.api import storage_config_file_from_args
from ros2bag.verb import VerbExtension
from ros2cli.node import NODE_NAME_PREFIX
from rosbag2_py import Player
Expand Down Expand Up @@ -63,14 +64,6 @@ def add_arguments(self, parser, cli_name): # noqa: D102
'--remap', '-m', default='', nargs='+',
help='list of topics to be remapped: in the form '
'"old_topic1:=new_topic1 old_topic2:=new_topic2 etc." ')
parser.add_argument(
'--storage-config-file', type=FileType('r'),
help='Path to a yaml file defining storage specific configurations. '
'For the default storage plugin settings are specified through syntax:'
'read:'
' pragmas: [\"<setting_name>\" = <setting_value>]'
'Note that applicable settings are limited to read-only for ros2 bag play.'
'For a list of sqlite3 settings, refer to sqlite3 documentation')
parser.add_argument(
'--clock', type=positive_float, nargs='?', const=40, default=0,
help='Publish to /clock at a specific frequency in Hz, to act as a ROS Time Source. '
Expand Down Expand Up @@ -115,9 +108,7 @@ def main(self, *, args): # noqa: D102
except (InvalidQoSProfileException, ValueError) as e:
return print_error(str(e))

storage_config_file = ''
if args.storage_config_file:
storage_config_file = args.storage_config_file.name
storage_config_file = storage_config_file_from_args(args)

topic_remapping = ['--ros-args']
for remap_rule in args.remap:
Expand All @@ -127,7 +118,7 @@ def main(self, *, args): # noqa: D102
storage_options = StorageOptions(
uri=args.bag_path,
storage_id=args.storage,
storage_config_uri=storage_config_file,
storage_config_uri=storage_config_file.name if storage_config_file else '',
)
play_options = PlayOptions()
play_options.read_ahead_queue_size = args.read_ahead_queue_size
Expand Down

0 comments on commit eb365f5

Please sign in to comment.