Skip to content

Commit

Permalink
fix: Use nested sync
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed Mar 12, 2024
1 parent f4c6ae3 commit e650fa1
Show file tree
Hide file tree
Showing 16 changed files with 412 additions and 450 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"-i",
"tests/data/data_model/example_msgs",
"-m",
"tests/data/empty_project_52",
"tests/data/ empty_project_60",
"-l",
"la",
"--no-deps"
Expand All @@ -57,7 +57,7 @@
"-i",
"git+https://github.com/DSD-DBS/dsd-ros-msg-definitions-oss",
"-m",
"tests/data/empty_project_52",
"tests/data/ empty_project_60",
"-l",
"la"
]
Expand Down
2 changes: 1 addition & 1 deletion capella_ros_tools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def import_msgs(
"-o",
"--output",
type=click.Path(path_type=pathlib.Path, file_okay=False),
default=pathlib.Path.cwd() / "export",
default=pathlib.Path.cwd() / "data-package",
help="Output directory for the .msg files.",
)
def export_capella(
Expand Down
4 changes: 1 addition & 3 deletions capella_ros_tools/exporter.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0
"""Tool for exporting a Capella data package to ROS messages."""
import logging

import pathlib
import re

from capellambse.model.crosslayer import information

from capella_ros_tools import data_model

logger = logging.getLogger(__name__)


def _clean_name(name: str) -> str:
return re.sub(r"\W", "", name)
Expand Down
87 changes: 35 additions & 52 deletions capella_ros_tools/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# SPDX-License-Identifier: Apache-2.0
"""Tool for importing ROS messages to a Capella data package."""

import logging

from capellambse import decl, filehandler, helpers

from capella_ros_tools import data_model
Expand All @@ -17,9 +15,6 @@
}


logger = logging.getLogger(__name__)


class Importer:
"""Class for importing ROS messages to a Capella data package."""

Expand Down Expand Up @@ -65,10 +60,8 @@ def _convert_datatype(self, promise_id: str) -> dict:

def _convert_package(
self,
parent: decl.Promise | decl.UUIDReference,
pkg_def: data_model.MessagePkgDef,
) -> list[dict]:
instructions = []
) -> dict:
classes = []
enums = []
packages = []
Expand All @@ -82,17 +75,17 @@ def _convert_package(
for new_pkg in pkg_def.packages:
promise_id = f"{pkg_def.name}.{new_pkg.name}"
self._promise_ids.add(promise_id)
packages.append(
{
"promise_id": promise_id,
"find": {
"name": new_pkg.name,
},
}
)
instructions.extend(
self._convert_package(decl.Promise(promise_id), new_pkg)
)
yml = {
"promise_id": promise_id,
"find": {
"name": new_pkg.name,
},
}

if new_sync := self._convert_package(new_pkg):
yml["sync"] = new_sync

packages.append(yml)

sync = {}
if classes:
Expand All @@ -102,15 +95,7 @@ def _convert_package(
if packages:
sync["packages"] = packages

if sync:
instructions.append(
{
"parent": parent,
"sync": sync,
}
)

return instructions
return sync

def _convert_class(
self, pkg_name: str, msg_def: data_model.MessageDef
Expand Down Expand Up @@ -182,35 +167,33 @@ def _convert_enum(self, enum_def: data_model.EnumDef) -> dict:

def to_yaml(self, layer_data_uuid: str, sa_data_uuid) -> str:
"""Import ROS messages into a Capella data package."""
instructions = self._convert_package(
decl.UUIDReference(helpers.UUIDString(layer_data_uuid)),
self.messages,
)

instructions = [
{
"parent": decl.UUIDReference(
helpers.UUIDString(layer_data_uuid)
),
"sync": self._convert_package(self.messages),
}
]
if needed_types := self._promise_id_refs - self._promise_ids:
datatypes = [
self._convert_datatype(promise_id)
for promise_id in needed_types
]
instructions.extend(
[
{
"parent": decl.UUIDReference(
helpers.UUIDString(sa_data_uuid)
),
"sync": {
"packages": [
{
"promise_id": "root.DataTypes",
"find": {"name": "Data Types"},
}
],
},
},
{
"parent": decl.Promise("root.DataTypes"),
"sync": {"datatypes": datatypes},
instructions.append(
{
"parent": decl.UUIDReference(
helpers.UUIDString(sa_data_uuid)
),
"sync": {
"packages": [
{
"promise_id": "root.DataTypes",
"find": {"name": "Data Types"},
"sync": {"datatypes": datatypes},
}
],
},
]
}
)
return decl.dump(instructions)
4 changes: 2 additions & 2 deletions docs/source/howtos.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Import ROS2 Messages:
python -m capella_ros_tools import \
-i tests/data/data_model/example_msgs \
-m tests/data/empty_project_52 -l la \
-m tests/data/empty_project_60 -l la \
--port=5000 --no-deps
Import ROS2 Messages from Git Repository:
Expand All @@ -28,7 +28,7 @@ Import ROS2 Messages from Git Repository:
python -m capella_ros_tools import \
-i git+https://github.com/DSD-DBS/dsd-ros-msg-definitions-oss \
-m tests/data/empty_project_52 -l la \
-m tests/data/ empty_project_60 -l la \
--port=5000
Export Capella data package:
Expand Down
4 changes: 2 additions & 2 deletions docs/source/messages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Enum definition
* **Indented Comment Lines:** Comments on a line of their own but indented are added to the description of the last encountered enum literal.
* **Block Comments:** Comments on a line of their own and not indented are added to the description of the next enum definition or the next enum literal definitions until an empty line and the block comment has been used.

.. literalinclude:: ../../tests/data/data_model/example_msgs/package1/msg/SampleEnum.msg
.. literalinclude:: ../../tests/data/data_model/example_msgs/package1/msg/types/SampleEnum.msg
:language: python

Enum and Class Definition
Expand All @@ -72,7 +72,7 @@ Enum and Class Definition
* Comments at the top of the file are added to the class description.
* **Inline Comments:** Comments on the same line as a property or enum literal are directly added to the description of that element.
* **Indented Comment Lines:** Comments on a line of their own but indented are added to the description of the last encountered property or enum literal.
* **Block Comments:** Comments on a line of their own and not indented are added to the descriptions of the next properties or added to the descriptions of the next/current enum until an empty line and the block comment has been used.
* **Block Comments:** Comments on a line of their own and not indented are added to the descriptions of the next properties, enum or enum literal until an empty line and the block comment has been used.

.. code-block:: python
Expand Down
Loading

0 comments on commit e650fa1

Please sign in to comment.