Skip to content

Commit

Permalink
Update header and fix linter errors
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Schildt <[email protected]>
  • Loading branch information
SebastianSchildt committed Sep 18, 2023
1 parent 4ffb37c commit 6909d58
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 23 deletions.
41 changes: 18 additions & 23 deletions vspec/vssexporters/vss2jsonschema.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#!/usr/bin/env python3

# (c) 2023 BMW Group
#
# All files and artifacts in this repository are licensed under the
# provisions of the license provided by the LICENSE file in this repository.
# 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
#
# Convert vspec tree to JSON Schema
# Convert vspec tree to OpenAPI compatible JSON schema


from vspec.model.vsstree import VSSNode
import argparse
Expand Down Expand Up @@ -42,14 +45,14 @@
"string[]": "array"
}


def add_arguments(parser: argparse.ArgumentParser):
parser.description = "The JSON schema exporter does not support any additional arguments."



def export_node(json_dict, node, config, print_uuid):
#TODO adding json schema version might be great
#TODO check if needed, formating of jsonschema is also possible
# TODO adding json schema version might be great
# TODO check if needed, formating of jsonschema is also possible
# tags starting with $ sign are left for custom extensions and they are not part of official JSON Schema
json_dict[node.name] = {
"$VSStype": str(node.type.value),
Expand All @@ -60,7 +63,7 @@ def export_node(json_dict, node, config, print_uuid):
json_dict[node.name]["$datatype"] = node.data_type_str
json_dict[node.name]["type"] = type_map[node.data_type_str]

#TODO map types, unless we want to keep original
# TODO map types, unless we want to keep original

# many optional attributes are initialized to "" in vsstree.py
if node.min != "":
Expand All @@ -73,8 +76,8 @@ def export_node(json_dict, node, config, print_uuid):
json_dict[node.name]["default"] = node.default
if node.deprecation != "":
json_dict[node.name]["$deprecation"] = node.deprecation
if node.is_struct():
#change type to object
if node.is_struct():
# change type to object
json_dict[node.type]["type"] = "object"

# in case of unit or aggregate, the attribute will be missing
Expand All @@ -84,9 +87,9 @@ def export_node(json_dict, node, config, print_uuid):
pass
try:
json_dict[node.name]["$aggregate"] = node.aggregate
if node.aggregate == True:
#change type to object
json_dict[node.type]["type"] = "object"
if node.aggregate is True:
# change type to object
json_dict[node.type]["type"] = "object"
except AttributeError:
pass

Expand All @@ -101,7 +104,7 @@ def export_node(json_dict, node, config, print_uuid):

# Generate child nodes
if node.is_branch() or node.is_struct():
#todo if struct, type could be linked to object and then list elements as properties
# todo if struct, type could be linked to object and then list elements as properties
json_dict[node.name]["$children"] = {}
for child in node.children:
export_node(json_dict[node.name]["$children"], child, config, print_uuid)
Expand All @@ -126,11 +129,3 @@ def export(config: argparse.Namespace, signal_root: VSSNode, print_uuid, data_ty

if __name__ == "__main__":
initLogging()

parser = argparse.ArgumentParser()
add_arguments(parser)
args = parser.parse_args()

# Assuming you have the necessary variables signal_root and data_type_root to represent the vspec model.
# Call the generate_json_schema function with appropriate arguments to generate the JSON schema.
generate_json_schema(args, signal_root, print_uuid=False, data_type_root=data_type_root)
105 changes: 105 additions & 0 deletions vssexporters/vss2openapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/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
#
# Convert vspec tree to OpenAPI compatible JSON schema

import argparse
import json
from vspec.model.vsstree import VSSNode, VSSType


def add_arguments(parser: argparse.ArgumentParser):
parser.add_argument('--openapi-all-extended-attributes', action='store_true',
help="Generate all extended attributes found in the model \
(default is generating only those given by the -e/--extended-attributes parameter).")


def vss_to_openapi_datatype(vssdatatype: str):
if "int" in vssdatatype and not vssdatatype.endswith("[]"):
return "integer"
elif "int" in vssdatatype and vssdatatype.endswith("[]"):
return "integer[]"
elif vssdatatype == "float" or vssdatatype == "double":
return "number"
elif vssdatatype == "boolean":
return "boolean"
elif vssdatatype == "string":
return "string"
else:
print(f"OpenAPI warning: Do not know who to convert {vssdatatype}, passing through.")
return vssdatatype


def export_node(json_dict, node, config, print_uuid):

json_dict[node.name] = {}

if node.type == VSSType.SENSOR or node.type == VSSType.ACTUATOR or node.type == VSSType.ATTRIBUTE:
json_dict[node.name]["type"] = vss_to_openapi_datatype(str(node.datatype.value))

json_dict[node.name]["vss_type"] = str(node.type.value)

# many optional attributes are initilized to "" in vsstree.py
if node.min != "":
json_dict[node.name]["min"] = node.min
if node.max != "":
json_dict[node.name]["max"] = node.max
if node.allowed != "":
json_dict[node.name]["allowed"] = node.allowed
if node.default != "":
json_dict[node.name]["default"] = node.default
if node.deprecation != "":
json_dict[node.name]["deprecation"] = node.deprecation

# in case of unit or aggregate, the attribute will be missing
try:
json_dict[node.name]["unit"] = str(node.unit.value)
except AttributeError:
pass
try:
json_dict[node.name]["aggregate"] = node.aggregate
except AttributeError:
pass

json_dict[node.name]["description"] = node.description
if node.comment != "":
json_dict[node.name]["comment"] = node.comment

if print_uuid:
json_dict[node.name]["uuid"] = node.uuid

for k, v in node.extended_attributes.items():
if not config.json_all_extended_attributes and k not in VSSNode.whitelisted_extended_attributes:
continue
json_dict[node.name][k] = v

# Might be better to not generate child dict, if there are no children
# if node.type == VSSType.BRANCH and len(node.children) != 0:
# json_dict[node.name]["children"]={}

# But old JSON code always generates children, so lets do so to
if node.type == VSSType.BRANCH:
json_dict[node.name]["properties"] = {}

for child in node.children:
export_node(json_dict[node.name]["properties"], child, config, print_uuid)


def export(config: argparse.Namespace, root: VSSNode, print_uuid):
print("Generating OpenAPI output...")
json_dict = {}
export_node(json_dict, root, config, print_uuid)
outfile = open(config.output_file, 'w')
if config.json_pretty:
print("Serializing pretty JSON...")
json.dump(json_dict, outfile, indent=2, sort_keys=True)
else:
print("Serializing compact JSON...")
json.dump(json_dict, outfile, indent=None, sort_keys=True)

0 comments on commit 6909d58

Please sign in to comment.