From 9a65c8e3b7b14f610556f5dd55f6cdbb1480b89e Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 3 Apr 2023 11:53:53 -0400 Subject: [PATCH 1/4] Allow generators to control loader paths --- .../matter_idl/generators/__init__.py | 20 +++++++++++++------ .../matter_idl/generators/bridge/__init__.py | 9 +++++---- .../generators/cpp/application/__init__.py | 7 ++++--- .../matter_idl/generators/java/__init__.py | 13 ++++++------ 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/scripts/py_matter_idl/matter_idl/generators/__init__.py b/scripts/py_matter_idl/matter_idl/generators/__init__.py index 0c944e68a650fd..39252385139d3a 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,24 @@ 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) + self.jinja_env = jinja2.Environment(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..09776fbc3c274c 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 @@ -13,6 +13,7 @@ # limitations under the License. from typing import List +import os from matter_idl.generators import CodeGenerator, GeneratorStorage from matter_idl.matter_idl_types import Cluster, ClusterSide, Idl @@ -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, From 7da28461ca9d24bbe3269c77b0a9d4b10ba27314 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 3 Apr 2023 11:57:36 -0400 Subject: [PATCH 2/4] Restyle --- scripts/py_matter_idl/matter_idl/generators/__init__.py | 7 ++++--- .../matter_idl/generators/cpp/application/__init__.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/py_matter_idl/matter_idl/generators/__init__.py b/scripts/py_matter_idl/matter_idl/generators/__init__.py index 39252385139d3a..131d8e38ccbeba 100644 --- a/scripts/py_matter_idl/matter_idl/generators/__init__.py +++ b/scripts/py_matter_idl/matter_idl/generators/__init__.py @@ -118,12 +118,13 @@ def __init__(self, storage: GeneratorStorage, idl: Idl, loader: Optional[jinja2. """ if not loader: if not fs_loader_searchpath: - fs_loader_searchpath=os.path.dirname(__file__) - loader=jinja2.FileSystemLoader(searchpath=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=loader, keep_trailing_newline=True) + self.jinja_env = jinja2.Environment( + 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/cpp/application/__init__.py b/scripts/py_matter_idl/matter_idl/generators/cpp/application/__init__.py index 09776fbc3c274c..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,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List import os +from typing import List from matter_idl.generators import CodeGenerator, GeneratorStorage from matter_idl.matter_idl_types import Cluster, ClusterSide, Idl From 73107a7a738af6d3af1b583387c6360986b3dce2 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 3 Apr 2023 12:04:34 -0400 Subject: [PATCH 3/4] Add fs_loader_path to the example generator plugin. --- .../examples/matter_idl_plugin/__init__.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 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..502c4ed187b85f 100644 --- a/scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py +++ b/scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py @@ -199,13 +199,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 +235,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, From d62810d0fd97880c5d5552ef824a746ba1d91b55 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 3 Apr 2023 13:11:52 -0400 Subject: [PATCH 4/4] Remove unused import --- scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py | 1 - 1 file changed, 1 deletion(-) 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 502c4ed187b85f..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