Skip to content

Commit

Permalink
supoort use jinja2 to render yaml and json
Browse files Browse the repository at this point in the history
supoort use jinja2 to render yaml and json
  • Loading branch information
huangshiyu13 authored Aug 24, 2023
2 parents 7938bce + 31ce537 commit 6340c88
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 deletions.
8 changes: 6 additions & 2 deletions openplugin/template/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.

""""""
from jinja2 import Template


def render_ROOT_URL(text: str, request=None) -> str:
Expand All @@ -23,5 +24,8 @@ def render_ROOT_URL(text: str, request=None) -> str:
ROOT_URL = request.url_root
if ROOT_URL.endswith("/"):
ROOT_URL = ROOT_URL[:-1]
text = text.replace("{% ROOT_URL %}", ROOT_URL)
return text

template = Template(text)
rendered_text = template.render(ROOT_URL=ROOT_URL)
# text = text.replace("{{ROOT_URL}}", ROOT_URL)
return rendered_text
32 changes: 16 additions & 16 deletions openplugin/utils/app_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,29 +357,29 @@ def write_error(sock, status_int, reason, mesg):
html_error = (
textwrap.dedent(
"""\
<html>
<head>
<title>%(reason)s</title>
</head>
<body>
<h1><p>%(reason)s</p></h1>
%(mesg)s
</body>
</html>
"""
<html>
<head>
<title>%(reason)s</title>
</head>
<body>
<h1><p>%(reason)s</p></h1>
%(mesg)s
</body>
</html>
"""
)
% {"reason": reason, "mesg": html.escape(mesg)}
)

http = (
textwrap.dedent(
"""\
HTTP/1.1 %s %s\r
Connection: close\r
Content-Type: text/html\r
Content-Length: %d\r
\r
%s"""
HTTP/1.1 %s %s\r
Connection: close\r
Content-Type: text/html\r
Content-Length: %d\r
\r
%s"""
)
% (str(status_int), reason, len(html_error), html_error)
)
Expand Down
2 changes: 1 addition & 1 deletion plugins/QRcode_plugin/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ info:
version: 1.0.0

servers:
- url: {% ROOT_URL %}
- url: {{ROOT_URL}}

paths:
/qrcode_image:
Expand Down
2 changes: 1 addition & 1 deletion plugins/ikun_plugin/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ info:
version: 1.0.0

servers:
- url: {% ROOT_URL %}
- url: {{ROOT_URL}}

paths:
/ikun_image:
Expand Down
4 changes: 2 additions & 2 deletions plugins/todo_plugin/ai-plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
},
"api": {
"type": "openapi",
"url": "{% ROOT_URL %}/openapi.yaml"
"url": "{{ROOT_URL}}/openapi.yaml"
},
"logo_url": "{% ROOT_URL %}/logo.png",
"logo_url": "{{ROOT_URL}}/logo.png",
"contact_email": "[email protected]",
"legal_info_url": "http://example.com/legal"
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/todo_plugin/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ info:
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".
version: 'v1'
servers:
- url: {% ROOT_URL %}
- url: {{ROOT_URL}}
paths:
/todos/{username}:
get:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def get_install_requires() -> list:
"ifaddr",
"numpy",
"requests",
"jinja2",
]


Expand Down

0 comments on commit 6340c88

Please sign in to comment.