Skip to content

Commit

Permalink
feat: Update license header and add support for custom license header…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
micha91 committed Jul 30, 2024
1 parent 6868bef commit ce73f58
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 13 deletions.
2 changes: 1 addition & 1 deletion capella_ros_tools/.license_header.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# SPDX-FileCopyrightText: Copyright DB Netz AG
# SPDX-FileCopyrightText: Copyright DB InfraGO AG
# SPDX-License-Identifier: Apache-2.0
12 changes: 8 additions & 4 deletions capella_ros_tools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from capellambse import cli_helpers, decl

import capella_ros_tools
from capella_ros_tools import exporter, importer

from . import logger
from capella_ros_tools import exporter, importer, logger


@click.group()
Expand Down Expand Up @@ -71,6 +69,11 @@ def cli():
type=click.Path(path_type=pathlib.Path, dir_okay=False),
help="Produce a declarative YAML instead of modifying the source model.",
)
@click.option(
"--license-header",
type=click.Path(path_type=pathlib.Path, dir_okay=False),
help="Ignore the license header from the given file when importing msgs.",
)
def import_msgs(
input: str,
model: capellambse.MelodyModel,
Expand All @@ -79,6 +82,7 @@ def import_msgs(
types: uuid.UUID,
no_deps: bool,
output: pathlib.Path,
license_header: pathlib.Path | None,
) -> None:
"""Import ROS messages into a Capella data package."""
if root:
Expand All @@ -93,7 +97,7 @@ def import_msgs(
else:
params = {"types_parent_uuid": model.sa.data_package.uuid}

parsed = importer.Importer(input, no_deps)
parsed = importer.Importer(input, no_deps, license_header)
logger.info("Loaded %d packages", len(parsed.messages.packages))

yml = parsed.to_yaml(root_uuid, **params)
Expand Down
14 changes: 10 additions & 4 deletions capella_ros_tools/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,15 @@ def __eq__(self, other: object) -> bool:

@classmethod
def from_file(
cls, file: abc.AbstractFilePath | pathlib.Path
cls,
file: abc.AbstractFilePath | pathlib.Path,
license_header: str | None = None,
) -> MessageDef:
"""Create message definition from a .msg file."""
msg_name = file.stem
msg_string = file.read_text()
msg_string = msg_string.removeprefix(LICENSE_HEADER)
license_header = license_header or LICENSE_HEADER
msg_string = msg_string.removeprefix(license_header)
return cls.from_string(msg_name, msg_string)

@classmethod
Expand Down Expand Up @@ -391,7 +394,10 @@ def __eq__(self, other: object) -> bool:

@classmethod
def from_msg_folder(
cls, pkg_name: str, msg_path: abc.AbstractFilePath | pathlib.Path
cls,
pkg_name: str,
msg_path: abc.AbstractFilePath | pathlib.Path,
license_header: str | None = None,
) -> MessagePkgDef:
"""Create a message package definition from a folder."""
out = cls(pkg_name, [], [])
Expand All @@ -400,6 +406,6 @@ def from_msg_folder(
msg_path.rglob("*.msg"),
)
for msg_file in sorted(files, key=os.fspath):
msg_def = MessageDef.from_file(msg_file)
msg_def = MessageDef.from_file(msg_file, license_header)
out.messages.append(msg_def)
return out
9 changes: 8 additions & 1 deletion capella_ros_tools/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""Tool for importing ROS messages to a Capella data package."""

import os
import pathlib
import typing as t

from capellambse import decl, filehandler, helpers
Expand All @@ -27,10 +28,14 @@ def __init__(
self,
msg_path: str,
no_deps: bool,
license_header_path: pathlib.Path | None = None,
):
self.messages = data_model.MessagePkgDef("root", [], [])
self._promise_ids: dict[str, None] = {}
self._promise_id_refs: dict[str, None] = {}
self._license_header = None
if license_header_path is not None:
self._license_header = license_header_path.read_text("utf-8")

self._add_packages("ros_msgs", msg_path)
if no_deps:
Expand All @@ -43,7 +48,9 @@ def _add_packages(self, name: str, path: str) -> None:
root = filehandler.get_filehandler(path).rootdir
for dir in sorted(root.rglob("msg"), key=os.fspath):
pkg_name = dir.parent.name or name
pkg_def = data_model.MessagePkgDef.from_msg_folder(pkg_name, dir)
pkg_def = data_model.MessagePkgDef.from_msg_folder(
pkg_name, dir, self._license_header
)
self.messages.packages.append(pkg_def)
logger.info("Loaded package %s from %s", pkg_name, dir)

Expand Down
3 changes: 3 additions & 0 deletions tests/data/data_model/custom_license_header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG
# SPDX-License-Identifier: Apache-2.0
# Additional Stuff to be removed
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG
# SPDX-License-Identifier: Apache-2.0
# Additional Stuff to be removed

# SampleClassEnum.msg
# Properties in SampleClassEnum can reference
# enums in the same file.

# This block comment is added to the
# enum description of SampleClassEnumStatus.
uint8 OK = 0
uint8 WARN = 1
uint8 ERROR = 2
uint8 STALE = 3

# This block comment is added to the
# enum description of Color.
uint8 COLOR_RED = 0
uint8 COLOR_BLUE = 1
uint8 COLOR_YELLOW = 2

uint8 status # The property status is of type
# SampleClassEnumStatus.
uint8 color # The property color is of type Color.
uint8 field
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright DB Netz AG
# SPDX-FileCopyrightText: Copyright DB InfraGO AG
# SPDX-License-Identifier: Apache-2.0

# SampleClass.msg
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright DB Netz AG
# SPDX-FileCopyrightText: Copyright DB InfraGO AG
# SPDX-License-Identifier: Apache-2.0

# SampleEnum.msg
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright DB Netz AG
# SPDX-FileCopyrightText: Copyright DB InfraGO AG
# SPDX-License-Identifier: Apache-2.0

# SampleClassEnum.msg
Expand Down
21 changes: 21 additions & 0 deletions tests/test_import_msgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@
PATH = pathlib.Path(__file__).parent

SAMPLE_PACKAGE_PATH = PATH.joinpath("data/data_model/example_msgs")
CUSTOM_LICENSE_PACKAGE_PATH = PATH.joinpath(
"data/data_model/custom_license_msgs"
)
SAMPLE_PACKAGE_YAML = PATH.joinpath("data/data_model/example_msgs.yaml")
DUMMY_PATH = PATH.joinpath("data/empty_project_60")
CUSTOM_LICENSE_PATH = PATH.joinpath(
"data/data_model/custom_license_header.txt"
)
EXPECTED_DESCRIPTION_SAMPLE_CLASS_ENUM = (
"SampleClassEnum.msg "
"Properties in SampleClassEnum can reference enums in the same file. "
)

ROOT = helpers.UUIDString("00000000-0000-0000-0000-000000000000")
SA_ROOT = helpers.UUIDString("00000000-0000-0000-0000-000000000001")
Expand Down Expand Up @@ -220,3 +230,14 @@ def test_convert_package():
)

assert actual == expected


def test_custom_license_header():
importer = Importer(
CUSTOM_LICENSE_PACKAGE_PATH.as_posix(), True, CUSTOM_LICENSE_PATH
)

assert (
importer.messages.packages[0].messages[0].description
== EXPECTED_DESCRIPTION_SAMPLE_CLASS_ENUM
)

0 comments on commit ce73f58

Please sign in to comment.