diff --git a/capella_ros_tools/.license_header.txt b/capella_ros_tools/.license_header.txt new file mode 100644 index 0000000..8c17559 --- /dev/null +++ b/capella_ros_tools/.license_header.txt @@ -0,0 +1,2 @@ +# SPDX-FileCopyrightText: Copyright DB Netz AG +# SPDX-License-Identifier: Apache-2.0 diff --git a/capella_ros_tools/data_model.py b/capella_ros_tools/data_model.py index 8420e50..03d96c6 100644 --- a/capella_ros_tools/data_model.py +++ b/capella_ros_tools/data_model.py @@ -12,6 +12,11 @@ from capellambse.filehandler import abc +LICENSE_HEADER = ( + pathlib.Path(__file__) + .parent.joinpath(".license_header.txt") + .read_text(encoding="utf-8") +) PACKAGE_NAME_MESSAGE_TYPE_SEPARATOR = "/" COMMENT_DELIMITER = "#" CONSTANT_SEPARATOR = "=" @@ -214,6 +219,7 @@ def from_file( """Create message definition from a .msg file.""" msg_name = file.stem msg_string = file.read_text() + msg_string = msg_string.removeprefix(LICENSE_HEADER) return cls.from_string(msg_name, msg_string) @classmethod diff --git a/tests/data/data_model/example_msgs/package1/msg/SampleClass.msg b/tests/data/data_model/example_msgs/package1/msg/SampleClass.msg index c5420a7..74d72af 100644 --- a/tests/data/data_model/example_msgs/package1/msg/SampleClass.msg +++ b/tests/data/data_model/example_msgs/package1/msg/SampleClass.msg @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: Copyright DB Netz AG +# SPDX-License-Identifier: Apache-2.0 + # SampleClass.msg # The first comment block at the top of the file # is added to the class description of SampleClass. diff --git a/tests/data/data_model/example_msgs/package1/msg/SampleClass.msg.license b/tests/data/data_model/example_msgs/package1/msg/SampleClass.msg.license deleted file mode 100644 index 62a1749..0000000 --- a/tests/data/data_model/example_msgs/package1/msg/SampleClass.msg.license +++ /dev/null @@ -1,2 +0,0 @@ -SPDX-FileCopyrightText: Copyright DB InfraGO AG -SPDX-License-Identifier: Apache-2.0 diff --git a/tests/data/data_model/example_msgs/package1/msg/types/SampleEnum.msg b/tests/data/data_model/example_msgs/package1/msg/types/SampleEnum.msg index 14f42a8..5ea8f22 100644 --- a/tests/data/data_model/example_msgs/package1/msg/types/SampleEnum.msg +++ b/tests/data/data_model/example_msgs/package1/msg/types/SampleEnum.msg @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: Copyright DB Netz AG +# SPDX-License-Identifier: Apache-2.0 + # SampleEnum.msg # This block comment is added to the # enum description of SampleEnumValue. diff --git a/tests/data/data_model/example_msgs/package1/msg/types/SampleEnum.msg.license b/tests/data/data_model/example_msgs/package1/msg/types/SampleEnum.msg.license deleted file mode 100644 index 62a1749..0000000 --- a/tests/data/data_model/example_msgs/package1/msg/types/SampleEnum.msg.license +++ /dev/null @@ -1,2 +0,0 @@ -SPDX-FileCopyrightText: Copyright DB InfraGO AG -SPDX-License-Identifier: Apache-2.0 diff --git a/tests/data/data_model/example_msgs/package2/msg/SampleClassEnum.msg b/tests/data/data_model/example_msgs/package2/msg/SampleClassEnum.msg index 59a2911..60b0c13 100644 --- a/tests/data/data_model/example_msgs/package2/msg/SampleClassEnum.msg +++ b/tests/data/data_model/example_msgs/package2/msg/SampleClassEnum.msg @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: Copyright DB Netz AG +# SPDX-License-Identifier: Apache-2.0 + # SampleClassEnum.msg # Properties in SampleClassEnum can reference # enums in the same file. diff --git a/tests/data/data_model/example_msgs/package2/msg/SampleClassEnum.msg.license b/tests/data/data_model/example_msgs/package2/msg/SampleClassEnum.msg.license deleted file mode 100644 index 62a1749..0000000 --- a/tests/data/data_model/example_msgs/package2/msg/SampleClassEnum.msg.license +++ /dev/null @@ -1,2 +0,0 @@ -SPDX-FileCopyrightText: Copyright DB InfraGO AG -SPDX-License-Identifier: Apache-2.0 diff --git a/tests/test_data_model.py b/tests/test_data_model.py index 50b0c10..957d7f3 100644 --- a/tests/test_data_model.py +++ b/tests/test_data_model.py @@ -158,10 +158,10 @@ def test_extract_file_level_comments_no_comments(): @staticmethod def test_extract_file_level_comments_no_newline(): msg_string = """# This is a comment - # This is another comment - uint8 OK = 0 - uint8 WARN = 1 - uint8 ERROR = 2""" +# This is another comment +uint8 OK = 0 +uint8 WARN = 1 +uint8 ERROR = 2""" comments, _ = data_model._extract_file_level_comments(msg_string) assert comments == "" @@ -169,11 +169,11 @@ def test_extract_file_level_comments_no_newline(): @staticmethod def test_extract_file_level_comments(): msg_string = """# This is a comment - # This is another comment +# This is another comment - uint8 OK = 0 - uint8 WARN = 1 - uint8 ERROR = 2""" +uint8 OK = 0 +uint8 WARN = 1 +uint8 ERROR = 2""" comments, _ = data_model._extract_file_level_comments(msg_string) expected = "This is a comment This is another comment " @@ -189,6 +189,20 @@ def test_extract_file_level_comments_with_newline(): assert comments == expected + @staticmethod + def test_extract_file_level_comments_strip_empty_lines_at_top(): + msg_string = """ + +# This is a comment + +uint8 OK = 0 +uint8 WARN = 1 +uint8 ERROR = 2""" + comments, _ = data_model._extract_file_level_comments(msg_string) + expected = "This is a comment " + + assert comments == expected + @staticmethod def test_parse_comments_no_comments(): msg_string = """uint8 field"""