From 0bf94a6238a7d15e648ceec1bc6d871f9a663d0b Mon Sep 17 00:00:00 2001 From: Marko Durkovic Date: Wed, 25 May 2022 15:39:47 +0200 Subject: [PATCH] Add typename guessing to examples --- docs/examples/register_types_files.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/examples/register_types_files.py b/docs/examples/register_types_files.py index 3cf1fd57..74eae068 100644 --- a/docs/examples/register_types_files.py +++ b/docs/examples/register_types_files.py @@ -4,21 +4,32 @@ from pathlib import Path from rosbags.typesys import get_types_from_msg, register_types + +def guess_msgtype(path: Path) -> str: + """Guess message type name from path.""" + name = path.relative_to(path.parents[2]).with_suffix('') + if 'msg' not in name.parts: + name = name.parent / 'msg' / name.name + return str(name) + + add_types = {} -msgdef = Path('/path/to/custom_msgs/msg/Speed.msg').read_text(encoding='utf-8') -add_types.update(get_types_from_msg(msgdef, 'custom_msgs/msg/Speed.msg')) - -msgdef = Path('/path/to/custom_msgs/msg/Accel.msg').read_text(encoding='utf-8') -add_types.update(get_types_from_msg(msgdef, 'custom_msgs/msg/Accel.msg')) +for pathstr in [ + '/path/to/custom_msgs/msg/Speed.msg', + '/path/to/custom_msgs/msg/Accel.msg', +]: + msgpath = Path(pathstr) + msgdef = msgpath.read_text(encoding='utf-8') + add_types.update(get_types_from_msg(msgdef, guess_msgtype(msgpath))) register_types(add_types) # Type import works only after the register_types call, -# the classname is derived from the msgtype names above +# the classname is derived from the msgtype names above. # pylint: disable=no-name-in-module,wrong-import-position -from rosbags.typesys.types import custom_msgs__msg__Speed as Speed # type: ignore # noqa from rosbags.typesys.types import custom_msgs__msg__Accel as Accel # type: ignore # noqa +from rosbags.typesys.types import custom_msgs__msg__Speed as Speed # type: ignore # noqa # pylint: enable=no-name-in-module,wrong-import-position