From 1082656f393bc1bfbd22168cda33a08d18d31c01 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Tue, 4 Apr 2023 18:24:20 -0400 Subject: [PATCH] Allow generators to override loader paths and loaders (#25947) * Allow generators to control loader paths * Restyle * Add fs_loader_path to the example generator plugin. * Remove unused import --- .../examples/matter_idl_plugin/__init__.py | 11 ++--------- .../matter_idl/generators/__init__.py | 19 ++++++++++++++----- .../matter_idl/generators/bridge/__init__.py | 9 +++++---- .../generators/cpp/application/__init__.py | 7 ++++--- .../matter_idl/generators/java/__init__.py | 13 +++++++------ 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py b/scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py index c1783f8651d592..2339424bc7d2b1 100644 --- a/scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py +++ b/scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py @@ -14,7 +14,6 @@ import os -import jinja2 from matter_idl.generators import CodeGenerator, GeneratorStorage from matter_idl.matter_idl_types import Cluster, ClusterSide, Command, Field, Idl @@ -199,13 +198,7 @@ def __init__(self, storage: GeneratorStorage, idl: Idl, **kargs): Inintialization is specific for java generation and will add filters as required by the java .jinja templates to function. """ - super().__init__(storage, idl) - - # Override the template path to use local templates within this plugin directory - self.jinja_env = jinja2.Environment( - loader=jinja2.FileSystemLoader( - searchpath=os.path.dirname(__file__)), - keep_trailing_newline=True) + super().__init__(storage, idl, fs_loader_searchpath=os.path.dirname(__file__)) # String helpers self.jinja_env.filters['toLowerSnakeCase'] = toLowerSnakeCase @@ -241,7 +234,7 @@ def internal_render_all(self): # Header containing a macro to initialize all cluster plugins self.internal_render_one_output( - template_path="./matter_cluster_proto.jinja", + template_path="matter_cluster_proto.jinja", output_file_name=filename, vars={ 'cluster': cluster, diff --git a/scripts/py_matter_idl/matter_idl/generators/__init__.py b/scripts/py_matter_idl/matter_idl/generators/__init__.py index 0c944e68a650fd..131d8e38ccbeba 100644 --- a/scripts/py_matter_idl/matter_idl/generators/__init__.py +++ b/scripts/py_matter_idl/matter_idl/generators/__init__.py @@ -15,7 +15,7 @@ import logging import os -from typing import Dict +from typing import Dict, Optional import jinja2 from matter_idl.matter_idl_types import Idl @@ -106,16 +106,25 @@ class CodeGenerator: write time of files do not change and rebuilds are not triggered). """ - def __init__(self, storage: GeneratorStorage, idl: Idl): + def __init__(self, storage: GeneratorStorage, idl: Idl, loader: Optional[jinja2.BaseLoader] = None, fs_loader_searchpath: Optional[str] = None): """ A code generator will render a parsed IDL (a AST) into a given storage. + + Args: + storage: Storage to use to read/save data + loader: if given, use a custom loader for templates + fs_loader_searchpath: if a loader is NOT given, this controls the search path + of a default FileSystemLoader that will be used """ + if not loader: + if not fs_loader_searchpath: + fs_loader_searchpath = os.path.dirname(__file__) + loader = jinja2.FileSystemLoader(searchpath=fs_loader_searchpath) + self.storage = storage self.idl = idl self.jinja_env = jinja2.Environment( - loader=jinja2.FileSystemLoader( - searchpath=os.path.dirname(__file__)), - keep_trailing_newline=True) + loader=loader, keep_trailing_newline=True) self.dry_run = False RegisterCommonFilters(self.jinja_env.filters) diff --git a/scripts/py_matter_idl/matter_idl/generators/bridge/__init__.py b/scripts/py_matter_idl/matter_idl/generators/bridge/__init__.py index 0fcb14d44d4f2e..f48c5eac5cc080 100644 --- a/scripts/py_matter_idl/matter_idl/generators/bridge/__init__.py +++ b/scripts/py_matter_idl/matter_idl/generators/bridge/__init__.py @@ -14,6 +14,7 @@ # limitations under the License. import logging +import os import re from matter_idl.generators import CodeGenerator, GeneratorStorage @@ -137,7 +138,7 @@ def __init__(self, storage: GeneratorStorage, idl: Idl, **kargs): Inintialization is specific for cpp generation and will add filters as required by the cpp .jinja templates to function. """ - super().__init__(storage, idl) + super().__init__(storage, idl, fs_loader_searchpath=os.path.dirname(__file__)) self.jinja_env.filters['getType'] = get_attr_type self.jinja_env.filters['getRawSizeAndType'] = get_raw_size_and_type @@ -163,7 +164,7 @@ def internal_render_all(self): output_file_name = "bridge/%s.h" % cluster.name self.internal_render_one_output( - template_path="bridge/BridgeClustersCpp.jinja", + template_path="BridgeClustersCpp.jinja", output_file_name=output_file_name, vars={ 'cluster': cluster, @@ -172,7 +173,7 @@ def internal_render_all(self): ) self.internal_render_one_output( - template_path="bridge/BridgeClustersCommon.jinja", + template_path="BridgeClustersCommon.jinja", output_file_name="bridge/BridgeClustersImpl.h", vars={ 'clusters': self.idl.clusters, @@ -181,7 +182,7 @@ def internal_render_all(self): ) self.internal_render_one_output( - template_path="bridge/BridgeClustersGlobalStructs.jinja", + template_path="BridgeClustersGlobalStructs.jinja", output_file_name="bridge/BridgeGlobalStructs.h", vars={ 'idl': self.idl, diff --git a/scripts/py_matter_idl/matter_idl/generators/cpp/application/__init__.py b/scripts/py_matter_idl/matter_idl/generators/cpp/application/__init__.py index 5cc22583a87192..2b9158864d28e4 100644 --- a/scripts/py_matter_idl/matter_idl/generators/cpp/application/__init__.py +++ b/scripts/py_matter_idl/matter_idl/generators/cpp/application/__init__.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os from typing import List from matter_idl.generators import CodeGenerator, GeneratorStorage @@ -32,7 +33,7 @@ def __init__(self, storage: GeneratorStorage, idl: Idl, **kargs): Inintialization is specific for java generation and will add filters as required by the java .jinja templates to function. """ - super().__init__(storage, idl) + super().__init__(storage, idl, fs_loader_searchpath=os.path.dirname(__file__)) self.jinja_env.filters['serverClustersOnly'] = serverClustersOnly @@ -43,7 +44,7 @@ def internal_render_all(self): # Header containing a macro to initialize all cluster plugins self.internal_render_one_output( - template_path="cpp/application/PluginApplicationCallbacksHeader.jinja", + template_path="PluginApplicationCallbacksHeader.jinja", output_file_name="app/PluginApplicationCallbacks.h", vars={ 'clusters': self.idl.clusters, @@ -53,7 +54,7 @@ def internal_render_all(self): # Source for __attribute__(weak) implementations of all cluster # initialization methods self.internal_render_one_output( - template_path="cpp/application/CallbackStubSource.jinja", + template_path="CallbackStubSource.jinja", output_file_name="app/callback-stub.cpp", vars={ 'clusters': self.idl.clusters, diff --git a/scripts/py_matter_idl/matter_idl/generators/java/__init__.py b/scripts/py_matter_idl/matter_idl/generators/java/__init__.py index bd641f82a2be59..0b21947cc809fc 100644 --- a/scripts/py_matter_idl/matter_idl/generators/java/__init__.py +++ b/scripts/py_matter_idl/matter_idl/generators/java/__init__.py @@ -15,6 +15,7 @@ import enum import logging +import os from typing import List, Set, Union from matter_idl.generators import CodeGenerator, GeneratorStorage @@ -405,7 +406,7 @@ def __init__(self, storage: GeneratorStorage, idl: Idl, **kargs): Inintialization is specific for java generation and will add filters as required by the java .jinja templates to function. """ - super().__init__(storage, idl) + super().__init__(storage, idl, fs_loader_searchpath=os.path.dirname(__file__)) self.jinja_env.filters['attributesWithCallback'] = attributesWithSupportedCallback self.jinja_env.filters['callbackName'] = CallbackName @@ -434,7 +435,7 @@ def internal_render_all(self): """ self.internal_render_one_output( - template_path="java/CHIPCallbackTypes.jinja", + template_path="CHIPCallbackTypes.jinja", output_file_name="jni/CHIPCallbackTypes.h", vars={ 'idl': self.idl, @@ -449,7 +450,7 @@ def internal_render_all(self): continue self.internal_render_one_output( - template_path="java/ChipClustersRead.jinja", + template_path="ChipClustersRead.jinja", output_file_name="jni/%sClient-ReadImpl.cpp" % cluster.name, vars={ 'cluster': cluster, @@ -458,7 +459,7 @@ def internal_render_all(self): ) self.internal_render_one_output( - template_path="java/ChipClustersCpp.jinja", + template_path="ChipClustersCpp.jinja", output_file_name="jni/%sClient-InvokeSubscribeImpl.cpp" % cluster.name, vars={ 'cluster': cluster, @@ -482,7 +483,7 @@ def internal_render_all(self): c for c in self.idl.clusters if c.side == ClusterSide.CLIENT] self.internal_render_one_output( - template_path="java/ClusterReadMapping.jinja", + template_path="ClusterReadMapping.jinja", output_file_name="java/chip/devicecontroller/ClusterReadMapping.java", vars={ 'idl': self.idl, @@ -491,7 +492,7 @@ def internal_render_all(self): ) self.internal_render_one_output( - template_path="java/ClusterWriteMapping.jinja", + template_path="ClusterWriteMapping.jinja", output_file_name="java/chip/devicecontroller/ClusterWriteMapping.java", vars={ 'idl': self.idl,