本文整理汇总了Python中pyramid_debugtoolbar.compat.bytes_函数的典型用法代码示例。如果您正苦于以下问题:Python bytes_函数的具体用法?Python bytes_怎么用?Python bytes_使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了bytes_函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_response
def process_response(self, response):
# If the body is HTML, then we add the toolbar to the response.
request = self.request
if isinstance(response, WSGIHTTPException):
# the body of a WSGIHTTPException needs to be "prepared"
response.prepare(request.environ)
for panel in self.panels:
panel.process_response(response)
if response.content_type in self.html_types:
static_path = request.static_url(STATIC_PATH)
root_path = request.route_url(ROOT_ROUTE_NAME)
button_style = get_setting(request.registry.settings,
'button_style', '')
vars = {'panels': self.panels, 'static_path': static_path,
'root_path': root_path, 'button_style': button_style}
toolbar_html = render(
'pyramid_debugtoolbar:templates/toolbar.dbtmako',
vars, request=request)
response_html = response.body
toolbar_html = toolbar_html.encode(response.charset or 'utf-8')
body = replace_insensitive(
response_html, bytes_('</body>'),
toolbar_html + bytes_('</body>')
)
response.app_iter = [body]
response.content_length = len(body)
示例2: process_response
def process_response(self, response):
data = []
for query in self.queries:
stmt = query['statement']
is_select = stmt.strip().lower().startswith('select')
params = ''
try:
params = url_quote(json.dumps(query['parameters']))
except TypeError:
pass # object not JSON serializable
except UnicodeDecodeError:
pass # parameters contain non-utf8 (probably binary) data
need = self.token + stmt + params
hash = hashlib.sha1(bytes_(need)).hexdigest()
data.append({
'engine_id': query['engine_id'],
'duration': query['duration'],
'sql': format_sql(stmt),
'raw_sql': stmt,
'hash': hash,
'parameters': query['parameters'],
'params': params,
'is_select': is_select,
'context': query['context'],
})
self.data = {
'queries':data,
'text':text,
}
示例3: sql_select
def sql_select(self):
stmt = self.request.params['sql']
params = self.request.params['params']
engine_id = self.request.params['engine_id']
# Validate hash
hash = hashlib.sha1(
bytes_(self.request.exc_history.token + stmt + params)).hexdigest()
if hash != self.request.params['hash']:
raise HTTPBadRequest('Bad token in request')
# Make sure it is a select statement
if not stmt.lower().strip().startswith('select'):
raise HTTPBadRequest('Not a SELECT SQL statement')
if not engine_id:
raise HTTPBadRequest('No valid database engine')
engine = getattr(self.request.registry, 'pdtb_sqla_engines')\
[int(engine_id)]()
params = json.loads(params)
result = engine.execute(stmt, params)
return {
'result': result.fetchall(),
'headers': result.keys(),
'sql': format_sql(stmt),
'duration': float(self.request.params['duration']),
}
示例4: test_execute
def test_execute(self):
request = self._makeRequest()
request.params['cmd'] = 'doit'
view = self._makeOne(request)
response = view.execute()
self.assertEqual(response.body, bytes_('evaled'))
self.assertEqual(response.content_type, 'text/html')
示例5: test_source
def test_source(self):
request = self._makeRequest()
request.params['frm'] = '0'
view = self._makeOne(request)
response = view.source()
self.assertEqual(response.body, bytes_('source'))
self.assertEqual(response.content_type, 'text/html')
示例6: sql_select
def sql_select(self):
stmt = self.request.params["sql"]
params = self.request.params["params"]
engine_id = self.request.params["engine_id"]
# Validate hash
hash = hashlib.sha1(bytes_(self.request.exc_history.token + stmt + params)).hexdigest()
if hash != self.request.params["hash"]:
raise HTTPBadRequest("Bad token in request")
# Make sure it is a select statement
if not stmt.lower().strip().startswith("select"):
raise HTTPBadRequest("Not a SELECT SQL statement")
if not engine_id:
raise HTTPBadRequest("No valid database engine")
engine = getattr(self.request.registry, "pdtb_sqla_engines")[int(engine_id)]()
params = json.loads(params)
result = engine.execute(stmt, params)
return {
"result": result.fetchall(),
"headers": result.keys(),
"sql": format_sql(stmt),
"duration": float(self.request.params["duration"]),
}
示例7: test_execute
def test_execute(self):
request = self._makeRequest()
request.params["frm"] = "0"
request.params["cmd"] = "doit"
view = self._makeOne(request)
response = view.execute()
self.assertEqual(response.body, bytes_("evaled"))
self.assertEqual(response.content_type, "text/html")
示例8: inject
def inject(self, request, response):
"""
Inject the debug toolbar iframe into an HTML response.
"""
# called in host app
response_html = response.body
toolbar_url = debug_toolbar_url(request, request.id)
button_style = get_setting(request.registry.settings,
'button_style', '')
css_path = request.static_url(STATIC_PATH + 'css/toolbar_button.css')
toolbar_html = toolbar_html_template % {
'button_style': button_style,
'css_path': css_path,
'toolbar_url': toolbar_url}
toolbar_html = toolbar_html.encode(response.charset or 'utf-8')
response.body = replace_insensitive(
response_html, bytes_('</body>'),
toolbar_html + bytes_('</body>')
)
示例9: validate
def validate(self):
stmt = self.request.params["sql"]
params = self.request.params["params"]
# Validate hash
need = self.request.exc_history.token + stmt + url_quote(params)
hash = hashlib.sha1(bytes_(need)).hexdigest()
if hash != self.request.params["hash"]:
raise HTTPBadRequest("Bad token in request")
return stmt, params
示例10: validate
def validate(self):
stmt = self.request.params['sql']
params = self.request.params['params']
# Validate hash
need = self.token + stmt + url_quote(params)
hash = hashlib.sha1(bytes_(need)).hexdigest()
if hash != self.request.params['hash']:
raise HTTPBadRequest('Bad token in request')
return stmt, params
示例11: test_inject_html
def test_inject_html(self):
from pyramid_debugtoolbar.utils import STATIC_PATH
self.config.add_static_view('_debugtoolbar/static', STATIC_PATH)
self.config.add_route('debugtoolbar', '/_debugtoolbar/*subpath')
response = Response('<body></body>')
response.content_type = 'text/html'
request = Request.blank('/')
request.pdtb_id = 'abc'
request.registry = self.config.registry
toolbar = self._makeOne(request, [DummyPanel], [DummyPanel], [])
toolbar.inject(request, response)
self.assertTrue(bytes_('div id="pDebug"') in response.app_iter[0])
self.assertEqual(response.content_length, len(response.app_iter[0]))
示例12: test_process_response_html
def test_process_response_html(self):
from pyramid_debugtoolbar.utils import ROOT_ROUTE_NAME
from pyramid_debugtoolbar.utils import STATIC_PATH
self.config.add_static_view('_debugtoolbar/static',
STATIC_PATH)
self.config.add_route(ROOT_ROUTE_NAME, '/_debugtoolbar')
response = Response('<body></body>')
response.content_type = 'text/html'
request = Request.blank('/')
request.registry = self.config.registry
toolbar = self._makeOne(request, [DummyPanel])
toolbar.process_response(response)
self.assertTrue(response.processed)
self.assertTrue(bytes_('div id="pDebug"') in response.app_iter[0])
示例13: test_passing_of_button_style
def test_passing_of_button_style(self):
from pyramid_debugtoolbar.utils import STATIC_PATH
self.config.add_static_view('_debugtoolbar/static', STATIC_PATH)
self.config.add_route('debugtoolbar', '/_debugtoolbar/*subpath')
self.config.registry.settings['debugtoolbar.button_style'] = \
'top:120px;zoom:50%'
response = Response('<body></body>')
response.content_type = 'text/html'
request = Request.blank('/')
request.pdtb_id = 'abc'
request.registry = self.config.registry
toolbar = self._makeOne(request, [DummyPanel], [DummyPanel], [])
toolbar.inject(request, response)
self.assertTrue(bytes_('top:120px;zoom:50%') in response.app_iter[0])
示例14: inject
def inject(self, request, response):
"""
Inject the debug toolbar iframe into an HTML response.
"""
# called in host app
response_html = response.body
toolbar_url = debug_toolbar_url(request, request.id)
button_style = get_setting(request.registry.settings, "button_style", "")
css_path = request.static_url(STATIC_PATH + "css/toolbar.css")
toolbar_html = toolbar_html_template % {
"button_style": button_style,
"css_path": css_path,
"toolbar_url": toolbar_url,
}
toolbar_html = toolbar_html.encode(response.charset or "utf-8")
response.body = replace_insensitive(response_html, bytes_("</body>"), toolbar_html + bytes_("</body>"))
示例15: test_passing_of_button_style
def test_passing_of_button_style(self):
from pyramid_debugtoolbar.utils import ROOT_ROUTE_NAME
from pyramid_debugtoolbar.utils import STATIC_PATH
self.config.include('pyramid_jinja2')
self.config.add_static_view('_debugtoolbar/static',
STATIC_PATH)
self.config.add_route(ROOT_ROUTE_NAME, '/_debugtoolbar')
self.config.registry.settings['debugtoolbar.button_style'] = \
'top:120px;zoom:50%'
response = Response('<body></body>')
response.content_type = 'text/html'
request = Request.blank('/')
request.registry = self.config.registry
toolbar = self._makeOne(request, [DummyPanel])
toolbar.process_response(response)
self.assertTrue(response.processed)
self.failUnless(bytes_('top:120px;zoom:50%') in response.app_iter[0])