summaryrefslogtreecommitdiff
path: root/qutebrowser/utils/objreg.py
blob: 99d8a0936c2f6a0cbb889baaf0a6435606328c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2014-2021 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 <https://www.gnu.org/licenses/>.

"""The global object registry and related utility functions."""


import collections
import functools
from typing import (TYPE_CHECKING, Any, Callable, MutableMapping, MutableSequence,
                    Optional, Sequence, Union)

from PyQt5.QtCore import QObject, QTimer
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget

from qutebrowser.utils import log, usertypes, utils
if TYPE_CHECKING:
    from qutebrowser.mainwindow import mainwindow


_WindowTab = Union[str, int, None]


class RegistryUnavailableError(Exception):

    """Exception raised when a certain registry does not exist yet."""


class NoWindow(Exception):

    """Exception raised by last_window if no window is available."""


class CommandOnlyError(Exception):

    """Raised when an object is requested which is used for commands only."""


_IndexType = Union[str, int]


class ObjectRegistry(collections.UserDict):

    """A registry of long-living objects in qutebrowser.

    Inspired by the eric IDE code (E5Gui/E5Application.py).

    Attributes:
        _partial_objs: A dictionary of the connected partial objects.
        command_only: Objects which are only registered for commands.
    """

    def __init__(self) -> None:
        super().__init__()
        self._partial_objs: MutableMapping[_IndexType, Callable[[], None]] = {}
        self.command_only: MutableSequence[str] = []

    def __setitem__(self, name: _IndexType, obj: Any) -> None:
        """Register an object in the object registry.

        Sets a slot to remove QObjects when they are destroyed.
        """
        if name is None:
            raise TypeError("Registering '{}' with name 'None'!".format(obj))
        if obj is None:
            raise TypeError("Registering object None with name '{}'!".format(
                name))

        self._disconnect_destroyed(name)

        if isinstance(obj, QObject):
            func = functools.partial(self.on_destroyed, name)
            obj.destroyed.connect(func)
            self._partial_objs[name] = func

        super().__setitem__(name, obj)

    def __delitem__(self, name: str) -> None:
        """Extend __delitem__ to disconnect the destroyed signal."""
        self._disconnect_destroyed(name)
        super().__delitem__(name)

    def _disconnect_destroyed(self, name: _IndexType) -> None:
        """Disconnect the destroyed slot if it was connected."""
        try:
            partial_objs = self._partial_objs
        except AttributeError:
            # This sometimes seems to happen on CI during
            # test_history.test_adding_item_during_async_read
            # and I have no idea why...
            return
        if name in partial_objs:
            func = partial_objs[name]
            try:
                self[name].destroyed.disconnect(func)
            except RuntimeError:
                # If C++ has deleted the object, the slot is already
                # disconnected.
                pass
            del partial_objs[name]

    def on_destroyed(self, name: str) -> None:
        """Schedule removing of a destroyed QObject.

        We don't remove the destroyed object immediately because it might still
        be destroying its children, which might still use the object
        registry.
        """
        log.destroy.debug("schedule removal: {}".format(name))
        QTimer.singleShot(0, functools.partial(self._on_destroyed, name))

    def _on_destroyed(self, name: str) -> None:
        """Remove a destroyed QObject."""
        log.destroy.debug("removed: {}".format(name))
        if not hasattr(self, 'data'):
            # This sometimes seems to happen on CI during
            # test_history.test_adding_item_during_async_read
            # and I have no idea why...
            return
        try:
            del self[name]
            del self._partial_objs[name]
        except KeyError:
            pass

    def dump_objects(self) -> Sequence[str]:
        """Dump all objects as a list of strings."""
        lines = []
        for name, obj in self.data.items():
            try:
                obj_repr = repr(obj)
            except RuntimeError:
                # Underlying object deleted probably
                obj_repr = '<deleted>'
            suffix = (" (for commands only)" if name in self.command_only
                      else "")
            lines.append("{}: {}{}".format(name, obj_repr, suffix))
        return lines


# The registry for global objects
global_registry = ObjectRegistry()
# The window registry.
window_registry = ObjectRegistry()


def _get_tab_registry(win_id: _WindowTab,
                      tab_id: _WindowTab) -> ObjectRegistry:
    """Get the registry of a tab."""
    if tab_id is None:
        raise ValueError("Got tab_id None (win_id {})".format(win_id))
    if tab_id == 'current' and win_id is None:
        window: Optional[QWidget] = QApplication.activeWindow()
        if window is None or not hasattr(window, 'win_id'):
            raise RegistryUnavailableError('tab')
        win_id = window.win_id
    elif win_id is None:
        raise TypeError("window is None with scope tab!")

    if tab_id == 'current':
        tabbed_browser = get('tabbed-browser', scope='window', window=win_id)
        tab = tabbed_browser.widget.currentWidget()
        if tab is None:
            raise RegistryUnavailableError('window')
        tab_id = tab.tab_id
    tab_registry = get('tab-registry', scope='window', window=win_id)
    try:
        return tab_registry[tab_id].registry
    except AttributeError:
        raise RegistryUnavailableError('tab')


