From 36fd2e5c7f6ed93c0c7652b35979e639e41ea223 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 26 Sep 2014 15:48:24 +0200 Subject: [PATCH] Improve __repr__ methods. --- qutebrowser/app.py | 2 +- qutebrowser/browser/cache.py | 6 ++--- qutebrowser/browser/commands.py | 4 ++-- qutebrowser/browser/cookies.py | 3 +-- qutebrowser/browser/downloads.py | 4 ++-- qutebrowser/browser/webelem.py | 5 +++-- qutebrowser/commands/runners.py | 2 +- qutebrowser/config/config.py | 6 ++--- qutebrowser/config/iniparsers.py | 6 ++--- qutebrowser/config/keyconfparser.py | 6 ++--- qutebrowser/config/lineparser.py | 8 +++---- qutebrowser/keyinput/basekeyparser.py | 5 ++--- qutebrowser/keyinput/keyparser.py | 6 ++--- qutebrowser/keyinput/modeman.py | 5 +++-- qutebrowser/keyinput/modeparsers.py | 6 ++--- qutebrowser/models/downloadmodel.py | 4 ++-- qutebrowser/test/config/test_configtypes.py | 14 ++++++------ qutebrowser/test/utils/test_utils.py | 5 +++-- .../test/utils/usertypes/test_neighborlist.py | 5 ----- qutebrowser/utils/completer.py | 4 ++-- qutebrowser/utils/message.py | 4 ++-- qutebrowser/utils/readline.py | 3 ++- qutebrowser/utils/rfc6266.py | 6 ++--- qutebrowser/utils/usertypes.py | 10 +++++---- qutebrowser/utils/utils.py | 22 +++++++++++++++++++ qutebrowser/widgets/completion.py | 4 ++-- qutebrowser/widgets/console.py | 5 ++--- qutebrowser/widgets/crash.py | 2 +- qutebrowser/widgets/downloads.py | 5 ++--- qutebrowser/widgets/mainwindow.py | 4 ++-- qutebrowser/widgets/misc.py | 5 +++-- qutebrowser/widgets/statusbar/bar.py | 4 ++-- qutebrowser/widgets/statusbar/progress.py | 3 ++- qutebrowser/widgets/statusbar/prompt.py | 4 ++-- qutebrowser/widgets/statusbar/prompter.py | 6 +++-- qutebrowser/widgets/statusbar/textbase.py | 4 ++-- qutebrowser/widgets/tabbedbrowser.py | 3 +-- qutebrowser/widgets/tabwidget.py | 5 ++--- qutebrowser/widgets/webview.py | 4 ++-- 39 files changed, 115 insertions(+), 94 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 08c343eaa..8e388ced8 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -150,7 +150,7 @@ class Application(QApplication): self._crashdlg.raise_() def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) def _init_config(self): """Inizialize and read the config.""" diff --git a/qutebrowser/browser/cache.py b/qutebrowser/browser/cache.py index e9362cc2a..0ed86a8de 100644 --- a/qutebrowser/browser/cache.py +++ b/qutebrowser/browser/cache.py @@ -39,6 +39,6 @@ class DiskCache(QNetworkDiskCache): self.setMaximumCacheSize(config.get('storage', 'cache-size')) def __repr__(self): - return '<{} size={}, maxsize={}, path="{}">'.format( - self.__class__.__name__, self.cacheSize(), self.maximumCacheSize(), - self.cacheDirectory()) + return utils.get_repr(self, size=self.cacheSize(), + maxsize=self.maximumCacheSize(), + path=self.cacheDirectory()) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index abdd53d71..5f27bc5fa 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -38,7 +38,7 @@ from qutebrowser.commands import userscripts, cmdexc, cmdutils from qutebrowser.config import config from qutebrowser.browser import quickmarks, webelem from qutebrowser.utils import (message, editor, usertypes, log, qtutils, - urlutils, objreg) + urlutils, objreg, utils) class CommandDispatcher: @@ -59,7 +59,7 @@ class CommandDispatcher: self._editor = None def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) def _count(self): """Convenience method to get the widget count.""" diff --git a/qutebrowser/browser/cookies.py b/qutebrowser/browser/cookies.py index 992764c6a..a900b643a 100644 --- a/qutebrowser/browser/cookies.py +++ b/qutebrowser/browser/cookies.py @@ -41,8 +41,7 @@ class CookieJar(QNetworkCookieJar): self.setAllCookies(cookies) def __repr__(self): - return '<{} count={}>'.format( - self.__class__.__name__, len(self.allCookies())) + return utils.get_repr(self, count=len(self.allCookies())) def purge_old_cookies(self): """Purge expired cookies from the cookie jar.""" diff --git a/qutebrowser/browser/downloads.py b/qutebrowser/browser/downloads.py index 498eb9592..46c3ea7db 100644 --- a/qutebrowser/browser/downloads.py +++ b/qutebrowser/browser/downloads.py @@ -108,7 +108,7 @@ class DownloadItem(QObject): self.timer.start() def __repr__(self): - return '<{} "{}">'.format(self.__class__.__name__, self.basename) + return utils.get_repr(self, basename=self.basename) def __str__(self): """Get the download as a string. @@ -349,7 +349,7 @@ class DownloadManager(QObject): self.questions = [] def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self, downloads=len(self.downloads)) @pyqtSlot('QUrl', 'QWebPage') def get(self, url, page): diff --git a/qutebrowser/browser/webelem.py b/qutebrowser/browser/webelem.py index f0ec7b5f7..a8aa31f29 100644 --- a/qutebrowser/browser/webelem.py +++ b/qutebrowser/browser/webelem.py @@ -117,9 +117,10 @@ class WebElementWrapper(collections.abc.MutableMapping): def __repr__(self): try: - return "".format(self.debug_text()) + html = self.debug_text() except IsNullError: - return "" + html = None + return utils.get_repr(self, html=html) def __getitem__(self, key): self._check_vanished() diff --git a/qutebrowser/commands/runners.py b/qutebrowser/commands/runners.py index 9821c9706..69db0067b 100644 --- a/qutebrowser/commands/runners.py +++ b/qutebrowser/commands/runners.py @@ -62,7 +62,7 @@ class SearchRunner(QObject): self._flags = 0 def __repr__(self): - return '<{} text={}>'.format(self.__class__.__name__, self._text) + return utils.get_repr(self, text=self._text, flags=self._flags) def _search(self, text, rev=False): """Search for a text on the current page. diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index f76ab7415..bf7732e04 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -35,7 +35,7 @@ from PyQt5.QtCore import pyqtSignal, QObject from qutebrowser.utils import log from qutebrowser.config import configdata, iniparsers, configtypes, textwrapper from qutebrowser.commands import cmdexc, cmdutils -from qutebrowser.utils import message, objreg +from qutebrowser.utils import message, objreg, utils from qutebrowser.utils.usertypes import Completion @@ -129,7 +129,7 @@ class ConfigManager(QObject): return self._proxies[key] def __repr__(self): - return '<{} {}>'.format(self.__class__.__name__, self._fname) + return utils.get_repr(self, fname=self._fname) def __str__(self): """Get the whole config as a string.""" @@ -458,7 +458,7 @@ class SectionProxy(collections.abc.MutableMapping): self.name = name def __repr__(self): - return '<{} {}>'.format(self.__class__.__name__, self.name) + return utils.get_repr(self, name=self.name) def __getitem__(self, key): if not self.conf.has_option(self.name, key): diff --git a/qutebrowser/config/iniparsers.py b/qutebrowser/config/iniparsers.py index e9e2cdb87..45ff5ea3a 100644 --- a/qutebrowser/config/iniparsers.py +++ b/qutebrowser/config/iniparsers.py @@ -23,7 +23,7 @@ import os import os.path import configparser -from qutebrowser.utils import log +from qutebrowser.utils import log, utils class ReadConfigParser(configparser.ConfigParser): @@ -54,8 +54,8 @@ class ReadConfigParser(configparser.ConfigParser): self.read(self._configfile, encoding='utf-8') def __repr__(self): - return '{}("{}", "{}")'.format( - self.__class__.__name__, self._configdir, self._fname) + return utils.get_repr(self, constructor=True, + configdir=self._configdir, fname=self._fname) class ReadWriteConfigParser(ReadConfigParser): diff --git a/qutebrowser/config/keyconfparser.py b/qutebrowser/config/keyconfparser.py index 03e8f73bd..ac96597d0 100644 --- a/qutebrowser/config/keyconfparser.py +++ b/qutebrowser/config/keyconfparser.py @@ -26,7 +26,7 @@ from PyQt5.QtCore import pyqtSignal, QObject from qutebrowser.config import configdata, textwrapper from qutebrowser.commands import cmdutils, cmdexc -from qutebrowser.utils import log +from qutebrowser.utils import log, utils class KeyConfigError(Exception): @@ -99,8 +99,8 @@ class KeyConfigParser(QObject): return '\n'.join(lines) + '\n' def __repr__(self): - return '{}("{}", "{}")'.format( - self.__class__.__name__, self._configdir, self._fname) + return utils.get_repr(self, constructor=True, + configdir=self._configdir, fname=self._fname) def _str_section_desc(self, sectname): """Get the section description string for sectname.""" diff --git a/qutebrowser/config/lineparser.py b/qutebrowser/config/lineparser.py index 18e5b61a4..ba360f44c 100644 --- a/qutebrowser/config/lineparser.py +++ b/qutebrowser/config/lineparser.py @@ -24,7 +24,7 @@ import os.path from PyQt5.QtCore import pyqtSlot -from qutebrowser.utils import log +from qutebrowser.utils import log, utils class LineConfigParser: @@ -60,9 +60,9 @@ class LineConfigParser: self.read(self._configfile) def __repr__(self): - return '{}("{}", "{}", limit={}, binary={})'.format( - self.__class__.__name__, self._configdir, self._fname, self._limit, - self._binary) + return utils.get_repr(self, constructor=True, + configdir=self._configdir, fname=self._fname, + limit=self._limit, binary=self._binary) def __iter__(self): """Iterate over the set data.""" diff --git a/qutebrowser/keyinput/basekeyparser.py b/qutebrowser/keyinput/basekeyparser.py index 77bc61b98..472dd708a 100644 --- a/qutebrowser/keyinput/basekeyparser.py +++ b/qutebrowser/keyinput/basekeyparser.py @@ -88,9 +88,8 @@ class BaseKeyParser(QObject): self.special_bindings = {} def __repr__(self): - return '<{} supports_count={}, supports_chains={}>'.format( - self.__class__.__name__, self._supports_count, - self._supports_chains) + return utils.get_repr(self, supports_count=self._supports_count, + supports_chains=self._supports_chains) def _debug_log(self, message): """Log a message to the debug log if logging is active. diff --git a/qutebrowser/keyinput/keyparser.py b/qutebrowser/keyinput/keyparser.py index 4c9254eb7..dca4145a4 100644 --- a/qutebrowser/keyinput/keyparser.py +++ b/qutebrowser/keyinput/keyparser.py @@ -20,7 +20,7 @@ """Advanced keyparsers.""" from qutebrowser.keyinput.basekeyparser import BaseKeyParser -from qutebrowser.utils import message +from qutebrowser.utils import message, utils from qutebrowser.commands import runners, cmdexc @@ -70,5 +70,5 @@ class PassthroughKeyParser(CommandKeyParser): self._mode = mode def __repr__(self): - return '<{} mode={}, warn={})'.format( - self.__class__.__name__, self._mode, self._warn_on_keychains) + return utils.get_repr(self, mode=self._mode, + warn=self._warn_on_keychains) diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index c61a0be10..7f1e2e21f 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -29,7 +29,7 @@ from PyQt5.QtWidgets import QApplication from qutebrowser.config import config from qutebrowser.commands import cmdexc, cmdutils -from qutebrowser.utils import usertypes, log, objreg +from qutebrowser.utils import usertypes, log, objreg, utils class ModeLockedError(Exception): @@ -107,7 +107,8 @@ class ModeManager(QObject): 'forward-unbound-keys') def __repr__(self): - return '<{} mode={}>'.format(self.__class__.__name__, self.mode()) + return utils.get_repr(self, mode=self.mode(), locked=self.locked, + passthrough=self.passthrough) def mode(self): """Get the current mode..""" diff --git a/qutebrowser/keyinput/modeparsers.py b/qutebrowser/keyinput/modeparsers.py index 5a25a2cba..cc1d2ae98 100644 --- a/qutebrowser/keyinput/modeparsers.py +++ b/qutebrowser/keyinput/modeparsers.py @@ -28,7 +28,7 @@ from PyQt5.QtCore import pyqtSlot, Qt from qutebrowser.utils import message from qutebrowser.config import config from qutebrowser.keyinput import keyparser -from qutebrowser.utils import usertypes, log, objreg +from qutebrowser.utils import usertypes, log, objreg, utils STARTCHARS = ":/?" @@ -44,7 +44,7 @@ class NormalKeyParser(keyparser.CommandKeyParser): self.read_config('normal') def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) def _handle_single_key(self, e): """Override _handle_single_key to abort if the key is a startchar. @@ -73,7 +73,7 @@ class PromptKeyParser(keyparser.CommandKeyParser): self.read_config('prompt') def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) class HintKeyParser(keyparser.CommandKeyParser): diff --git a/qutebrowser/models/downloadmodel.py b/qutebrowser/models/downloadmodel.py index 4f0a15954..1d3550722 100644 --- a/qutebrowser/models/downloadmodel.py +++ b/qutebrowser/models/downloadmodel.py @@ -23,7 +23,7 @@ from PyQt5.QtCore import (pyqtSlot, Qt, QVariant, QAbstractListModel, QModelIndex) from qutebrowser.config import config -from qutebrowser.utils import usertypes, qtutils, objreg +from qutebrowser.utils import usertypes, qtutils, objreg, utils Role = usertypes.enum('Role', 'item', start=Qt.UserRole, is_int=True) @@ -49,7 +49,7 @@ class DownloadModel(QAbstractListModel): download_manager.data_changed.connect(self.on_data_changed) def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self, count=self.rowCount()) @pyqtSlot(int) def on_data_changed(self, idx): diff --git a/qutebrowser/test/config/test_configtypes.py b/qutebrowser/test/config/test_configtypes.py index 6c3d5b9b7..ffbcd5951 100644 --- a/qutebrowser/test/config/test_configtypes.py +++ b/qutebrowser/test/config/test_configtypes.py @@ -27,7 +27,7 @@ from unittest import mock from qutebrowser.config import configtypes from qutebrowser.test import stubs -from qutebrowser.utils import debug +from qutebrowser.utils import debug, utils from PyQt5.QtCore import QUrl from PyQt5.QtGui import QColor, QFont @@ -41,9 +41,9 @@ class Font(QFont): def __repr__(self): weight = debug.qenum_key(QFont, self.weight(), add_base=True, klass=QFont.Weight) - return ''.format( - self.family(), self.pointSize(), self.pixelSize(), weight, - self.style()) + return utils.get_repr(self, family=self.family(), pt=self.pointSize(), + px=self.pixelSize(), weight=weight, + style=self.style()) @classmethod def fromdesc(cls, desc): @@ -65,9 +65,9 @@ class NetworkProxy(QNetworkProxy): """A QNetworkProxy with a nicer repr().""" def __repr__(self): - return 'QNetworkProxy({}, "{}", {}, "{}", "{}")'.format( - self.type(), self.hostName(), self.port(), self.user(), - self.password()) + return utils.get_repr(self, type=self.type(), hostName=self.hostName(), + port=self.port(), user=self.user(), + password=self.password()) class ValidValuesTest(unittest.TestCase): diff --git a/qutebrowser/test/utils/test_utils.py b/qutebrowser/test/utils/test_utils.py index 7c4a4d905..a421cf8f5 100644 --- a/qutebrowser/test/utils/test_utils.py +++ b/qutebrowser/test/utils/test_utils.py @@ -39,8 +39,9 @@ class Color(QColor): """A QColor with a nicer repr().""" def __repr__(self): - return 'Color({}, {}, {}, {})'.format( - self.red(), self.green(), self.blue(), self.alpha()) + return utils.get_repr(self, constructor=True, red=self.red(), + green=self.green(), blue=self.blue(), + alpha=self.alpha()) class ElidingTests(unittest.TestCase): diff --git a/qutebrowser/test/utils/usertypes/test_neighborlist.py b/qutebrowser/test/utils/usertypes/test_neighborlist.py index 3dd59b5ed..d74b765bf 100644 --- a/qutebrowser/test/utils/usertypes/test_neighborlist.py +++ b/qutebrowser/test/utils/usertypes/test_neighborlist.py @@ -49,11 +49,6 @@ class InitTests(unittest.TestCase): nl = usertypes.NeighborList([1, 2, 3]) self.assertEqual(len(nl), 3) - def test_repr(self): - """Test repr() on NeighborList.""" - nl = usertypes.NeighborList([1, 2, 3]) - self.assertEqual(repr(nl), 'NeighborList([1, 2, 3])') - def test_contains(self): """Test 'in' on NeighborList.""" nl = usertypes.NeighborList([1, 2, 3]) diff --git a/qutebrowser/utils/completer.py b/qutebrowser/utils/completer.py index b573804e4..8f5136bc5 100644 --- a/qutebrowser/utils/completer.py +++ b/qutebrowser/utils/completer.py @@ -23,7 +23,7 @@ from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject from qutebrowser.config import config, configdata from qutebrowser.commands import cmdutils -from qutebrowser.utils import usertypes, log, objreg +from qutebrowser.utils import usertypes, log, objreg, utils from qutebrowser.models import completion as models from qutebrowser.models.completionfilter import CompletionFilterModel as CFM @@ -59,7 +59,7 @@ class Completer(QObject): self._init_setting_completions() def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) def _model(self): """Convienience method to get the current completion model.""" diff --git a/qutebrowser/utils/message.py b/qutebrowser/utils/message.py index 7d11c84d3..52b661c10 100644 --- a/qutebrowser/utils/message.py +++ b/qutebrowser/utils/message.py @@ -21,7 +21,7 @@ from PyQt5.QtCore import pyqtSignal, QObject, QTimer -from qutebrowser.utils import usertypes, log, objreg +from qutebrowser.utils import usertypes, log, objreg, utils def error(message, immediately=False): @@ -152,7 +152,7 @@ class MessageBridge(QObject): s_question = pyqtSignal(usertypes.Question, bool) def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) def _emit_later(self, signal, *args): """Emit a message later when the mainloop is not busy anymore. diff --git a/qutebrowser/utils/readline.py b/qutebrowser/utils/readline.py index e38a10457..44b25c901 100644 --- a/qutebrowser/utils/readline.py +++ b/qutebrowser/utils/readline.py @@ -23,6 +23,7 @@ from PyQt5.QtWidgets import QApplication, QLineEdit from qutebrowser.commands import cmdutils from qutebrowser.utils import usertypes as typ +from qutebrowser.utils import utils class ReadlineBridge: @@ -37,7 +38,7 @@ class ReadlineBridge: self._deleted = {} def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) def _widget(self): """Get the currently active QLineEdit.""" diff --git a/qutebrowser/utils/rfc6266.py b/qutebrowser/utils/rfc6266.py index 281a58008..79b1f9d40 100644 --- a/qutebrowser/utils/rfc6266.py +++ b/qutebrowser/utils/rfc6266.py @@ -26,7 +26,7 @@ import re import pypeg2 as peg -from qutebrowser.utils import log +from qutebrowser.utils import log, utils class UniqueNamespace(peg.Namespace): @@ -279,8 +279,8 @@ class ContentDisposition: return self.disposition.lower() == 'inline' def __repr__(self): - return 'ContentDisposition(%r, %r)' % ( - self.disposition, self.assocs) + return utils.get_repr(self, constructor=True, + disposition=self.disposition, assocs=self.assocs) def normalize_ws(text): diff --git a/qutebrowser/utils/usertypes.py b/qutebrowser/utils/usertypes.py index 7c480836c..79b933af4 100644 --- a/qutebrowser/utils/usertypes.py +++ b/qutebrowser/utils/usertypes.py @@ -30,7 +30,7 @@ import enum as pyenum from PyQt5.QtCore import pyqtSignal, QObject, QTimer -from qutebrowser.utils import log, qtutils +from qutebrowser.utils import log, qtutils, utils _UNSET = object() @@ -103,7 +103,8 @@ class NeighborList(collections.abc.Sequence): return len(self._items) def __repr__(self): - return '{}({})'.format(self.__class__.__name__, self._items) + return utils.get_repr(self, items=self._items, mode=self._mode, + idx=self._idx, fuzzyval=self.fuzzyval) def _snap_in(self, offset): """Set the current item to the closest item to self.fuzzyval. @@ -296,7 +297,8 @@ class Question(QObject): self.is_aborted = False def __repr__(self): - return '<{} "{}">'.format(self.__class__.__name__, self.text) + return utils.get_repr(self, text=self.text, mode=self._mode, + default=self.default) @property def mode(self): @@ -359,7 +361,7 @@ class Timer(QTimer): self._name = name def __repr__(self): - return '<{} {}>'.format(self.__class__.__name__, self._name) + return utils.get_repr(self, name=self._name) def setInterval(self, msec): """Extend setInterval to check for overflows.""" diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 4a64e5341..1ee0abbe8 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -582,3 +582,25 @@ def is_enum(obj): return issubclass(obj, enum.Enum) except TypeError: return False + + +def get_repr(obj, constructor=False, **attrs): + """Get a suitable __repr__ string for an object. + + Args: + obj: The object to get a repr for. + constructor: If True, show the Foo(one=1, two=2) form instead of + . + attrs: The attributes to add. + """ + cls = getattr(obj.__class__, '__qualname__', obj.__class__.__name__) + parts = [] + for name, val in attrs.items(): + parts.append('{}={!r}'.format(name, val)) + if constructor: + return '{}({})'.format(cls, ', '.join(parts)) + else: + if parts: + return '<{} {}>'.format(cls, ' '.join(parts)) + else: + return '<{}>'.format(cls) diff --git a/qutebrowser/widgets/completion.py b/qutebrowser/widgets/completion.py index 8a3b9a28a..5598520fa 100644 --- a/qutebrowser/widgets/completion.py +++ b/qutebrowser/widgets/completion.py @@ -29,7 +29,7 @@ from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QItemSelectionModel from qutebrowser.commands import cmdutils from qutebrowser.config import config, style from qutebrowser.widgets import completiondelegate -from qutebrowser.utils import completer, usertypes, qtutils, objreg +from qutebrowser.utils import completer, usertypes, qtutils, objreg, utils class CompletionView(QTreeView): @@ -116,7 +116,7 @@ class CompletionView(QTreeView): # FIXME set elidemode def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) def _resize_columns(self): """Resize the completion columns based on COLUMN_WIDTHS.""" diff --git a/qutebrowser/widgets/console.py b/qutebrowser/widgets/console.py index f971bd17a..65b203090 100644 --- a/qutebrowser/widgets/console.py +++ b/qutebrowser/widgets/console.py @@ -162,7 +162,7 @@ class ConsoleTextEdit(QTextEdit): self.setFocusPolicy(Qt.NoFocus) def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) def on_config_changed(self, section, option): """Update font when config changed.""" @@ -193,8 +193,7 @@ class ConsoleWidget(QWidget): self._lineedit.setFocus() def __repr__(self): - return '<{}, visible={}>'.format( - self.__class__.__name__, self.isVisible()) + return utils.get_repr(self, visible=self.isVisible()) @pyqtSlot(str, str) def on_config_changed(self, section, option): diff --git a/qutebrowser/widgets/crash.py b/qutebrowser/widgets/crash.py index 9a6fe6167..1c80dfe36 100644 --- a/qutebrowser/widgets/crash.py +++ b/qutebrowser/widgets/crash.py @@ -78,7 +78,7 @@ class _CrashDialog(QDialog): self._init_buttons() def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) def _init_text(self): """Initialize the main text to be displayed on an exception. diff --git a/qutebrowser/widgets/downloads.py b/qutebrowser/widgets/downloads.py index 6e1a22b1b..0964ccbe8 100644 --- a/qutebrowser/widgets/downloads.py +++ b/qutebrowser/widgets/downloads.py @@ -24,7 +24,7 @@ from PyQt5.QtWidgets import QListView, QSizePolicy, QMenu from qutebrowser.models import downloadmodel from qutebrowser.config import style -from qutebrowser.utils import qtutils +from qutebrowser.utils import qtutils, utils class DownloadView(QListView): @@ -64,8 +64,7 @@ class DownloadView(QListView): self.customContextMenuRequested.connect(self.show_context_menu) def __repr__(self): - return '<{} with {} downloads>'.format(self.__class__.__name__, - self.model().rowCount()) + return utils.get_repr(self, count=self.model().rowCount()) @pyqtSlot('QPoint') def show_context_menu(self, point): diff --git a/qutebrowser/widgets/mainwindow.py b/qutebrowser/widgets/mainwindow.py index ea1955ee5..e3086719e 100644 --- a/qutebrowser/widgets/mainwindow.py +++ b/qutebrowser/widgets/mainwindow.py @@ -27,7 +27,7 @@ from PyQt5.QtWidgets import QWidget, QVBoxLayout from qutebrowser.commands import cmdutils from qutebrowser.config import config -from qutebrowser.utils import message, log, usertypes, qtutils, objreg +from qutebrowser.utils import message, log, usertypes, qtutils, objreg, utils from qutebrowser.widgets import tabbedbrowser, completion, downloads from qutebrowser.widgets.statusbar import bar @@ -100,7 +100,7 @@ class MainWindow(QWidget): #QtCore.QMetaObject.connectSlotsByName(MainWindow) def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) def _connect_resize_completion(self): """Connect the resize_completion signal and resize it once.""" diff --git a/qutebrowser/widgets/misc.py b/qutebrowser/widgets/misc.py index a9d18de7f..6232848b0 100644 --- a/qutebrowser/widgets/misc.py +++ b/qutebrowser/widgets/misc.py @@ -24,6 +24,7 @@ from PyQt5.QtWidgets import QLineEdit from PyQt5.QtGui import QValidator from qutebrowser.models import cmdhistory +from qutebrowser.utils import utils class MinimalLineEditMixin: @@ -41,7 +42,7 @@ class MinimalLineEditMixin: self.setAttribute(Qt.WA_MacShowFocusRect, False) def __repr__(self): - return '<{} "{}">'.format(self.__class__.__name__, self.text()) + return utils.get_repr(self, text=self.text()) class CommandLineEdit(QLineEdit): @@ -94,7 +95,7 @@ class CommandLineEdit(QLineEdit): self.setSelection(self._promptlen, oldpos - self._promptlen) def __repr__(self): - return '<{} "{}">'.format(self.__class__.__name__, self.text()) + return utils.get_repr(self, text=self.text()) class _CommandValidator(QValidator): diff --git a/qutebrowser/widgets/statusbar/bar.py b/qutebrowser/widgets/statusbar/bar.py index c4a66020e..153ecffb8 100644 --- a/qutebrowser/widgets/statusbar/bar.py +++ b/qutebrowser/widgets/statusbar/bar.py @@ -26,7 +26,7 @@ from PyQt5.QtCore import pyqtSignal, pyqtSlot, pyqtProperty, Qt from PyQt5.QtWidgets import QWidget, QHBoxLayout, QStackedLayout, QSizePolicy from qutebrowser.config import config, style -from qutebrowser.utils import usertypes, log, objreg +from qutebrowser.utils import usertypes, log, objreg, utils from qutebrowser.widgets.statusbar import (command, progress, keystring, percentage, url, prompt) from qutebrowser.widgets.statusbar import text as textwidget @@ -170,7 +170,7 @@ class StatusBar(QWidget): self._hbox.addWidget(self.prog) def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) @pyqtProperty(bool) def error(self): diff --git a/qutebrowser/widgets/statusbar/progress.py b/qutebrowser/widgets/statusbar/progress.py index 67eebb55d..325ec4695 100644 --- a/qutebrowser/widgets/statusbar/progress.py +++ b/qutebrowser/widgets/statusbar/progress.py @@ -24,6 +24,7 @@ from PyQt5.QtWidgets import QProgressBar, QSizePolicy from qutebrowser.widgets import webview from qutebrowser.config import style +from qutebrowser.utils import utils class Progress(QProgressBar): @@ -52,7 +53,7 @@ class Progress(QProgressBar): self.hide() def __repr__(self): - return '<{} {}%>'.format(self.__class__.__name__, self.value()) + return utils.get_repr(self, value=self.value()) @pyqtSlot() def on_load_started(self): diff --git a/qutebrowser/widgets/statusbar/prompt.py b/qutebrowser/widgets/statusbar/prompt.py index fde0afe70..e5cc21f00 100644 --- a/qutebrowser/widgets/statusbar/prompt.py +++ b/qutebrowser/widgets/statusbar/prompt.py @@ -24,7 +24,7 @@ from PyQt5.QtWidgets import QHBoxLayout, QWidget, QLineEdit from qutebrowser.widgets import misc from qutebrowser.widgets.statusbar import textbase, prompter -from qutebrowser.utils import objreg +from qutebrowser.utils import objreg, utils class PromptLineEdit(misc.MinimalLineEditMixin, QLineEdit): @@ -70,4 +70,4 @@ class Prompt(QWidget): objreg.register('prompter', prompter_obj) def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self) diff --git a/qutebrowser/widgets/statusbar/prompter.py b/qutebrowser/widgets/statusbar/prompter.py index fa3119de1..872a957d3 100644 --- a/qutebrowser/widgets/statusbar/prompter.py +++ b/qutebrowser/widgets/statusbar/prompter.py @@ -26,7 +26,7 @@ from PyQt5.QtWidgets import QLineEdit from qutebrowser.keyinput import modeman from qutebrowser.commands import cmdutils -from qutebrowser.utils import usertypes, log, qtutils, objreg +from qutebrowser.utils import usertypes, log, qtutils, objreg, utils PromptContext = collections.namedtuple('PromptContext', @@ -69,7 +69,9 @@ class Prompter: self._busy = False def __repr__(self): - return '<{}>'.format(self.__class__.__name__) + return utils.get_repr(self, loops=len(self._loops), + question=self._question, queue=len(self._queue), + busy=self._busy) def _pop_later(self): """Helper to call self._pop as soon as everything else is done.""" diff --git a/qutebrowser/widgets/statusbar/textbase.py b/qutebrowser/widgets/statusbar/textbase.py index 92fe3a56d..e45b6b283 100644 --- a/qutebrowser/widgets/statusbar/textbase.py +++ b/qutebrowser/widgets/statusbar/textbase.py @@ -23,7 +23,7 @@ from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QLabel, QSizePolicy from PyQt5.QtGui import QPainter -from qutebrowser.utils import qtutils +from qutebrowser.utils import qtutils, utils class TextBase(QLabel): @@ -47,7 +47,7 @@ class TextBase(QLabel): self._elided_text = '' def __repr__(self): - return '<{} "{}">'.format(self.__class__.__name__, self.text()) + return utils.get_repr(self, text=self.text()) def _update_elided_text(self, width): """Update the elided text when necessary. diff --git a/qutebrowser/widgets/tabbedbrowser.py b/qutebrowser/widgets/tabbedbrowser.py index 0f51c2a07..54dfb7c34 100644 --- a/qutebrowser/widgets/tabbedbrowser.py +++ b/qutebrowser/widgets/tabbedbrowser.py @@ -112,8 +112,7 @@ class TabbedBrowser(tabwidget.TabWidget): self.setIconSize(QSize(12, 12)) def __repr__(self): - return '<{} with {} tabs>'.format(self.__class__.__name__, - self.count()) + return utils.get_repr(self, count=self.count()) def widgets(self): """Get a list of open tab widgets. diff --git a/qutebrowser/widgets/tabwidget.py b/qutebrowser/widgets/tabwidget.py index 096d2cec6..4ab4cde6b 100644 --- a/qutebrowser/widgets/tabwidget.py +++ b/qutebrowser/widgets/tabwidget.py @@ -31,7 +31,7 @@ from PyQt5.QtWidgets import (QTabWidget, QTabBar, QSizePolicy, QCommonStyle, QStyle, QStylePainter, QStyleOptionTab) from PyQt5.QtGui import QIcon, QPalette, QColor -from qutebrowser.utils import qtutils, objreg +from qutebrowser.utils import qtutils, objreg, utils from qutebrowser.config import config @@ -110,8 +110,7 @@ class TabBar(QTabBar): QTimer.singleShot(0, self._autohide) def __repr__(self): - return '<{} with {} tabs>'.format(self.__class__.__name__, - self.count()) + return utils.get_repr(self, count=self.count()) def _autohide(self): """Auto-hide the tabbar if needed.""" diff --git a/qutebrowser/widgets/webview.py b/qutebrowser/widgets/webview.py index cf589fe85..018c2ab36 100644 --- a/qutebrowser/widgets/webview.py +++ b/qutebrowser/widgets/webview.py @@ -112,8 +112,8 @@ class WebView(QWebView): self.viewing_source = False def __repr__(self): - url = self.url().toDisplayString() - return "WebView(url='{}')".format(utils.elide(url, 50)) + url = utils.elide(self.url().toDisplayString(), 50) + return utils.get_repr(self, url=url) def _set_load_status(self, val): """Setter for load_status.