From aeed7753a4c5f8b8c540a398ba7b204d52a7d432 Mon Sep 17 00:00:00 2001 From: Gunnar Andersson Date: Thu, 12 Oct 2023 10:31:03 +0200 Subject: [PATCH] ifex_ast: Simplify, add get_ast_node_type_names() function 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 --- ifex/model/ifex_ast.py | 21 ++++++++++++--------- ifex/templates/IFEX/README.md | 8 ++++++++ ifex/templates/TemplateDir.py | 25 +------------------------ 3 files changed, 21 insertions(+), 33 deletions(-) create mode 100644 ifex/templates/IFEX/README.md diff --git a/ifex/model/ifex_ast.py b/ifex/model/ifex_ast.py index a6d8a316..5ed153cf 100644 --- a/ifex/model/ifex_ast.py +++ b/ifex/model/ifex_ast.py @@ -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: diff --git a/ifex/templates/IFEX/README.md b/ifex/templates/IFEX/README.md new file mode 100644 index 00000000..0a26654a --- /dev/null +++ b/ifex/templates/IFEX/README.md @@ -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. diff --git a/ifex/templates/TemplateDir.py b/ifex/templates/TemplateDir.py index 9107aad2..f2a919bd 100644 --- a/ifex/templates/TemplateDir.py +++ b/ifex/templates/TemplateDir.py @@ -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: @@ -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]