Skip to content

Commit

Permalink
Update Formatting and fix few linting issues
Browse files Browse the repository at this point in the history
Signed-off-by: Adnan Bekan <[email protected]>
  • Loading branch information
adobekan committed Sep 20, 2023
1 parent d900491 commit 978de45
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions vspec/vssexporters/vss2jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
#
# Convert vspec tree compatible JSON schema


from vspec.model.vsstree import VSSNode
import argparse
import json
import logging
from typing import Dict, Any
from vspec.model.vsstree import VSSNode
from vspec.loggingconfig import initLogging

type_map = {
Expand Down Expand Up @@ -47,25 +46,24 @@


def add_arguments(parser: argparse.ArgumentParser):
"""Check for input arguments."""
parser.add_argument('--jsonschema-all-extended-attributes', action='store_true',
help="Generate all extended attributes found in the model. Should not be used with strict mode JSON Schema validators."
"(default is generating only those given by the -e/--extended-attributes parameter).")
help="Generate all extended attributes found in the model."
"Should not be used with strict mode JSON Schema validators.")
parser.add_argument('--jsonschema-pretty', action='store_true',
help=" Pretty print JSON Schema output.")


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
# tags starting with $ sign are left for custom extensions and they are not part of official JSON Schema
"""Preparing nodes for JSON schema output."""
# keyword with X- sign are left for extensions and they are not part of official JSON schema
json_dict[node.name] = {
"description": node.description,
}

if node.is_signal() or node.is_property():
json_dict[node.name]["type"] = type_map[node.data_type_str]

# TODO map types, unless we want to keep original

# many optional attributes are initialized to "" in vsstree.py
if node.min != "":
json_dict[node.name]["minimum"] = node.min
Expand All @@ -80,23 +78,23 @@ def export_node(json_dict, node, config, print_uuid):
json_dict[node.type]["type"] = "object"

if config.jsonschema_all_extended_attributes:
if node.type !="":
json_dict[node.name]["x-VSStype"] = str(node.type.value)
if node.data_type_str !="":
if node.type != "":
json_dict[node.name]["x-VSStype"] = str(node.type.value)
if node.data_type_str != "":
json_dict[node.name]["x-datatype"] = node.data_type_str
if node.deprecation != "":
json_dict[node.name]["x-deprecation"] = node.deprecation

# in case of unit or aggregate, the attribute will be missing
try:
json_dict[node.name]["x-unit"] = str(node.unit.value)
except AttributeError:
pass
try:
json_dict[node.name]["x-aggregate"] = node.aggregate
if node.aggregate == True:
#change type to object
json_dict[node.type]["type"] = "object"
if node.aggregate:
# change type to object
json_dict[node.type]["type"] = "object"
except AttributeError:
pass

Expand All @@ -111,13 +109,13 @@ 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
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, signal_root: VSSNode, print_uuid, data_type_root: VSSNode):
"""Export function for generating JSON schema file."""
logging.info("Generating JSON schema...")
indent = None
if config.jsonschema_pretty:
Expand All @@ -133,16 +131,16 @@ def export(config: argparse.Namespace, signal_root: VSSNode, print_uuid, data_ty
export_node(data_types_json_schema, data_type_root, config, print_uuid)
if config.jsonschema_all_extended_attributes:
signals_json_schema["x-ComplexDataTypes"] = data_types_json_schema

top_node = signals_json_schema.pop("Vehicle")
# Create a new JSON Schema object
json_schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Vehicle",
"type": "object",
**top_node}
with open(config.output_file, 'w') as f:
json.dump(json_schema, f, indent=indent, sort_keys=False)
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Vehicle",
"type": "object",
**top_node}
with open(config.output_file, 'w', encoding="utf-8") as output_file:
json.dump(json_schema, output_file, indent=indent, sort_keys=False)


if __name__ == "__main__":
Expand Down

0 comments on commit 978de45

Please sign in to comment.