Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add const UID attribute to allow constant IDs #352

Merged
merged 11 commits into from
May 17, 2024
54 changes: 46 additions & 8 deletions tests/vspec/test_static_uids/test_static_uids.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,24 @@
# HELPERS


def get_cla_test(test_file: str):
return (
"../../../vspec2id.py "
+ test_file
+ " ./out.vspec --validate-static-uid "
+ "./validation_vspecs/validation.vspec "
+ "--only-validate-no-export"
)
def get_cla_test(test_file: str, overlay: str | None = None):
if overlay:
return (
"../../../vspec2id.py "
+ test_file
+ " -o "
+ overlay
+ " ./out.vspec --validate-static-uid "
+ "./validation_vspecs/validation.vspec "
)
else:
return (
"../../../vspec2id.py "
+ test_file
+ " ./out.vspec --validate-static-uid "
+ "./validation_vspecs/validation.vspec "
+ "--only-validate-no-export"
Copy link
Collaborator

@erikbosch erikbosch May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me it is not obvious why you have "--only-validate-no-export" here but not for the overlay alternative. Possibly add a comment.

Updated:

Now I noticed that in one of the overlay tests we actually check output. I suggest that we remove --only-validate-no-export here as well so that the two variants behave in the same way, i.e. both generate files. Or have a separate option to control that.

)


def get_cla_validation(validation_file: str):
Expand Down Expand Up @@ -341,3 +351,31 @@ def test_deleted_attribute(caplog: pytest.LogCaptureFixture):
)
for record in caplog.records:
assert "DELETED ATTRIBUTE" in record.msg


@pytest.mark.usefixtures("change_test_dir")
def test_overlay(caplog: pytest.LogCaptureFixture):
test_file: str = "./test_vspecs/test.vspec"
overlay: str = "./test_vspecs/test_overlay.vspec"
clas = shlex.split(get_cla_test(test_file, overlay))
vspec2id.main(clas[1:])

assert len(caplog.records) == 1 and all(
log.levelname == "WARNING" for log in caplog.records
)

for record in caplog.records:
assert "ADDED ATTRIBUTE" in record.msg


@pytest.mark.usefixtures("change_test_dir")
def test_const_id(caplog: pytest.LogCaptureFixture):
test_file: str = "./test_vspecs/test.vspec"
overlay: str = "./test_vspecs/test_const_id.vspec"
clas = shlex.split(get_cla_test(test_file, overlay))
vspec2id.main(clas[1:])

result = yaml.load(open("./out.vspec"), Loader=yaml.FullLoader)
for key, value in get_all_keys_values(result):
if key == "A.B.Int32":
assert value["staticUID"] == "0x00112233"
12 changes: 12 additions & 0 deletions tests/vspec/test_static_uids/test_vspecs/test_const_id.vspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024 Contributors to COVESA
#
# This program and the accompanying materials are made available under the
# terms of the Mozilla Public License 2.0 which is available at
# https://www.mozilla.org/en-US/MPL/2.0/
#
# SPDX-License-Identifier: MPL-2.0

A.B.Int32:
datatype: int32
type: sensor
constUID: '0x00112233'
12 changes: 12 additions & 0 deletions tests/vspec/test_static_uids/test_vspecs/test_overlay.vspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024 Contributors to COVESA
#
# This program and the accompanying materials are made available under the
# terms of the Mozilla Public License 2.0 which is available at
# https://www.mozilla.org/en-US/MPL/2.0/
#
# SPDX-License-Identifier: MPL-2.0

A.B.OverlayNode:
datatype: int32
type: sensor
description: This is the description of the overlay node.
4 changes: 3 additions & 1 deletion vspec/model/vsstree.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class VSSNode(Node):
"$file_name$",
"fka",
"delete",
"constUID",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should discuss how integrated we want the id-attributes to be in vss-tools. By adding constUID (and previously fka) they will be considered as core attributes and accepted in all tools.

