Skip to content

Commit 1061195

Browse files
andy31415pull[bot]
authored andcommitted
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
1 parent c7cfad7 commit 1061195

File tree

5 files changed

+32
-27
lines changed

5 files changed

+32
-27
lines changed

scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import os
1616

17-
import jinja2
1817
from matter_idl.generators import CodeGenerator, GeneratorStorage
1918
from matter_idl.matter_idl_types import Cluster, ClusterSide, Command, Field, Idl
2019

@@ -199,13 +198,7 @@ def __init__(self, storage: GeneratorStorage, idl: Idl, **kargs):
199198
Inintialization is specific for java generation and will add
200199
filters as required by the java .jinja templates to function.
201200
"""
202-
super().__init__(storage, idl)
203-
204-
# Override the template path to use local templates within this plugin directory
205-
self.jinja_env = jinja2.Environment(
206-
loader=jinja2.FileSystemLoader(
207-
searchpath=os.path.dirname(__file__)),
208-
keep_trailing_newline=True)
201+
super().__init__(storage, idl, fs_loader_searchpath=os.path.dirname(__file__))
209202

210203
# String helpers
211204
self.jinja_env.filters['toLowerSnakeCase'] = toLowerSnakeCase
@@ -241,7 +234,7 @@ def internal_render_all(self):
241234

242235
# Header containing a macro to initialize all cluster plugins
243236
self.internal_render_one_output(
244-
template_path="./matter_cluster_proto.jinja",
237+
template_path="matter_cluster_proto.jinja",
245238
output_file_name=filename,
246239
vars={
247240
'cluster': cluster,

scripts/py_matter_idl/matter_idl/generators/__init__.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import logging
1717
import os
18-
from typing import Dict
18+
from typing import Dict, Optional
1919

2020
import jinja2
2121
from matter_idl.matter_idl_types import Idl
@@ -106,16 +106,25 @@ class CodeGenerator:
106106
write time of files do not change and rebuilds are not triggered).
107107
"""
108108

109-
def __init__(self, storage: GeneratorStorage, idl: Idl):
109+
def __init__(self, storage: GeneratorStorage, idl: Idl, loader: Optional[jinja2.BaseLoader] = None, fs_loader_searchpath: Optional[str] = None):
110110
"""
111111
A code generator will render a parsed IDL (a AST) into a given storage.
112+
113+
Args:
114+
storage: Storage to use to read/save data
115+
loader: if given, use a custom loader for templates
116+
fs_loader_searchpath: if a loader is NOT given, this controls the search path
117+
of a default FileSystemLoader that will be used
112118
"""
119+
if not loader:
120+
if not fs_loader_searchpath:
121+
fs_loader_searchpath = os.path.dirname(__file__)
122+
loader = jinja2.FileSystemLoader(searchpath=fs_loader_searchpath)
123+
113124
self.storage = storage
114125
self.idl = idl
115126
self.jinja_env = jinja2.Environment(
116-
loader=jinja2.FileSystemLoader(
117-
searchpath=os.path.dirname(__file__)),
118-
keep_trailing_newline=True)
127+
loader=loader, keep_trailing_newline=True)
119128
self.dry_run = False
120129

121130
RegisterCommonFilters(self.jinja_env.filters)

scripts/py_matter_idl/matter_idl/generators/bridge/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515

1616
import logging
17+
import os
1718
import re
1819

1920
from matter_idl.generators import CodeGenerator, GeneratorStorage
@@ -137,7 +138,7 @@ def __init__(self, storage: GeneratorStorage, idl: Idl, **kargs):
137138
Inintialization is specific for cpp generation and will add
138139
filters as required by the cpp .jinja templates to function.
139140
"""
140-
super().__init__(storage, idl)
141+
super().__init__(storage, idl, fs_loader_searchpath=os.path.dirname(__file__))
141142

142143
self.jinja_env.filters['getType'] = get_attr_type
143144
self.jinja_env.filters['getRawSizeAndType'] = get_raw_size_and_type
@@ -163,7 +164,7 @@ def internal_render_all(self):
163164
output_file_name = "bridge/%s.h" % cluster.name
164165

165166
self.internal_render_one_output(
166-
template_path="bridge/BridgeClustersCpp.jinja",
167+
template_path="BridgeClustersCpp.jinja",
167168
output_file_name=output_file_name,
168169
vars={
169170
'cluster': cluster,
@@ -172,7 +173,7 @@ def internal_render_all(self):
172173
)
173174

174175
self.internal_render_one_output(
175-
template_path="bridge/BridgeClustersCommon.jinja",
176+
template_path="BridgeClustersCommon.jinja",
176177
output_file_name="bridge/BridgeClustersImpl.h",
177178
vars={
178179
'clusters': self.idl.clusters,
@@ -181,7 +182,7 @@ def internal_render_all(self):
181182
)
182183

183184
self.internal_render_one_output(
184-
template_path="bridge/BridgeClustersGlobalStructs.jinja",
185+
template_path="BridgeClustersGlobalStructs.jinja",
185186
output_file_name="bridge/BridgeGlobalStructs.h",
186187
vars={
187188
'idl': self.idl,

scripts/py_matter_idl/matter_idl/generators/cpp/application/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
from typing import List
1617

1718
from matter_idl.generators import CodeGenerator, GeneratorStorage
@@ -32,7 +33,7 @@ def __init__(self, storage: GeneratorStorage, idl: Idl, **kargs):
3233
Inintialization is specific for java generation and will add
3334
filters as required by the java .jinja templates to function.
3435
"""
35-
super().__init__(storage, idl)
36+
super().__init__(storage, idl, fs_loader_searchpath=os.path.dirname(__file__))
3637

