qutebrowser/qutebrowser/network/qutescheme.py

157 lines
5.5 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-09-23 04:22:51 +02:00
from qutebrowser.utils import version, utils, jinja, log, message, docutils
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".
log.misc.debug("url: {}, path: {}, host {}".format(
2014-09-07 23:22:38 +02:00
request.url().toDisplayString(), path, host))
try:
handler = HANDLERS[path]
except KeyError:
2014-09-07 23:22:38 +02:00
try:
handler = HANDLERS[host]
except KeyError:
2014-09-07 23:22:38 +02:00
errorstr = "No handler found for {}!".format(
request.url().toDisplayString())
return schemehandler.ErrorNetworkReply(
request, errorstr, QNetworkReply.ContentNotFoundError,
self.parent())
2014-09-12 20:10:13 +02:00
try:
2014-10-06 08:27:15 +02:00
data = handler(self._win_id, request)
2014-09-12 20:10:13 +02:00
except IOError as e:
return schemehandler.ErrorNetworkReply(
request, str(e), QNetworkReply.ContentNotFoundError,
self.parent())
2014-08-26 19:10:14 +02:00
return schemehandler.SpecialNetworkReply(
request, data, 'text/html', self.parent())
2014-09-28 22:13:14 +02:00
def qute_pyeval(_win_id, _request):
"""Handler for qute:pyeval. Return HTML content as bytes."""
html = jinja.env.get_template('pre.html').render(
title='pyeval', content=pyeval_output)
return html.encode('UTF-8', errors='xmlcharrefreplace')
2014-09-28 22:13:14 +02:00
def qute_version(_win_id, _request):
"""Handler for qute:version. Return HTML content as bytes."""
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-09-28 22:13:14 +02:00
def qute_plainlog(_win_id, _request):
"""Handler for qute:plainlog. Return HTML content as bytes."""
if log.ram_handler is None:
text = "Log output was disabled."
else:
text = log.ram_handler.dump_log()
html = jinja.env.get_template('pre.html').render(title='log', content=text)
return html.encode('UTF-8', errors='xmlcharrefreplace')
2014-09-28 22:13:14 +02:00
def qute_log(_win_id, _request):
"""Handler for qute:log. Return HTML content as bytes."""
if log.ram_handler is None:
html_log = None
else:
html_log = log.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-09-28 22:13:14 +02:00
def qute_gpl(_win_id, _request):
"""Handler for qute:gpl. Return HTML content as bytes."""
return utils.read_file('html/COPYING.html').encode('ASCII')
2014-09-28 22:13:14 +02:00
def qute_help(win_id, request):
"""Handler for qute:help. Return HTML content as bytes."""
2014-09-21 21:21:41 +02:00
try:
utils.read_file('html/doc/index.html')
except FileNotFoundError:
html = jinja.env.get_template('error.html').render(
title="Error while loading documentation",
url=request.url().toDisplayString(),
error="This most likely means the documentation was not generated "
"properly. If you are running qutebrowser from the git "
"repository, please run scripts/asciidoc2html.py."
"If you're running a released version this is a bug, please "
"use :report to report it.",
icon='')
return html.encode('UTF-8', errors='xmlcharrefreplace')
urlpath = request.url().path()
if not urlpath or urlpath == '/':
urlpath = 'index.html'
else:
urlpath = urlpath.lstrip('/')
2014-09-23 04:22:51 +02:00
if not docutils.docs_up_to_date(urlpath):
2014-09-28 22:13:14 +02:00
message.error(win_id, "Your documentation is outdated! Please re-run "
"scripts/asciidoc2html.py.")
path = 'html/doc/{}'.format(urlpath)
return utils.read_file(path).encode('UTF-8', errors='xmlcharrefreplace')
HANDLERS = {
'pyeval': qute_pyeval,
'version': qute_version,
'plainlog': qute_plainlog,
'log': qute_log,
'gpl': qute_gpl,
'help': qute_help,
}