Clean up :messages implementation.

- Add log.LOG_LEVELS to map names to levels (instead of using
  logging._levelToName)
- Test that log pages do not contain messages below the requested level
- Use pythons urllib.parse.parse_qs instead of Qt's UrlQuery
- Document tab, bg, window args for :messages
- Clean up style
This commit is contained in:
Ryan Roden-Corrent 2016-05-12 07:47:20 -04:00
parent c36f760114
commit fcd233a675
5 changed files with 31 additions and 13 deletions

View File

@ -1348,7 +1348,12 @@ class CommandDispatcher:
level: Include messages with `level` or higher severity.
Valid values: vdebug, debug, info, warning, error, critical.
plain: Whether to show plaintext (as opposed to html).
tab: Open in a new tab.
bg: Open in a background tab.
window: Open in a new window.
"""
if level.upper() not in log.LOG_LEVELS:
raise cmdexc.CommandError("Invalid log level {}!".format(level))
if plain:
url = QUrl('qute://plainlog?level={}'.format(level))
else:

View File

@ -26,8 +26,9 @@ Module attributes:
import functools
import configparser
import mimetypes
import urllib.parse
from PyQt5.QtCore import pyqtSlot, QObject, QUrlQuery
from PyQt5.QtCore import pyqtSlot, QObject
from PyQt5.QtNetwork import QNetworkReply
import qutebrowser
@ -158,36 +159,34 @@ def qute_version(_win_id, _request):
@add_handler('plainlog')
def qute_plainlog(_win_id, _request):
def qute_plainlog(_win_id, request):
"""Handler for qute:plainlog. Return HTML content as bytes.
An optional query parameter specifies the min log level to print.
An optional query parameter specifies the minimum log level to print.
For example, qute://log?level=warning prints warnings and errors.
Level can be one of: vdebug, debug, info, warning, error, critical.
"""
if log.ram_handler is None:
text = "Log output was disabled."
else:
query = QUrlQuery(_request.url().query())
level = query.queryItemValue('level')
level = urllib.parse.parse_qs(request.url().query()).get('level')[0]
text = log.ram_handler.dump_log(html=False, level=level)
html = jinja.render('pre.html', title='log', content=text)
return html.encode('UTF-8', errors='xmlcharrefreplace')
@add_handler('log')
def qute_log(_win_id, _request):
def qute_log(_win_id, request):
"""Handler for qute:log. Return HTML content as bytes.
An optional query parameter specifies the min log level to print.
An optional query parameter specifies the minimum log level to print.
For example, qute://log?level=warning prints warnings and errors.
Level can be one of: vdebug, debug, info, warning, error, critical.
"""
if log.ram_handler is None:
html_log = None
else:
query = QUrlQuery(_request.url().query())
level = query.queryItemValue('level')
level = urllib.parse.parse_qs(request.url().query()).get('level')[0]
html_log = log.ram_handler.dump_log(html=True, level=level)
html = jinja.render('log.html', title='log', content=html_log)

View File

@ -64,13 +64,21 @@ LOG_COLORS = {
'CRITICAL': 'red',
}
# We first monkey-patch logging to support our VDEBUG level before getting the
# loggers. Based on http://stackoverflow.com/a/13638084
VDEBUG_LEVEL = 9
logging.addLevelName(VDEBUG_LEVEL, 'VDEBUG')
logging.VDEBUG = VDEBUG_LEVEL
LOG_LEVELS = {
'VDEBUG': logging.VDEBUG,
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'ERROR': logging.ERROR,
'CRITICAL': logging.CRITICAL,
}
def vdebug(self, msg, *args, **kwargs):
"""Log with a VDEBUG level.
@ -436,8 +444,7 @@ class RAMHandler(logging.Handler):
(probably obsolete when moving to a widget for logging,
https://github.com/The-Compiler/qutebrowser/issues/34
"""
# pylint: disable=protected-access
minlevel = logging._nameToLevel.get(level.upper(), VDEBUG_LEVEL)
minlevel = LOG_LEVELS.get(level.upper(), VDEBUG_LEVEL)
lines = []
fmt = self.html_formatter.format if html else self.format
self.acquire()

View File

@ -368,7 +368,7 @@ def check_contents_plain(quteproc, text):
@bdd.then(bdd.parsers.parse('the page should not contain the plaintext '
'"{text}"'))
def check_contents_plain(quteproc, text):
def check_not_contents_plain(quteproc, text):
"""Check the current page's content based on a substring."""
content = quteproc.get_content().strip()
assert text not in content

View File

@ -378,6 +378,8 @@ Feature: Various utility commands.
Then the error "the-error-message" should be shown
And the warning "the-warning-message" should be shown
And the page should contain the plaintext "the-error-message"
And the page should not contain the plaintext "the-warning-message"
And the page should not contain the plaintext "the-info-message"
Scenario: Showing messages of type 'warning' or greater
When I run :message-error the-error-message
@ -388,6 +390,7 @@ Feature: Various utility commands.
And the warning "the-warning-message" should be shown
And the page should contain the plaintext "the-error-message"
And the page should contain the plaintext "the-warning-message"
And the page should not contain the plaintext "the-info-message"
Scenario: Showing messages of type 'info' or greater
When I run :message-error the-error-message
@ -399,3 +402,7 @@ Feature: Various utility commands.
And the page should contain the plaintext "the-error-message"
And the page should contain the plaintext "the-warning-message"
And the page should contain the plaintext "the-info-message"
Scenario: Showing messages of an invalid level
When I run :messages cataclysmic
Then the error "Invalid log level cataclysmic!" should be shown