Skip to content

Commit ead8804

Browse files
committed
Add template variable start / end string definition for parsing
1 parent 91fad03 commit ead8804

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

runtime/chalicelib/template_manager/__interface__.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
class TemplateInformation(pydantic.BaseModel):
1919
code: str
2020
template: type_util.TemplateType
21+
template_variable_start_end_string: tuple[str, str]
2122

2223
@pydantic.computed_field # type: ignore[misc]
2324
@property
2425
def template_variables(self) -> set[str]:
2526
# From https://stackoverflow.com/a/77363330
26-
template = jinja2.Environment(autoescape=True).parse(source=json.dumps(self.template))
27+
template = jinja2.Environment(
28+
autoescape=True,
29+
variable_start_string=self.template_variable_start_end_string[0],
30+
variable_end_string=self.template_variable_start_end_string[1],
31+
).parse(source=json.dumps(self.template, ensure_ascii=False))
2732
return jinja2.meta.find_undeclared_variables(ast=template)
2833

2934

@@ -32,6 +37,10 @@ def template_variables(self) -> set[str]:
3237

3338
class TemplateManagerInterface(typing.Protocol[TemplateStructureType]):
3439
template_structure_cls: type[TemplateStructureType] | type[str] | None = None
40+
template_variable_start_end_string: typing.ClassVar[tuple[str, str]] = ("{{", "}}")
41+
42+
@property
43+
def initialized(self) -> bool: ...
3544

3645
def check_template_valid(self, template_data: type_util.TemplateType) -> bool:
3746
if not self.template_structure_cls or self.template_structure_cls == str:
@@ -59,21 +68,33 @@ def render(self, code: str, context: type_util.ContextType) -> type_util.Templat
5968
class S3ResourceTemplateManager(template_mgr_interface.TemplateManagerInterface):
6069
resource: typing.ClassVar[aws_resource.S3ResourcePath]
6170

71+
@property
72+
def initialized(self) -> bool:
73+
return True
74+
6275
def list(self) -> list[template_mgr_interface.TemplateInformation]:
6376
return [self.retrieve(code=f.split(sep=".")[0]) for f in self.resource.list_objects(filter_by_extension=True)]
6477

6578
@functools.lru_cache # noqa: B019
6679
def retrieve(self, code: str) -> template_mgr_interface.TemplateInformation | None:
6780
try:
6881
template_body: str = self.resource.download(code=code).decode(encoding="utf-8")
69-
return template_mgr_interface.TemplateInformation(code=code, template=template_body)
82+
return template_mgr_interface.TemplateInformation(
83+
code=code,
84+
template=template_body,
85+
template_variable_start_end_string=self.template_variable_start_end_string,
86+
)
7087
except botocore.exceptions.ClientError:
7188
return None
7289

7390
def create(self, code: str, template_data: type_util.TemplateType) -> template_mgr_interface.TemplateInformation:
7491
self.check_template_valid(template_data=template_data)
7592
self.resource.upload(code=code, content=template_data)
76-
return template_mgr_interface.TemplateInformation(code=code, template=template_data)
93+
return template_mgr_interface.TemplateInformation(
94+
code=code,
95+
template=template_data,
96+
template_variable_start_end_string=self.template_variable_start_end_string,
97+
)
7798

7899
def update(self, code: str, template_data: type_util.TemplateType) -> template_mgr_interface.TemplateInformation:
79100
return self.create(code=code, template_data=template_data)

runtime/chalicelib/template_manager/toast_alimtalk.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import dataclasses
44
import functools
5+
import typing
56

7+
import chalicelib.config as config_module
68
import chalicelib.external_api.toast_alimtalk as toast_alimtalk_client
79
import chalicelib.template_manager.__interface__ as template_mgr_interface
810
import chalicelib.util.type_util as type_util
@@ -11,22 +13,36 @@
1113

1214
@dataclasses.dataclass
1315
class ToastAlimtalkTemplateManager(template_mgr_interface.TemplateManagerInterface):
14-
template_structure_cls = str
16+
template_structure_cls = toast_alimtalk_client.Template
17+
template_variable_start_end_string: typing.ClassVar[tuple[str, str]] = ("#{", "}")
18+
19+
@property
20+
def initialized(self) -> bool:
21+
return config_module.config.toast.is_configured()
1522

1623
@functools.cached_property
1724
def client(self) -> toast_alimtalk_client.ToastAlimTalkClient:
1825
return toast_alimtalk_client.ToastAlimTalkClient()
1926

2027
def list(self) -> list[template_mgr_interface.TemplateInformation]:
2128
return [
22-
template_mgr_interface.TemplateInformation(code=t.templateCode, template=t.templateContent)
29+
template_mgr_interface.TemplateInformation(
30+
code=t.templateCode,
31+
template=t.model_dump(mode="json"),
32+
template_variable_start_end_string=self.template_variable_start_end_string,
33+
)
2334
for t in self.client.get_template_list().templateListResponse.templates
35+
if t.status == "TSC03"
2436
]
2537

2638
def retrieve(self, code: str) -> template_mgr_interface.TemplateInformation | None:
2739
query_params = toast_alimtalk_client.TemplateListQueryRequest(templateCode=code)
2840
if t := self.client.get_template_list(query_params=query_params).templateListResponse.templates:
29-
return template_mgr_interface.TemplateInformation(code=t[0].templateCode, template=t[0].templateContent)
41+
return template_mgr_interface.TemplateInformation(
42+
code=t[0].templateCode,
43+
template=t[0].model_dump(mode="json"),
44+
template_variable_start_end_string=self.template_variable_start_end_string,
45+
)
3046
return None
3147

3248
def create(self, code: str, template_data: str) -> template_mgr_interface.TemplateInformation:

0 commit comments

Comments
 (0)