-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring tool structure so that there is not a single entrypoint (vspec2x.py) but rather individual ones. Makes it easier to add custom generators. Signed-off-by: Erik Jaegervall <[email protected]>
- Loading branch information
Showing
43 changed files
with
886 additions
and
798 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
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,66 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2023 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 | ||
|
||
import sys | ||
import logging | ||
from vspec.model.vsstree import VSSNode | ||
import argparse | ||
from typing import Optional | ||
from vspec.vss2x import Vss2X | ||
from vspec.vspec2vss_config import Vspec2VssConfig | ||
from vspec.vspec2x import Vspec2X | ||
|
||
|
||
class ExampleGenerator(Vss2X): | ||
""" | ||
This is an example on how easy you can write your own generator | ||
""" | ||
|
||
def __init__(self, vspec2vss_config: Vspec2VssConfig, keyword: str): | ||
# Change default configs | ||
# By default Vspec2X requires that an output file is needed and specified | ||
# That is not needed for the ExampleGenerator as it just use stdout | ||
# By that reason we change default config to indicate that no output arguments is needed | ||
# That also implies that there will be an error if an output argument is specified | ||
vspec2vss_config.output_file_required = False | ||
|
||
# A lot of other things we do not care about, this reduces number or arguments shown | ||
# if you do "./example_generator.py --help" | ||
vspec2vss_config.uuid_supported = False | ||
vspec2vss_config.extended_attributes_supported = False | ||
vspec2vss_config.type_tree_supported = False | ||
|
||
# The rest here is generator-specific initializations | ||
self.keyword = str.lower(keyword) | ||
self.count = 0 | ||
|
||
def handle_node(self, node: VSSNode): | ||
if self.keyword in str.lower(node.comment): | ||
self.count += 1 | ||
for child in node.children: | ||
self.handle_node(child) | ||
|
||
def generate(self, config: argparse.Namespace, signal_root: VSSNode, vspec2vss_config: Vspec2VssConfig, | ||
data_type_root: Optional[VSSNode] = None) -> None: | ||
""" | ||
It is required to implement the generate method | ||
""" | ||
self.handle_node(signal_root) | ||
|
||
logging.info("Generating Example output...") | ||
logging.info("I found %d comments with %s", self.count, self.keyword) | ||
|
||
|
||
if __name__ == "__main__": | ||
vspec2vss_config = Vspec2VssConfig() | ||
# The generator shall know nothing about vspec processing or vspec2vss arguments! | ||
# (Even if it may have some expectations on how the model look like) | ||
generator = ExampleGenerator(vspec2vss_config, "VSS contributor") | ||
vspec2x = Vspec2X(generator, vspec2vss_config) | ||
vspec2x.main(sys.argv[1:]) |
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,34 @@ | ||
# Copyright (c) 2023 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: | ||
type: branch | ||
description: Branch A. | ||
|
||
############ Testing Single Line Comments ############## | ||
|
||
A.Erik: | ||
datatype: float | ||
type: sensor | ||
unit: km | ||
description: A sensor. | ||
comment: A VSS contributor! | ||
|
||
A.Sebastian: | ||
datatype: float | ||
type: sensor | ||
unit: km | ||
description: A sensor. | ||
comment: A VSS contributor! | ||
|
||
A.ChuckNorris: | ||
datatype: float | ||
type: sensor | ||
unit: km | ||
description: A sensor. | ||
comment: An actor! |
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,30 @@ | ||
# Copyright (c) 2023 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 | ||
|
||
import pytest | ||
import os | ||
|
||
|
||
@pytest.fixture | ||
def change_test_dir(request, monkeypatch): | ||
# To make sure we run from test directory | ||
monkeypatch.chdir(request.fspath.dirname) | ||
|
||
|
||
def test_generator(change_test_dir): | ||
|
||
test_str = "./example_generator.py -u ../vspec/test_units.yaml test.vspec > out.txt 2>&1" | ||
result = os.system(test_str) | ||
assert os.WIFEXITED(result) | ||
assert os.WEXITSTATUS(result) == 0 | ||
|
||
test_str = 'grep \"I found 2 comments with vss contributor\" out.txt > /dev/null' | ||
result = os.system(test_str) | ||
assert os.WIFEXITED(result) | ||
assert os.WEXITSTATUS(result) == 0 | ||
os.system("rm -f out.txt") |
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.