-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
296 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Copyright DB Netz AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
"""CLI for importing .msg to capella model.""" | ||
import sys | ||
from pathlib import Path | ||
|
||
import click | ||
|
||
import rosidl2capella | ||
from rosidl2capella.modules import BASIC_TYPES, ROS_INTERFACES | ||
from rosidl2capella.modules.parse_capella import ParseCapella | ||
from rosidl2capella.modules.serialize_message import ( | ||
SerializeMessageDef, | ||
SerializeMessagesPkg, | ||
) | ||
|
||
|
||
class Capella2Msg: | ||
"""Class for converting capella model to .msg files.""" | ||
|
||
def __init__(self, path_to_capella_model, layer, overlap) -> None: | ||
self.parser = ParseCapella(path_to_capella_model, layer) | ||
self.overlap = overlap | ||
|
||
def add_package(self, current_root): | ||
"""Add package to message package.""" | ||
out = SerializeMessagesPkg({}, {}) | ||
|
||
messages = self.parser.get_classes(current_root) | ||
types = self.parser.get_types(current_root) | ||
|
||
out.messages = { | ||
msg_name: SerializeMessageDef(msg_name, desc, props) | ||
for msg_name, (desc, props) in (messages | types).items() | ||
} | ||
out.packages = { | ||
pkg_name: self.add_package(current_root.packages.by_name(pkg_name)) | ||
for pkg_name in self.parser.get_packages(current_root) | ||
} | ||
return out | ||
|
||
|
||
@click.command() | ||
@click.version_option( | ||
version=rosidl2capella.__version__, | ||
prog_name="rosidl2capella", | ||
message="%(prog)s %(version)s", | ||
) | ||
@click.argument( | ||
"path-to-msgs-root", | ||
type=click.Path( | ||
file_okay=False, | ||
readable=True, | ||
resolve_path=True, | ||
path_type=Path, | ||
), | ||
required=True, | ||
) | ||
@click.argument( | ||
"path-to-capella-model", | ||
type=click.Path( | ||
exists=True, | ||
readable=True, | ||
resolve_path=True, | ||
path_type=str, | ||
), | ||
required=True, | ||
) | ||
@click.argument( | ||
"layer", | ||
type=click.Choice(["oa", "sa", "la", "pa"], case_sensitive=False), | ||
required=True, | ||
) | ||
@click.option( | ||
"-o", | ||
"--overlap", | ||
type=click.Choice( | ||
["keep", "overwrite", "ask", "abort"], case_sensitive=False | ||
), | ||
default="ask" if sys.stdin.isatty() else "abort", | ||
) | ||
@click.option("--debug", is_flag=True) | ||
def capella2msg( | ||
path_to_msgs_root, path_to_capella_model, layer, overlap, debug | ||
): | ||
"""Convert capella model to .msg files.""" | ||
converter = Capella2Msg(path_to_capella_model, layer, overlap) | ||
current_root = converter.parser.data | ||
|
||
messages = converter.parser.get_classes(current_root) | ||
types = converter.parser.get_types(current_root) | ||
|
||
packages = converter.parser.get_packages(current_root) | ||
packages.discard(BASIC_TYPES) | ||
packages.discard(ROS_INTERFACES) | ||
|
||
root = SerializeMessagesPkg({}, {}) | ||
root.messages = { | ||
msg_name: SerializeMessageDef(msg_name, desc, props) | ||
for msg_name, (desc, props) in (messages | types).items() | ||
} | ||
|
||
root.packages = { | ||
pkg_name: converter.add_package( | ||
current_root.packages.by_name(pkg_name) | ||
) | ||
for pkg_name in packages | ||
} | ||
|
||
if debug: | ||
click.echo(root) | ||
else: | ||
root.to_msg_folder(path_to_msgs_root) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright DB Netz AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
"""Serializer for ROS messages.""" | ||
|
||
from pathlib import Path | ||
|
||
from . import MessageDef, MessagesPkg | ||
|
||
|
||
class SerializeMessageDef(MessageDef): | ||
"""Serializer for message files.""" | ||
|
||
def to_msg_file(self, file: Path) -> None: | ||
"""Write message definition to message file.""" | ||
description = "# " + self.description.replace("\n", "\n# ") + "\n" | ||
props = "\n".join( | ||
f"{p.typedir+'/' if p.typedir else ''}{p.type}" | ||
f"{'[]' if p.min != p.max else ''} {p.name}\t" | ||
f"{'# ' if p.comment else ''}{p.comment}" | ||
for p in self.props | ||
) | ||
file.write_text(description + "\n" + props) | ||
|
||
def to_type_file(self, file: Path) -> None: | ||
"""Write message definition to message file.""" | ||
description = "# " + self.description.replace("\n", "\n# ") + "\n" | ||
props = "\n".join( | ||
f"{p.type} {file.stem + '_' + p.name}\t= {p.value}\t" | ||
f"{'# ' if p.comment else ''}{p.comment}" | ||
for p in self.props | ||
) | ||
file.write_text(description + "\n" + props) | ||
|
||
|
||
class SerializeMessagesPkg(MessagesPkg): | ||
"""Serializer for message packages.""" | ||
|
||
def to_msg_folder(self, path_to_pkg_root: Path) -> None: | ||
"""Write message package to message package.""" | ||
path_to_pkg_root.mkdir(parents=True, exist_ok=True) | ||
for msg_name, msg in self.messages.items(): | ||
msg_file = path_to_pkg_root.joinpath(msg_name + ".msg") | ||
if path_to_pkg_root.name == "types": | ||
msg.to_type_file(msg_file) | ||
else: | ||
msg.to_msg_file(msg_file) | ||
|
||
for pkg_name, pkg in self.packages.items(): | ||
new_path = path_to_pkg_root.joinpath(pkg_name) | ||
new_path.mkdir(parents=True, exist_ok=True) | ||
pkg.to_msg_folder(new_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.