Skip to content

Commit

Permalink
#625 Drop dependency to jinja2
Browse files Browse the repository at this point in the history
  • Loading branch information
SebCorbin committed Nov 9, 2022
1 parent 843c898 commit 28ec464
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
'python-dateutil',
'requests',
'sqlparse',
'Jinja2',
'autopep8',
'gprof2dot>=2017.09.19',
],
Expand Down
28 changes: 15 additions & 13 deletions silk/code_generation/curl.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import json

# noinspection PyUnresolvedReferences
from urllib.parse import urlencode

import jinja2
from django.template import Context, Template

curl_template = """
curl {% if method %}-X {{ method }}{% endif %}
Expand All @@ -16,7 +14,6 @@

def _curl_process_params(body, content_type, query_params):
extra = None
modifier = None
if query_params:
try:
query_params = urlencode(
Expand Down Expand Up @@ -55,13 +52,18 @@ def curl_cmd(url, method=None, query_params=None, body=None, content_type=None):
if not content_type:
content_type = 'text/plain'
modifier, body, query_params, content_type, extra = _curl_process_params(
body, content_type, query_params
body,
content_type,
query_params,
)
t = jinja2.Template(curl_template)
return ' '.join(t.render(url=url,
method=method,
query_params=query_params,
body=body,
modifier=modifier,
content_type=content_type,
extra=extra).split('\n'))
t = Template(curl_template)
context = {
'url': url,
'method': method,
'query_params': query_params,
'body': body,
'modifier': modifier,
'content_type': content_type,
'extra': extra,
}
return t.render(Context(context)).replace('\n', ' ')
31 changes: 13 additions & 18 deletions silk/code_generation/django_test_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# noinspection PyUnresolvedReferences
from urllib.parse import urlencode

import autopep8
import jinja2
from django.template import Template

from silk.profiling.dynamic import is_str_typ

Expand All @@ -23,31 +22,27 @@ def _encode_query_params(query_params):
return '?' + query_params


def gen(path,
method=None,
query_params=None,
data=None,
content_type=None):
def gen(path, method=None, query_params=None, data=None, content_type=None):
# generates python code representing a call via django client.
# useful for use in testing
method = method.lower()
t = jinja2.Template(template)
t = Template(template)
context = {
'path': path,
'lower_case_method': method,
'content_type': content_type,
}
if method == 'get':
r = t.render(path=path,
data=query_params,
lower_case_method=method,
content_type=content_type)
context['data'] = query_params
else:
if query_params:
query_params = _encode_query_params(query_params)
path += query_params
if is_str_typ(data):
data = "'%s'" % data
r = t.render(path=path,
data=data,
lower_case_method=method,
query_params=query_params,
content_type=content_type)
context['data'] = data
context['query_params'] = query_params
return autopep8.fix_code(
r, options=autopep8.parse_args(['--aggressive', ''])
t.render(context),
options=autopep8.parse_args(['--aggressive', '']),
)

0 comments on commit 28ec464

Please sign in to comment.