68 lines
1.6 KiB
Python
Raw Normal View History

2022-04-10 23:32:25 +02:00
# Copyright 2020-2022 Ternaris.
2021-05-02 14:51:08 +02:00
# SPDX-License-Identifier: Apache-2.0
"""CLI tool for rosbag conversion."""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
from typing import TYPE_CHECKING
from .converter import ConverterError, convert
if TYPE_CHECKING:
from typing import Callable
2021-11-25 14:26:17 +01:00
def pathtype(exists: bool = True) -> Callable[[str], Path]:
2021-05-02 14:51:08 +02:00
"""Path argument for argparse.
Args:
exists: Path should exists in filesystem.
Returns:
Argparse type function.
"""
def topath(pathname: str) -> Path:
path = Path(pathname)
if exists != path.exists():
raise argparse.ArgumentTypeError(
f'{path} should {"exist" if exists else "not exist"}.',
)
return path
return topath
def main() -> None:
"""Parse cli arguments and run conversion."""
2022-01-09 20:31:51 +01:00
parser = argparse.ArgumentParser(description='Convert between rosbag1 and rosbag2.')
2021-05-02 14:51:08 +02:00
parser.add_argument(
'src',
type=pathtype(),
2022-01-09 20:31:51 +01:00
help='source path to read rosbag1 or rosbag2 from',
2021-05-02 14:51:08 +02:00
)
parser.add_argument(
'--dst',
type=pathtype(exists=False),
2022-01-09 20:31:51 +01:00
help='destination path for converted rosbag',
2021-05-02 14:51:08 +02:00
)
2022-01-09 20:31:51 +01:00
2021-05-02 14:51:08 +02:00
args = parser.parse_args()
2022-01-09 20:31:51 +01:00
if args.dst is not None and (args.src.suffix == '.bag') == (args.dst.suffix == '.bag'):
print('Source and destination rosbag versions must differ.') # noqa: T001
sys.exit(1)
2021-05-02 14:51:08 +02:00
try:
convert(args.src, args.dst)
except ConverterError as err:
print(f'ERROR: {err}') # noqa: T001
sys.exit(1)
if __name__ == '__main__':
main()