2024-07-22 15:29:39 +08:00
|
|
|
import re
|
|
|
|
|
|
|
|
from core.workflow.entities.variable_pool import VariablePool
|
|
|
|
|
2024-07-26 15:03:56 +08:00
|
|
|
from . import SegmentGroup, factory
|
|
|
|
|
2024-07-22 15:29:39 +08:00
|
|
|
VARIABLE_PATTERN = re.compile(r'\{\{#([a-zA-Z0-9_]{1,50}(?:\.[a-zA-Z_][a-zA-Z0-9_]{0,29}){1,10})#\}\}')
|
|
|
|
|
|
|
|
|
|
|
|
def convert_template(*, template: str, variable_pool: VariablePool):
|
|
|
|
parts = re.split(VARIABLE_PATTERN, template)
|
|
|
|
segments = []
|
2024-07-26 18:19:33 +08:00
|
|
|
for part in filter(lambda x: x, parts):
|
2024-07-22 15:29:39 +08:00
|
|
|
if '.' in part and (value := variable_pool.get(part.split('.'))):
|
|
|
|
segments.append(value)
|
|
|
|
else:
|
|
|
|
segments.append(factory.build_segment(part))
|
2024-07-26 15:03:56 +08:00
|
|
|
return SegmentGroup(value=segments)
|