|
|
|
|
|
|
|
|
# |
|
|
# |
|
|
import json |
|
|
import json |
|
|
import re |
|
|
import re |
|
|
|
|
|
|
|
|
|
|
|
from jinja2 import StrictUndefined |
|
|
|
|
|
from jinja2.sandbox import SandboxedEnvironment |
|
|
|
|
|
|
|
|
from agent.component.base import ComponentBase, ComponentParamBase |
|
|
from agent.component.base import ComponentBase, ComponentParamBase |
|
|
from jinja2 import Template as Jinja2Template |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TemplateParam(ComponentParamBase): |
|
|
class TemplateParam(ComponentParamBase): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = "" |
|
|
result = "" |
|
|
if "content" in out.columns: |
|
|
if "content" in out.columns: |
|
|
result = "\n".join( |
|
|
|
|
|
[o if isinstance(o, str) else str(o) for o in out["content"]] |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
result = "\n".join([o if isinstance(o, str) else str(o) for o in out["content"]]) |
|
|
|
|
|
|
|
|
self.make_kwargs(para, kwargs, result) |
|
|
self.make_kwargs(para, kwargs, result) |
|
|
|
|
|
|
|
|
template = Jinja2Template(content) |
|
|
|
|
|
|
|
|
env = SandboxedEnvironment( |
|
|
|
|
|
autoescape=True, |
|
|
|
|
|
undefined=StrictUndefined, |
|
|
|
|
|
) |
|
|
|
|
|
template = env.from_string(content) |
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
content = template.render(kwargs) |
|
|
content = template.render(kwargs) |
|
|
|
|
|
|
|
|
v = json.dumps(v, ensure_ascii=False) |
|
|
v = json.dumps(v, ensure_ascii=False) |
|
|
except Exception: |
|
|
except Exception: |
|
|
pass |
|
|
pass |
|
|
content = re.sub( |
|
|
|
|
|
r"\{%s\}" % re.escape(n), v, content |
|
|
|
|
|
) |
|
|
|
|
|
content = re.sub( |
|
|
|
|
|
r"(#+)", r" \1 ", content |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
content = re.sub(r"\{%s\}" % re.escape(n), v, content) |
|
|
|
|
|
content = re.sub(r"(#+)", r" \1 ", content) |
|
|
|
|
|
|
|
|
return Template.be_output(content) |
|
|
return Template.be_output(content) |
|
|
|
|
|
|
|
|
def make_kwargs(self, para, kwargs, value): |
|
|
def make_kwargs(self, para, kwargs, value): |
|
|
self._param.inputs.append( |
|
|
|
|
|
{"component_id": para["key"], "content": value} |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
self._param.inputs.append({"component_id": para["key"], "content": value}) |
|
|
try: |
|
|
try: |
|
|
value = json.loads(value) |
|
|
value = json.loads(value) |
|
|
except Exception: |
|
|
except Exception: |