There are two alternatives, one is to consider them as extended attributes and give warning unless they are specified by -e, the other is to refactor vss-tools so that each tool can add their own core attributes. Or alternatively that we consider them as "global core attributes", but then we should possibly mention them in https://github.com/COVESA/vss-tools/blob/master/docs/vspec2x_arch.md

Copy link
Contributor Author

@nwesem nwesem May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikbosch can you please discuss this during the public meeting today? I have been sick yesterday / today, but can finish this tomorrow if you decide on how you want it to work. I would appreciate it, thank you!!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets keep this for now, just add some documentation, then I can do a quick look and if ok I merge

]

# List of accepted extended attributes. In strict terminate if an attribute is
Expand All @@ -90,6 +91,7 @@ class VSSNode(Node):
deprecation = ""
fka = ""
delete: bool = False
constUID: str | None = None

def __deepcopy__(self, memo):
# Deep copy of source_dict and children needed as overlay or programmatic changes
Expand Down Expand Up @@ -252,7 +254,7 @@ def validate_name_style(self, sourcefile):
)
)

camel_regexp = re.compile('[A-Z][A-Za-z0-9]*$')
camel_regexp = re.compile("[A-Z][A-Za-z0-9]*$")
# relax camel case requirement for struct properties
if not self.is_property() and not camel_regexp.match(self.name):
raise NameStyleValidationException(
Expand Down
33 changes: 25 additions & 8 deletions vspec/vssexporters/vss2id.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@
from vspec.model.constants import VSSTreeType
from vspec.model.vsstree import VSSNode
from vspec.utils import vss2id_val
from vspec.utils.idgen_utils import (fnv1_32_hash, get_all_keys_values,
get_node_identifier_bytes)
from vspec.utils.idgen_utils import (
fnv1_32_hash,
get_all_keys_values,
get_node_identifier_bytes,
)
from vspec.vss2x import Vss2X
from vspec.vspec2vss_config import Vspec2VssConfig


def generate_split_id(
node: VSSNode, id_counter: int, strict_mode: bool
) -> Tuple[str, int]:

"""Generates static UIDs using 4-byte FNV-1 hash.

@param node: VSSNode that we want to generate a static UID for
Expand Down Expand Up @@ -68,7 +70,15 @@ def export_node(yaml_dict, node, id_counter, strict_mode: bool) -> Tuple[int, in
@param strict_mode: strict mode means case sensitivity for static UID generation
@return: id_counter, id_counter
"""
node_id, id_counter = generate_split_id(node, id_counter, strict_mode)

if not node.constUID:
node_id, id_counter = generate_split_id(node, id_counter, strict_mode)
else:
assert node.constUID.startswith(
"0x"
), f"constUID has to begin with '0x': {node.constUID}"
assert len(node.constUID) == 10, f"Invalid constUID: {node.constUID}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am considering to move these two assert statements to a utlils function and also run them on the generation of UIDs as an additional safety measure..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a better way to solve this here f46dd0c

node_id = node.constUID[2:]

# check for hash duplicates
for key, value in get_all_keys_values(yaml_dict):
Expand Down Expand Up @@ -121,7 +131,10 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
@param parser: the pre-existing argument parser
"""
parser.add_argument(
"--validate-static-uid", type=str, default="", help="Path to validation file."
"--validate-static-uid",
type=str,
default="",
help="Path to validation file.",
)
parser.add_argument(
"--only-validate-no-export",
Expand All @@ -135,9 +148,13 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
help="Strict mode means that the generation of static UIDs is case-sensitive.",
)

def generate(self, config: argparse.Namespace, signal_root: VSSNode, vspec2vss_config: Vspec2VssConfig,
data_type_root: Optional[VSSNode] = None) -> None:

def generate(
self,
config: argparse.Namespace,
signal_root: VSSNode,
vspec2vss_config: Vspec2VssConfig,
data_type_root: Optional[VSSNode] = None,
) -> None:
"""Main export function used to generate the output id vspec.

@param config: Command line arguments it was run with
Expand Down