Skip to content

Commit

Permalink
ifex_ast: Simplify, add get_ast_node_type_names() function
Browse files Browse the repository at this point in the history
This simplifies the implementation of TemplateDir so that it can
easily get the node names to match them against template names.
(the walk function was clearly overkill for this task).

Signed-off-by: Gunnar Andersson <gunnar_dev@[email protected]>
  • Loading branch information
Gunnar Andersson authored and gunnar-mb committed Jun 18, 2024
1 parent 474e9f2 commit aeed775
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
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

0 comments on commit aeed775

Please sign in to comment.