Skip to content

Commit 6340c88

Browse files
authored
supoort use jinja2 to render yaml and json
supoort use jinja2 to render yaml and json
2 parents 7938bce + 31ce537 commit 6340c88

File tree

7 files changed

+28
-23
lines changed

7 files changed

+28
-23
lines changed

openplugin/template/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616

1717
""""""
18+
from jinja2 import Template
1819

1920

2021
def render_ROOT_URL(text: str, request=None) -> str:
@@ -23,5 +24,8 @@ def render_ROOT_URL(text: str, request=None) -> str:
2324
ROOT_URL = request.url_root
2425
if ROOT_URL.endswith("/"):
2526
ROOT_URL = ROOT_URL[:-1]
26-
text = text.replace("{% ROOT_URL %}", ROOT_URL)
27-
return text
27+
28+
template = Template(text)
29+
rendered_text = template.render(ROOT_URL=ROOT_URL)
30+
# text = text.replace("{{ROOT_URL}}", ROOT_URL)
31+
return rendered_text

openplugin/utils/app_util.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -357,29 +357,29 @@ def write_error(sock, status_int, reason, mesg):
357357
html_error = (
358358
textwrap.dedent(
359359
"""\
360-
<html>
361-
<head>
362-
<title>%(reason)s</title>
363-
</head>
364-
<body>
365-
<h1><p>%(reason)s</p></h1>
366-
%(mesg)s
367-
</body>
368-
</html>
369-
"""
360+
<html>
361+
<head>
362+
<title>%(reason)s</title>
363+
</head>
364+
<body>
365+
<h1><p>%(reason)s</p></h1>
366+
%(mesg)s
367+
</body>
368+
</html>
369+
"""
370370
)
371371
% {"reason": reason, "mesg": html.escape(mesg)}
372372
)
373373

374374
http = (
375375
textwrap.dedent(
376376
"""\
377-
HTTP/1.1 %s %s\r
378-
Connection: close\r
379-
Content-Type: text/html\r
380-
Content-Length: %d\r
381-
\r
382-
%s"""
377+
HTTP/1.1 %s %s\r
378+
Connection: close\r
379+
Content-Type: text/html\r
380+
Content-Length: %d\r
381+
\r
382+
%s"""
383383
)
384384
% (str(status_int), reason, len(html_error), html_error)
385385
)

plugins/QRcode_plugin/openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ info:
55
version: 1.0.0
66

77
servers:
8-
- url: {% ROOT_URL %}
8+
- url: {{ROOT_URL}}
99

1010
paths:
1111
/qrcode_image:

plugins/ikun_plugin/openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ info:
55
version: 1.0.0
66

77
servers:
8-
- url: {% ROOT_URL %}
8+
- url: {{ROOT_URL}}
99

1010
paths:
1111
/ikun_image:

plugins/todo_plugin/ai-plugin.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
},
1010
"api": {
1111
"type": "openapi",
12-
"url": "{% ROOT_URL %}/openapi.yaml"
12+
"url": "{{ROOT_URL}}/openapi.yaml"
1313
},
14-
"logo_url": "{% ROOT_URL %}/logo.png",
14+
"logo_url": "{{ROOT_URL}}/logo.png",
1515
"contact_email": "huangsy1314@163.com",
1616
"legal_info_url": "http://example.com/legal"
1717
}

plugins/todo_plugin/openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ info:
44
description: A plugin that allows the user to create and manage a TODO list using ChatGPT. If you do not know the user's username, ask them first before making queries to the plugin. Otherwise, use the username "global".
55
version: 'v1'
66
servers:
7-
- url: {% ROOT_URL %}
7+
- url: {{ROOT_URL}}
88
paths:
99
/todos/{username}:
1010
get:

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def get_install_requires() -> list:
3838
"ifaddr",
3939
"numpy",
4040
"requests",
41+
"jinja2",
4142
]
4243

4344

0 commit comments

Comments
 (0)