qutebrowser/qutebrowser/network/qutescheme.py

107 lines
2.9 KiB
Python
Raw Normal View History

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-02-21 07:18:04 +01:00
"""Handler functions for different qute:... pages."""
2014-02-17 12:23:52 +01:00
2014-02-21 07:35:53 +01:00
import logging
from qutebrowser.network.schemehandler import (SchemeHandler,
SpecialNetworkReply)
2014-02-10 15:01:05 +01:00
from qutebrowser.utils.version import version
2014-02-21 07:18:04 +01:00
from qutebrowser.utils.url import urlstring
2014-02-07 11:01:58 +01:00
2014-02-19 11:04:13 +01:00
_HTML_TEMPLATE = """
2014-02-07 11:01:58 +01:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{title}</title>
</head>
<body>
{body}
</body>
</html>
"""
pyeval_output = None
def _get_html(title, snippet):
"""Add HTML boilerplate to a html snippet.
2014-02-19 10:58:32 +01:00
Args:
title: The title the page should have.
snippet: The html snippet.
Return:
HTML content as bytes.
2014-02-07 11:01:58 +01:00
"""
2014-02-19 11:04:13 +01:00
return _HTML_TEMPLATE.format(title=title, body=snippet).encode('UTF-8')
2014-02-07 11:01:58 +01:00
2014-02-21 07:18:04 +01:00
class QuteSchemeHandler(SchemeHandler):
2014-02-21 07:18:04 +01:00
"""Scheme handler for qute: URLs."""
2014-02-21 19:51:04 +01:00
def _transform_url(self, url):
"""Transform a special URL to an QuteHandlers method name.
Args:
url: The URL as string.
Return:
The method name qute_*
"""
return url.replace('http://', '').replace('qute:', 'qute_')
2014-04-16 11:05:58 +02:00
def createRequest(self, op, request, outgoing_data):
"""Create a new request.
Args:
op: Operation op
2014-02-21 07:34:49 +01:00
request: const QNetworkRequest & req
outgoing_data: QIODevice * outgoingData
Return:
A QNetworkReply.
"""
2014-04-16 11:05:58 +02:00
# pylint: disable=unused-argument
# FIXME handle unknown pages
# FIXME adjust URLutils based on handlers
2014-02-21 07:35:53 +01:00
logging.debug('request: {}'.format(request))
2014-02-21 19:51:04 +01:00
url = urlstring(request.url())
handler = getattr(QuteHandlers, self._transform_url(url))
data = handler()
return 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-02-21 07:18:04 +01:00
def qute_pyeval(cls):
"""Handler for qute:pyeval. Return HTML content as bytes."""
2014-02-07 11:01:58 +01:00
return _get_html('pyeval', '<pre>{}</pre>'.format(pyeval_output))
@classmethod
2014-02-21 07:18:04 +01:00
def qute_version(cls):
"""Handler for qute:version. Return HTML content as bytes."""
2014-02-07 17:20:55 +01:00
return _get_html('Version', '<pre>{}</pre>'.format(version()))