Skip to content

Commit

Permalink
vsc_ast: bring back AST root node
Browse files Browse the repository at this point in the history
After short discussion we have decided to bring back the AST node.
This node is always a root, but inherits everything from the Namespace class.

And vsc_parser.gen() function now accepts object of any type. Later we
might want to introduce some checks for the passed node, like
assert dataclass.is_dataclass(node).

Signed-off-by: Mikhail Tsukerman <mikhail.tcukerman@daimler.com>
Signed-off-by: Magnus Feuer <magnus.feuer@mercedes-benz.com>
  • Loading branch information
miketsukerman authored and gunnarx committed Sep 21, 2022
1 parent d45cfd6 commit 65b527c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
10 changes: 5 additions & 5 deletions tests/gen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def test_ast_gen():


def test_ast_manual():
namespace = vsc_ast.Namespace(name='test', description='test', major_version=1, minor_version=0)
service = vsc_ast.AST(name='test', description='test', major_version=1, minor_version=0)

assert namespace.name == 'test'
assert namespace.description == 'test'
assert namespace.major_version == 1
assert namespace.minor_version == 0
assert service.name == 'test'
assert service.description == 'test'
assert service.major_version == 1
assert service.minor_version == 0


# Unused
Expand Down
8 changes: 6 additions & 2 deletions vsc/model/vsc_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ class Namespace:

namespaces: Optional[List['Namespace']] = field(default_factory=lambda: [])

@dataclass
class AST(Namespace):
pass


def read_yaml_file(filename) -> str:
"""
Expand All @@ -135,7 +139,7 @@ def parse_yaml_file(yaml_string: str) -> Dict[Any, Any]:
return yaml.safe_load(yaml_string)


def read_ast_from_yaml_file(filename: str) -> Namespace:
def read_ast_from_yaml_file(filename: str) -> AST:
"""
Reads a yaml file and returns AST
:param filename: path to a yaml file
Expand All @@ -146,6 +150,6 @@ def read_ast_from_yaml_file(filename: str) -> Namespace:

yaml_dict = parse_yaml_file(yaml_string)

ast = dacite.from_dict(data_class=Namespace, data=yaml_dict)
ast = dacite.from_dict(data_class=AST, data=yaml_dict)

return ast
13 changes: 7 additions & 6 deletions vsc/model/vsc_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

# This performs the following related functions:

# 1. As input.yaml we expect an Namespace as provided by the parser module
# 1. As input.yaml we expect an AST as provided by the parser module
#
# 2. It uses jinja2 templating library to generate code or configuration
# according to given templates.

# For other features from parser module
from vsc.model import vsc_ast
from vsc.model.vsc_ast import Namespace
from vsc.templates import TemplatePath
from typing import Generic, TypeVar
import jinja2
import sys

Expand All @@ -44,6 +44,7 @@

default_templates = {}

T = TypeVar('T')

# Exception:
class GeneratorError(BaseException):
Expand All @@ -58,7 +59,7 @@ def get_template(filename):
return jinja_env.get_template(filename)

# gen() function to be called from templates
def gen(node : Namespace, template_file = None):
def gen(node : Generic[T], template_file = None):
# Processing of lists of objects?

if node is None:
Expand All @@ -80,7 +81,7 @@ def gen(node : Namespace, template_file = None):
raise GeneratorError(f'Wrong use of gen() function! Usage: pass the node as first argument (you passed a {type(node)}), and optionally template name (str) as second argument. (You passed a {template_file.__name__})')

# gen helper function:
def _gen_with_default_template(node : Namespace):
def _gen_with_default_template(node : Generic[T]):

# None is for a field that exists in Namespace definition, but was not given
# a value in the YAML (=> happens only if it was an optional item).
Expand Down Expand Up @@ -111,7 +112,7 @@ def _gen_with_default_template(node : Namespace):

# Instead of providing a template file, provide the template text itself
# (for unit tests mostly). See gen() for more comments/explanation.
def gen_template_text(node: Namespace, template_text: str):
def gen_template_text(node: Generic[T], template_text: str):
# Processing of lists of objects, see gen() for explanation
if type(node) == list or type(node) == tuple:
return [gen_template_text(x, template_text) for x in node]
Expand Down Expand Up @@ -139,7 +140,7 @@ def usage():
# This is a default definition for our current generation tests.
# It may need to be changed, or defined differently in a custom generator
default_templates = {
'Namespace': 'Namespace-simple_doc.tpl',
'AST': 'AST-simple_doc.tpl',
'Service': 'Service-simple_doc.html',
'Argument': 'Argument-simple_doc.html',
'Error': 'Error-simple_doc.html',
Expand Down

0 comments on commit 65b527c

Please sign in to comment.