qutebrowser/qutebrowser/network/qutescheme.py

132 lines
4.6 KiB
Python
Raw Normal View History

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/>.
#
# pylint complains when using .render() on jinja templates, so we make it shut
# up for this whole module.
# pylint: disable=maybe-no-member
2014-02-07 11:01:58 +01:00
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
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
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-21 07:18:04 +01:00
"""Scheme handler for qute: URLs."""
2014-04-22 17:53:27 +02:00
def createRequest(self, _op, request, _outgoing_data):
"""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
Return:
A QNetworkReply.
"""
path = request.url().path()
2014-09-07 23:22:38 +02:00
host = request.url().host()
# An url like "qute:foo" is split as "scheme:path", not "scheme:host".
2014-09-07 23:22:38 +02:00
logutils.misc.debug("url: {}, path: {}, host {}".format(
request.url().toDisplayString(), path, host))
try:
handler = getattr(QuteHandlers, path)
2014-06-21 22:40:31 +02:00
except AttributeError:
2014-09-07 23:22:38 +02:00
try:
handler = getattr(QuteHandlers, host)
except AttributeError:
errorstr = "No handler found for {}!".format(
request.url().toDisplayString())
return schemehandler.ErrorNetworkReply(
request, errorstr, QNetworkReply.ContentNotFoundError,
self.parent())
else:
data = handler(request)
else:
2014-09-07 23:22:38 +02:00
data = handler(request)
2014-08-26 19:10:14 +02:00
return schemehandler.SpecialNetworkReply(
request, data, 'text/html', self.parent())
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-09-07 23:22:38 +02:00
def pyeval(cls, _request):
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-09-07 23:22:38 +02:00
def version(cls, _request):
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-09-07 23:22:38 +02:00
def plainlog(cls, _request):
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:
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
2014-09-07 23:22:38 +02:00
def log(cls, _request):
2014-06-25 10:00:27 +02:00
"""Handler for qute:log. Return HTML content as bytes."""
if logutils.ram_handler is None:
html_log = None
2014-06-25 10:00:27 +02:00
else:
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-09-07 23:22:38 +02:00
def gpl(cls, _request):
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')
2014-09-07 23:22:38 +02:00
@classmethod
def help(cls, request):
"""Handler for qute:help. Return HTML content as bytes."""
path = 'html/doc/{}'.format(request.url().path())
2014-09-08 06:57:22 +02:00
return utils.read_file(path).encode('UTF-8',
errors='xmlcharrefreplace')