diff --git a/ifex/model/ifex_ast.py b/ifex/model/ifex_ast.py index a6d8a31..5ed153c 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 0000000..0a26654 --- /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 9107aad..f2a919b 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]