3738
self.jinja_env.filters['serverClustersOnly'] = serverClustersOnly
3839

@@ -43,7 +44,7 @@ def internal_render_all(self):
4344

4445
# Header containing a macro to initialize all cluster plugins
4546
self.internal_render_one_output(
46-
template_path="cpp/application/PluginApplicationCallbacksHeader.jinja",
47+
template_path="PluginApplicationCallbacksHeader.jinja",
4748
output_file_name="app/PluginApplicationCallbacks.h",
4849
vars={
4950
'clusters': self.idl.clusters,
@@ -53,7 +54,7 @@ def internal_render_all(self):
5354
# Source for __attribute__(weak) implementations of all cluster
5455
# initialization methods
5556
self.internal_render_one_output(
56-
template_path="cpp/application/CallbackStubSource.jinja",
57+
template_path="CallbackStubSource.jinja",
5758
output_file_name="app/callback-stub.cpp",
5859
vars={
5960
'clusters': self.idl.clusters,

scripts/py_matter_idl/matter_idl/generators/java/__init__.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import enum
1717
import logging
18+
import os
1819
from typing import List, Set, Union
1920

2021
from matter_idl.generators import CodeGenerator, GeneratorStorage
@@ -405,7 +406,7 @@ def __init__(self, storage: GeneratorStorage, idl: Idl, **kargs):
405406
Inintialization is specific for java generation and will add
406407
filters as required by the java .jinja templates to function.
407408
"""
408-
super().__init__(storage, idl)
409+
super().__init__(storage, idl, fs_loader_searchpath=os.path.dirname(__file__))
409410

410411
self.jinja_env.filters['attributesWithCallback'] = attributesWithSupportedCallback
411412
self.jinja_env.filters['callbackName'] = CallbackName
@@ -434,7 +435,7 @@ def internal_render_all(self):
434435
"""
435436

436437
self.internal_render_one_output(
437-
template_path="java/CHIPCallbackTypes.jinja",
438+
template_path="CHIPCallbackTypes.jinja",
438439
output_file_name="jni/CHIPCallbackTypes.h",
439440
vars={
440441
'idl': self.idl,
@@ -449,7 +450,7 @@ def internal_render_all(self):
449450
continue
450451

451452
self.internal_render_one_output(
452-
template_path="java/ChipClustersRead.jinja",
453+
template_path="ChipClustersRead.jinja",
453454
output_file_name="jni/%sClient-ReadImpl.cpp" % cluster.name,
454455
vars={
455456
'cluster': cluster,
@@ -458,7 +459,7 @@ def internal_render_all(self):
458459
)
459460

460461
self.internal_render_one_output(
461-
template_path="java/ChipClustersCpp.jinja",
462+
template_path="ChipClustersCpp.jinja",
462463
output_file_name="jni/%sClient-InvokeSubscribeImpl.cpp" % cluster.name,
463464
vars={
464465
'cluster': cluster,
@@ -482,7 +483,7 @@ def internal_render_all(self):
482483
c for c in self.idl.clusters if c.side == ClusterSide.CLIENT]
483484

484485
self.internal_render_one_output(
485-
template_path="java/ClusterReadMapping.jinja",
486+
template_path="ClusterReadMapping.jinja",
486487
output_file_name="java/chip/devicecontroller/ClusterReadMapping.java",
487488
vars={
488489
'idl': self.idl,
@@ -491,7 +492,7 @@ def internal_render_all(self):
491492
)
492493

493494
self.internal_render_one_output(
494-
template_path="java/ClusterWriteMapping.jinja",
495+
template_path="ClusterWriteMapping.jinja",
495496
output_file_name="java/chip/devicecontroller/ClusterWriteMapping.java",
496497
vars={
497498
'idl': self.idl,

0 commit comments

Comments
 (0)