qutebrowser/qutebrowser/utils/debug.py

280 lines
8.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:
2015-01-03 15:51:31 +01:00
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-03-03 21:22:20 +01:00
#
# 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/>.
"""Utilities used for debugging."""
2014-06-23 07:11:15 +02:00
import re
2014-10-11 11:40:15 +02:00
import inspect
2014-08-26 19:10:14 +02:00
import functools
import datetime
import contextlib
2014-03-03 21:22:20 +01:00
2015-08-11 17:09:20 +02:00
from PyQt5.QtCore import Qt, QEvent, QMetaMethod, QObject
2015-04-30 07:37:25 +02:00
from PyQt5.QtWidgets import QApplication
2014-03-03 21:22:20 +01:00
2015-04-30 07:37:25 +02:00
from qutebrowser.utils import log, utils, qtutils, objreg
2014-06-17 11:12:55 +02:00
2014-08-27 22:23:37 +02:00
2014-06-17 11:12:55 +02:00
def log_events(klass):
"""Class decorator to log Qt events."""
old_event = klass.event
2014-08-26 19:10:14 +02:00
@functools.wraps(old_event)
2014-06-17 11:12:55 +02:00
def new_event(self, e, *args, **kwargs):
"""Wrapper for event() which logs events."""
2014-10-08 21:11:04 +02:00
log.misc.debug("Event in {}: {}".format(utils.qualname(klass),
2014-08-26 20:15:41 +02:00
qenum_key(QEvent, e.type())))
2014-06-17 11:12:55 +02:00
return old_event(self, e, *args, **kwargs)
klass.event = new_event
return klass
2014-10-11 11:40:15 +02:00
def log_signals(obj):
"""Log all signals of an object or class.
Can be used as class decorator.
"""
def log_slot(obj, signal, *args):
2014-10-14 07:59:07 +02:00
"""Slot connected to a signal to log it."""
2014-10-11 11:40:15 +02:00
dbg = dbg_signal(signal, args)
2014-11-25 19:51:53 +01:00
try:
r = repr(obj)
2015-08-11 17:11:00 +02:00
except RuntimeError: # pragma: no cover
2014-11-25 19:51:53 +01:00
r = '<deleted>'
2015-04-13 08:49:04 +02:00
log.signals.debug("Signal in {}: {}".format(r, dbg))
2014-10-11 11:40:15 +02:00
def connect_log_slot(obj):
2014-10-14 07:59:07 +02:00
"""Helper function to connect all signals to a logging slot."""
2014-10-11 11:40:15 +02:00
metaobj = obj.metaObject()
for i in range(metaobj.methodCount()):
meta_method = metaobj.method(i)
qtutils.ensure_valid(meta_method)
if meta_method.methodType() == QMetaMethod.Signal:
name = bytes(meta_method.name()).decode('ascii')
if name != 'destroyed':
signal = getattr(obj, name)
signal.connect(functools.partial(log_slot, obj, signal))
2014-10-11 11:40:15 +02:00
if inspect.isclass(obj):
old_init = obj.__init__
@functools.wraps(old_init)
def new_init(self, *args, **kwargs):
"""Wrapper for __init__() which logs signals."""
ret = old_init(self, *args, **kwargs)
connect_log_slot(self)
return ret
obj.__init__ = new_init
return obj
else:
connect_log_slot(obj)
2014-08-07 07:35:05 +02:00
def qenum_key(base, value, add_base=False, klass=None):
2014-06-15 11:11:08 +02:00
"""Convert a Qt Enum value to its key as a string.
Args:
base: The object the enum is in, e.g. QFrame.
value: The value to get.
2014-08-07 07:35:05 +02:00
add_base: Whether the base should be added to the printed name.
klass: The enum class the value belongs to.
If None, the class will be auto-guessed.
2014-06-15 11:11:08 +02:00
Return:
2014-08-07 07:35:05 +02:00
The key associated with the value as a string if it could be found.
The original value as a string if not.
2014-06-15 11:11:08 +02:00
"""
2014-08-07 07:35:05 +02:00
if klass is None:
klass = value.__class__
if klass == int:
raise TypeError("Can't guess enum class of an int!")
2014-06-15 11:11:08 +02:00
try:
idx = base.staticMetaObject.indexOfEnumerator(klass.__name__)
ret = base.staticMetaObject.enumerator(idx).valueToKey(value)
2014-06-15 11:11:08 +02:00
except AttributeError:
ret = None
if ret is None:
2014-06-15 11:11:08 +02:00
for name, obj in vars(base).items():
if isinstance(obj, klass) and obj == value:
2014-08-07 07:35:05 +02:00
ret = name
break
else:
ret = '0x{:04x}'.format(int(value))
2014-08-07 07:35:05 +02:00
if add_base and hasattr(base, '__name__'):
return '.'.join([base.__name__, ret])
else:
return ret
2014-06-23 07:11:15 +02:00
2014-08-07 14:41:39 +02:00
def qflags_key(base, value, add_base=False, klass=None):
"""Convert a Qt QFlags value to its keys as string.
Note: Passing a combined value (such as Qt.AlignCenter) will get the names
for the individual bits (e.g. Qt.AlignVCenter | Qt.AlignHCenter). FIXME
2014-10-01 22:23:27 +02:00
https://github.com/The-Compiler/qutebrowser/issues/42
2014-08-07 14:41:39 +02:00
Args:
base: The object the flags are in, e.g. QtCore.Qt
value: The value to get.
add_base: Whether the base should be added to the printed names.
klass: The flags class the value belongs to.
If None, the class will be auto-guessed.
Return:
The keys associated with the flags as a '|' separated string if they
could be found. Hex values as a string if not.
"""
if klass is None:
# We have to store klass here because it will be lost when iterating
# over the bits.
klass = value.__class__
if klass == int:
raise TypeError("Can't guess enum class of an int!")
bits = []
names = []
mask = 0x01
value = int(value)
while mask < value:
if value & mask:
bits.append(mask)
mask <<= 1
for bit in bits:
# We have to re-convert to an enum type here or we'll sometimes get an
# empty string back.
names.append(qenum_key(base, klass(bit), add_base))
return '|'.join(names)
2014-06-23 07:11:15 +02:00
def signal_name(sig):
"""Get a cleaned up name of a signal.
Args:
sig: The pyqtSignal
Return:
The cleaned up signal name.
"""
m = re.match(r'[0-9]+(.*)\(.*\)', sig.signal)
return m.group(1)
2015-08-11 17:08:49 +02:00
def format_args(args=None, kwargs=None):
2014-09-03 08:57:21 +02:00
"""Format a list of arguments/kwargs to a function-call like string."""
if args is not None:
arglist = [utils.compact_text(repr(arg), 200) for arg in args]
2014-09-03 08:57:21 +02:00
else:
arglist = []
if kwargs is not None:
for k, v in kwargs.items():
arglist.append('{}={}'.format(k, utils.compact_text(repr(v), 200)))
2014-09-03 08:57:21 +02:00
return ', '.join(arglist)
2014-06-23 07:11:15 +02:00
def dbg_signal(sig, args):
"""Get a string representation of a signal for debugging.
Args:
sig: A pyqtSignal.
args: The arguments as list of strings.
Return:
A human-readable string representation of signal/args.
"""
2015-08-11 17:08:49 +02:00
return '{}({})'.format(signal_name(sig), format_args(args))
2014-09-03 08:57:21 +02:00
def format_call(func, args=None, kwargs=None, full=True):
"""Get a string representation of a function calls with the given args.
Args:
func: The callable to print.
args: A list of positional arguments.
kwargs: A dict of named arguments.
full: Whether to print the full name
Return:
A string with the function call.
"""
if full:
2014-10-08 21:11:04 +02:00
name = utils.qualname(func)
2014-09-03 08:57:21 +02:00
else:
name = func.__name__
2015-08-11 17:08:49 +02:00
return '{}({})'.format(name, format_args(args, kwargs))
@contextlib.contextmanager
def log_time(logger, action='operation'):
2015-03-17 06:39:02 +01:00
"""Log the time the operation in the with-block takes.
Args:
logger: The logging.Logger to use for logging.
action: A description of what's being done.
"""
started = datetime.datetime.now()
try:
yield
finally:
finished = datetime.datetime.now()
delta = (finished - started).total_seconds()
logger.debug("{} took {} seconds.".format(action.capitalize(), delta))
2015-04-30 07:37:25 +02:00
def _get_widgets():
"""Get a string list of all widgets."""
widgets = QApplication.instance().allWidgets()
widgets.sort(key=repr)
return [repr(w) for w in widgets]
def _get_pyqt_objects(lines, obj, depth=0):
"""Recursive method for get_all_objects to get Qt objects."""
2015-08-11 17:09:20 +02:00
for kid in obj.findChildren(QObject, '', Qt.FindDirectChildrenOnly):
2015-04-30 07:37:25 +02:00
lines.append(' ' * depth + repr(kid))
_get_pyqt_objects(lines, kid, depth + 1)
def get_all_objects(start_obj=None):
2015-04-30 07:37:25 +02:00
"""Get all children of an object recursively as a string."""
output = ['']
widget_lines = _get_widgets()
widget_lines = [' ' + e for e in widget_lines]
2015-08-11 17:10:41 +02:00
widget_lines.insert(0, "Qt widgets - {} objects:".format(
2015-04-30 07:37:25 +02:00
len(widget_lines)))
output += widget_lines
if start_obj is None:
start_obj = QApplication.instance()
2015-04-30 07:37:25 +02:00
pyqt_lines = []
_get_pyqt_objects(pyqt_lines, start_obj)
2015-04-30 07:37:25 +02:00
pyqt_lines = [' ' + e for e in pyqt_lines]
pyqt_lines.insert(0, 'Qt objects - {} objects:'.format(
len(pyqt_lines)))
2015-08-11 17:10:41 +02:00
2015-04-30 07:37:25 +02:00
output += ['']
2015-08-11 17:10:41 +02:00
output += pyqt_lines
2015-04-30 07:37:25 +02:00
output += objreg.dump_objects()
return '\n'.join(output)