2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2014-02-07 11:01:58 +01:00
|
|
|
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-04-17 17:44:27 +02:00
|
|
|
"""Handler functions for different qute:... pages.
|
|
|
|
|
|
|
|
Module attributes:
|
|
|
|
pyeval_output: The output of the last :pyeval command.
|
|
|
|
"""
|
2014-02-17 12:23:52 +01:00
|
|
|
|
2014-06-21 23:05:26 +02:00
|
|
|
from PyQt5.QtNetwork import QNetworkReply
|
|
|
|
|
|
|
|
import qutebrowser
|
2014-08-26 19:10:14 +02:00
|
|
|
from qutebrowser.network import schemehandler
|
2014-08-29 06:41:18 +02:00
|
|
|
from qutebrowser.utils import version, utils, jinja
|
2014-08-26 19:10:14 +02:00
|
|
|
from qutebrowser.utils import log as logutils
|
2014-02-07 11:01:58 +01:00
|
|
|
|
|
|
|
|
2014-05-07 09:03:40 +02:00
|
|
|
pyeval_output = ":pyeval was never called"
|
2014-02-07 11:01:58 +01:00
|
|
|
|
|
|
|
|
2014-08-26 19:10:14 +02:00
|
|
|
class QuteSchemeHandler(schemehandler.SchemeHandler):
|
2014-02-20 23:08:27 +01:00
|
|
|
|
2014-02-21 07:18:04 +01:00
|
|
|
"""Scheme handler for qute: URLs."""
|
2014-02-20 23:08:27 +01:00
|
|
|
|
2014-04-22 17:53:27 +02:00
|
|
|
def createRequest(self, _op, request, _outgoing_data):
|
2014-02-20 23:08:27 +01:00
|
|
|
"""Create a new request.
|
|
|
|
|
|
|
|
Args:
|
2014-02-21 07:34:49 +01:00
|
|
|
request: const QNetworkRequest & req
|
2014-04-22 17:53:27 +02:00
|
|
|
_op: Operation op
|
|
|
|
_outgoing_data: QIODevice * outgoingData
|
2014-02-20 23:08:27 +01:00
|
|
|
|
|
|
|
Return:
|
|
|
|
A QNetworkReply.
|
|
|
|
"""
|
2014-06-20 16:33:01 +02:00
|
|
|
path = request.url().path()
|
|
|
|
# An url like "qute:foo" is split as "scheme:path", not "scheme:host".
|
2014-06-20 23:26:19 +02:00
|
|
|
logutils.misc.debug("url: {}, path: {}".format(
|
2014-06-20 19:50:44 +02:00
|
|
|
request.url().toDisplayString(), path))
|
2014-04-22 15:46:46 +02:00
|
|
|
try:
|
2014-06-20 16:33:01 +02:00
|
|
|
handler = getattr(QuteHandlers, path)
|
2014-06-21 22:40:31 +02:00
|
|
|
except AttributeError:
|
2014-06-21 23:10:47 +02:00
|
|
|
errorstr = "No handler found for {}!".format(
|
|
|
|
request.url().toDisplayString())
|
2014-08-26 19:10:14 +02:00
|
|
|
return schemehandler.ErrorNetworkReply(
|
|
|
|
request, errorstr, QNetworkReply.ContentNotFoundError,
|
|
|
|
self.parent())
|
2014-04-22 15:46:46 +02:00
|
|
|
else:
|
|
|
|
data = handler()
|
2014-08-26 19:10:14 +02:00
|
|
|
return schemehandler.SpecialNetworkReply(
|
|
|
|
request, data, 'text/html', self.parent())
|
2014-02-20 23:08:27 +01:00
|
|
|
|
|
|
|
|
2014-02-21 07:18:04 +01:00
|
|
|
class QuteHandlers:
|
2014-02-07 20:21:50 +01:00
|
|
|
|
2014-02-21 07:18:04 +01:00
|
|
|
"""Handlers for qute:... pages."""
|
2014-02-07 11:01:58 +01:00
|
|
|
|
|
|
|
@classmethod
|
2014-06-20 16:33:01 +02:00
|
|
|
def pyeval(cls):
|
2014-02-21 07:18:04 +01:00
|
|
|
"""Handler for qute:pyeval. Return HTML content as bytes."""
|
2014-08-29 06:41:18 +02:00
|
|
|
html = jinja.env.get_template('pre.html').render(
|
|
|
|
title='pyeval', content=pyeval_output)
|
|
|
|
return html.encode('UTF-8', errors='xmlcharrefreplace')
|
2014-02-07 11:01:58 +01:00
|
|
|
|
|
|
|
@classmethod
|
2014-06-20 16:33:01 +02:00
|
|
|
def version(cls):
|
2014-02-21 07:18:04 +01:00
|
|
|
"""Handler for qute:version. Return HTML content as bytes."""
|
2014-08-29 06:41:18 +02:00
|
|
|
html = jinja.env.get_template('version.html').render(
|
|
|
|
title='Version info', version=version.version(),
|
|
|
|
copyright=qutebrowser.__copyright__)
|
|
|
|
return html.encode('UTF-8', errors='xmlcharrefreplace')
|
2014-05-25 20:26:26 +02:00
|
|
|
|
|
|
|
@classmethod
|
2014-06-25 10:00:27 +02:00
|
|
|
def plainlog(cls):
|
2014-08-29 06:41:18 +02:00
|
|
|
"""Handler for qute:plainlog. Return HTML content as bytes."""
|
2014-06-20 23:26:19 +02:00
|
|
|
if logutils.ram_handler is None:
|
2014-06-19 12:06:36 +02:00
|
|
|
text = "Log output was disabled."
|
|
|
|
else:
|
2014-08-29 06:41:18 +02:00
|
|
|
text = logutils.ram_handler.dump_log()
|
|
|
|
html = jinja.env.get_template('pre.html').render(
|
|
|
|
title='log', content=text)
|
|
|
|
return html.encode('UTF-8', errors='xmlcharrefreplace')
|
2014-05-27 15:33:44 +02:00
|
|
|
|
2014-06-25 10:00:27 +02:00
|
|
|
@classmethod
|
|
|
|
def log(cls):
|
|
|
|
"""Handler for qute:log. Return HTML content as bytes."""
|
|
|
|
if logutils.ram_handler is None:
|
2014-08-29 06:58:13 +02:00
|
|
|
html_log = None
|
2014-06-25 10:00:27 +02:00
|
|
|
else:
|
2014-08-29 06:58:13 +02:00
|
|
|
html_log = logutils.ram_handler.dump_log(html=True)
|
|
|
|
html = jinja.env.get_template('log.html').render(
|
|
|
|
title='log', content=html_log)
|
|
|
|
return html.encode('UTF-8', errors='xmlcharrefreplace')
|
2014-06-25 10:00:27 +02:00
|
|
|
|
2014-05-27 15:33:44 +02:00
|
|
|
@classmethod
|
2014-06-20 16:33:01 +02:00
|
|
|
def gpl(cls):
|
2014-05-27 15:33:44 +02:00
|
|
|
"""Handler for qute:gpl. Return HTML content as bytes."""
|
2014-08-26 19:10:14 +02:00
|
|
|
return utils.read_file('html/COPYING.html').encode('ASCII')
|