From 5996651a2f0fcf481b5ae7b7020fd56ae9142f3d Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 23 Jun 2014 07:11:15 +0200 Subject: [PATCH] Merge utils.signals into utils.debug --- qutebrowser/browser/signalfilter.py | 2 +- qutebrowser/test/utils/test_debug.py | 36 +++++++++++++++ qutebrowser/test/utils/test_signal.py | 63 --------------------------- qutebrowser/utils/debug.py | 29 ++++++++++++ qutebrowser/utils/signals.py | 51 ---------------------- 5 files changed, 66 insertions(+), 115 deletions(-) delete mode 100644 qutebrowser/test/utils/test_signal.py delete mode 100644 qutebrowser/utils/signals.py diff --git a/qutebrowser/browser/signalfilter.py b/qutebrowser/browser/signalfilter.py index a610fb340..91a9d6fe8 100644 --- a/qutebrowser/browser/signalfilter.py +++ b/qutebrowser/browser/signalfilter.py @@ -23,7 +23,7 @@ from functools import partial from PyQt5.QtCore import QObject -from qutebrowser.utils.signals import dbg_signal +from qutebrowser.utils.debug import dbg_signal from qutebrowser.widgets.webview import WebView from qutebrowser.utils.log import signals as logger diff --git a/qutebrowser/test/utils/test_debug.py b/qutebrowser/test/utils/test_debug.py index 4f5610ca7..57115f2fb 100644 --- a/qutebrowser/test/utils/test_debug.py +++ b/qutebrowser/test/utils/test_debug.py @@ -27,6 +27,14 @@ from PyQt5.QtWidgets import QStyle, QFrame import qutebrowser.utils.debug as debug +class FakeSignal: + + """Fake pyqtSignal stub which uses a mock to see if it was called.""" + + def __init__(self, name='fake'): + self.signal = '2{}(int, int)'.format(name) + + class QEnumKeyTests(TestCase): """Tests for qenum_key.""" @@ -45,5 +53,33 @@ class QEnumKeyTests(TestCase): self.assertEqual(key, 'Sunken') +class TestDebug(TestCase): + + """Test signal debug output functions.""" + + def setUp(self): + self.signal = FakeSignal() + + def test_signal_name(self): + """Test signal_name().""" + self.assertEqual(debug.signal_name(self.signal), 'fake') + + def test_dbg_signal(self): + """Test dbg_signal().""" + self.assertEqual(debug.dbg_signal(self.signal, [23, 42]), + 'fake(23, 42)') + + def test_dbg_signal_eliding(self): + """Test eliding in dbg_signal().""" + self.assertEqual(debug.dbg_signal(self.signal, + [12345678901234567890123]), + 'fake(1234567890123456789\u2026)') + + def test_dbg_signal_newline(self): + """Test dbg_signal() with a newline.""" + self.assertEqual(debug.dbg_signal(self.signal, ['foo\nbar']), + 'fake(foo bar)') + + if __name__ == '__main__': unittest.main() diff --git a/qutebrowser/test/utils/test_signal.py b/qutebrowser/test/utils/test_signal.py deleted file mode 100644 index 3c59260ac..000000000 --- a/qutebrowser/test/utils/test_signal.py +++ /dev/null @@ -1,63 +0,0 @@ -# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et - -# Copyright 2014 Florian Bruhin (The Compiler) -# -# 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 . - -# pylint: disable=missing-docstring - -"""Tests for signal utils.""" - -import unittest -from unittest import TestCase - -import qutebrowser.utils.signals as sigutils - - -class FakeSignal: - - """Fake pyqtSignal stub which uses a mock to see if it was called.""" - - def __init__(self, name='fake'): - self.signal = '2{}(int, int)'.format(name) - - -class TestDebug(TestCase): - - """Test signal debug output functions.""" - - def setUp(self): - self.signal = FakeSignal() - - def test_signal_name(self): - self.assertEqual(sigutils.signal_name(self.signal), 'fake') - - def test_dbg_signal(self): - self.assertEqual(sigutils.dbg_signal(self.signal, [23, 42]), - 'fake(23, 42)') - - def test_dbg_signal_eliding(self): - self.assertEqual(sigutils.dbg_signal(self.signal, - [12345678901234567890123]), - 'fake(1234567890123456789\u2026)') - - def test_dbg_signal_newline(self): - self.assertEqual(sigutils.dbg_signal(self.signal, ['foo\nbar']), - 'fake(foo bar)') - - -if __name__ == '__main__': - unittest.main() diff --git a/qutebrowser/utils/debug.py b/qutebrowser/utils/debug.py index 2f952c460..0a9f450c1 100644 --- a/qutebrowser/utils/debug.py +++ b/qutebrowser/utils/debug.py @@ -19,12 +19,14 @@ """Utilities used for debugging.""" +import re import sys import types from functools import wraps from PyQt5.QtCore import pyqtRemoveInputHook, QEvent, QCoreApplication +from qutebrowser.utils.misc import elide from qutebrowser.utils.log import misc as logger @@ -146,3 +148,30 @@ def qenum_key(base, value): if isinstance(obj, klass) and obj == value: return name return None + + +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) + + +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. + """ + argstr = ', '.join([elide(str(a).replace('\n', ' '), 20) for a in args]) + return '{}({})'.format(signal_name(sig), argstr) diff --git a/qutebrowser/utils/signals.py b/qutebrowser/utils/signals.py deleted file mode 100644 index d30e09f08..000000000 --- a/qutebrowser/utils/signals.py +++ /dev/null @@ -1,51 +0,0 @@ -# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: - -# Copyright 2014 Florian Bruhin (The Compiler) -# -# 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 . - -"""Utilities regarding signals.""" - -import re - -from qutebrowser.utils.misc import elide - - -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) - - -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. - """ - argstr = ', '.join([elide(str(a).replace('\n', ' '), 20) for a in args]) - return '{}({})'.format(signal_name(sig), argstr)