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

Draft: ifex_ast: Simplify, add get_ast_node_type_names() function #119

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions ifex/model/ifex_ast.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
# SPDX-FileCopyrightText: Copyright (c) 2022 MBition GmbH.
# SPDX-License-Identifier: MPL-2.0

# Module contains Vehicle Service Catalog abstract syntax tree
# implemented using python dataclasses.
#
# The specification can be found here, but the rules will be generated from
# this file. This file is the formal definition of the language.
# https://github.com/COVESA/vehicle_service_catalog/blob/master/syntax.md

from dataclasses import dataclass, field
from typing import List, Optional, Any

import inspect, sys

# shortcut to reduce code duplication for default_factory
# parameter in field()
def EmptyList():
return []


# Module contains Vehicle Service Catalog abstract syntax tree
# implemented using python dataclasses.
#
# The specification can be found here, but the rules will be generated from
# this file. This file is the formal definition of the language.
# https://github.com/COVESA/vehicle_service_catalog/blob/master/syntax.md

# This small helper function returns a list of all the dataclass names -
# in other words all the AST node types. There are no other classes in this file.
def get_ast_node_type_names():
return [x[0] for x in inspect.getmembers(sys.modules[__name__], inspect.isclass)]

@dataclass
class Argument:
Expand Down
8 changes: 8 additions & 0 deletions ifex/templates/IFEX/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# templates/IFEX

This directory contains templates that generate IFEX - in other words probably
used in translators from another format to IFEX. Other template directories
usually generate other formats _from_ IFEX.

Specifically, these templates are likely to generate IFEX Core IDL, but some of
them may be designed to generate other Layer Types.
25 changes: 1 addition & 24 deletions ifex/templates/TemplateDir.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,12 @@
# Needed globally by setup.py
TemplatePath = os.path.dirname(os.path.realpath(__file__))

# NOTE: Common module variable. We're not expecting this module to have
# concurrent access from more than one. If that changes, re-wrap this in a
# class so that we use instance methods and instance variables instead!
classes_list = set()

def find_matching_template_files(directory : str, recurse = False, absolute = False):
"""Search in given directory to find any files that match (start with) any of the given class names.
Return a dict that maps each class name to its corresponding template file name.
If there is more than one matching file, the last found one will remain in result."""

# Reuse walk_type_tree() from documentation generator, which gives us each of the
# node classes in the ifex AST definition. More correctly, its like the
# visitor pattern, so the function we provide (collector) will be called once
# for each found class.

# clear variable for collector => function can be called more than once (but not concurrently!)
global classes_list
classes_list = set()
ifex_ast_doc.walk_type_tree(ifex_ast.AST, collector, {})

# Now all classes are collected in the classes_list variable
classes = classes_list
classes = ifex_ast.get_ast_node_type_names()

templates = {} # Dict maps node name to the template file name
if recurse:
Expand Down Expand Up @@ -58,13 +42,6 @@ def abs_template_path(p):
# Relative to this file (i.e. template root)
return os.path.join(os.path.dirname(os.path.realpath(__file__)), p)

# collector: Small helper that grabs each output from the walk function,
# and in the process converting each class object to its name string
# Reusing type_name() from ifex_ast_doc takes care of ForwardRef type correctly.
def collector(x):
global classes_list
classes_list.add(ifex_ast_doc.type_name(x))

# Some test code - print out result from template-dir as arg
if __name__ == '__main__':
template_dir = sys.argv[1]
Expand Down
Loading