def _get_window_registry(window: _WindowTab) -> ObjectRegistry:
    """Get the registry of a window."""
    if window is None:
        raise TypeError("window is None with scope window!")
    try:
        if window == 'current':
            win: Optional[QWidget] = QApplication.activeWindow()
        elif window == 'last-focused':
            win = last_focused_window()
        else:
            win = window_registry[window]
    except (KeyError, NoWindow):
        win = None

    if win is None:
        raise RegistryUnavailableError('window')

    try:
        return win.registry
    except AttributeError:
        raise RegistryUnavailableError('window')


def _get_registry(scope: str,
                  window: _WindowTab = None,
                  tab: _WindowTab = None) -> ObjectRegistry:
    """Get the correct registry for a given scope."""
    if window is not None and scope not in ['window', 'tab']:
        raise TypeError("window is set with scope {}".format(scope))
    if tab is not None and scope != 'tab':
        raise TypeError("tab is set with scope {}".format(scope))
    if scope == 'global':
        return global_registry
    elif scope == 'tab':
        return _get_tab_registry(window, tab)
    elif scope == 'window':
        return _get_window_registry(window)
    else:
        raise ValueError("Invalid scope '{}'!".format(scope))


def get(name: str,
        default: Any = usertypes.UNSET,
        scope: str = 'global',
        window: _WindowTab = None,
        tab: _WindowTab = None,
        from_command: bool = False) -> Any:
    """Helper function to get an object.

    Args:
        default: A default to return if the object does not exist.
    """
    reg = _get_registry(scope, window, tab)
    if name in reg.command_only and not from_command:
        raise CommandOnlyError("{} is only registered for commands"
                               .format(name))

    try:
        return reg[name]
    except KeyError:
        if default is not usertypes.UNSET:
            return default
        else:
            raise


def register(name: str,
             obj: Any,
             update: bool = False,
             scope: str = None,
             registry: ObjectRegistry = None,
             window: _WindowTab = None,
             tab: _WindowTab = None,
             command_only: bool = False) -> None:
    """Helper function to register an object.

    Args:
        name: The name the object will be registered as.
        obj: The object to register.
        update: If True, allows to update an already registered object.
    """
    if scope is not None and registry is not None:
        raise ValueError("scope ({}) and registry ({}) can't be given at the "
                         "same time!".format(scope, registry))

    if registry is not None:
        reg = registry
    else:
        if scope is None:
            scope = 'global'
        reg = _get_registry(scope, window, tab)

    if not update and name in reg:
        raise KeyError("Object '{}' is already registered ({})!".format(
            name, repr(reg[name])))
    reg[name] = obj

    if command_only:
        reg.command_only.append(name)


def delete(name: str,
           scope: str = 'global',
           window: _WindowTab = None,
           tab: _WindowTab = None) -> None:
    """Helper function to unregister an object."""
    reg = _get_registry(scope, window, tab)
    del reg[name]


def dump_objects() -> Sequence[str]:
    """Get all registered objects in all registries as a string."""
    blocks = []
    lines = []
    blocks.append(('global', global_registry.dump_objects()))
    for win_id in window_registry:
        registry = _get_registry('window', window=win_id)
        blocks.append(('window-{}'.format(win_id), registry.dump_objects()))
        tab_registry = get('tab-registry', scope='window', window=win_id)
        for tab_id, tab in tab_registry.items():
            dump = tab.registry.dump_objects()
            data = ['    ' + line for line in dump]
            blocks.append(('    tab-{}'.format(tab_id), data))
    for name, block_data in blocks:
        lines.append("")
        lines.append("{} object registry - {} objects:".format(
            name, len(block_data)))
        for line in block_data:
            lines.append("    {}".format(line))
    return lines


def last_visible_window() -> 'mainwindow.MainWindow':
    """Get the last visible window, or the last focused window if none."""
    try:
        window = get('last-visible-main-window')
    except KeyError:
        return last_focused_window()
    if window.tabbed_browser.is_shutting_down:
        return last_focused_window()
    return window


def last_focused_window() -> 'mainwindow.MainWindow':
    """Get the last focused window, or the last window if none."""
    try:
        window = get('last-focused-main-window')
    except KeyError:
        return last_opened_window()
    if window.tabbed_browser.is_shutting_down:
        return last_opened_window()
    return window


def _window_by_index(idx: int) -> 'mainwindow.MainWindow':
    """Get the Nth opened window object."""
    if not window_registry:
        raise NoWindow()
    key = sorted(window_registry)[idx]
    return window_registry[key]


def last_opened_window() -> 'mainwindow.MainWindow':
    """Get the last opened window object."""
    if not window_registry:
        raise NoWindow()
    for idx in range(-1, -(len(window_registry)+1), -1):
        window = _window_by_index(idx)
        if not window.tabbed_browser.is_shutting_down:
            return window
    raise utils.Unreachable()


def first_opened_window() -> 'mainwindow.MainWindow':
    """Get the first opened window object."""
    if not window_registry:
        raise NoWindow()
    for idx in range(0, len(window_registry)+1):
        window = _window_by_index(idx)
        if not window.tabbed_browser.is_shutting_down:
            return window
    raise utils.Unreachable()