qutebrowser/qutebrowser/utils/objreg.py

177 lines
5.2 KiB
Python
Raw Normal View History

2014-09-24 07:06:45 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2014 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 <http://www.gnu.org/licenses/>.
"""The global object registry and related utility functions."""
import collections
import functools
from PyQt5.QtCore import QObject
2014-09-24 07:06:45 +02:00
class UnsetObject:
"""Class for an unset object.
Only used (rather than object) so we can tell pylint to shut up about it.
"""
__slots__ = ()
2014-09-25 07:43:50 +02:00
class RegistryUnavailableError(Exception):
"""Exception raised when a certain registry does not exist yet."""
pass
_UNSET = UnsetObject()
2014-09-24 07:06:45 +02:00
class ObjectRegistry(collections.UserDict):
"""A registry of long-living objects in qutebrowser.
Inspired by the eric IDE code (E5Gui/E5Application.py).
"""
def __setitem__(self, name, obj):
"""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))
2014-09-24 07:06:45 +02:00
if isinstance(obj, QObject):
obj.destroyed.connect(functools.partial(self.on_destroyed, name))
super().__setitem__(name, obj)
def on_destroyed(self, name):
"""Remove a destroyed QObject."""
try:
del self[name]
except KeyError:
pass
def dump_objects(self):
"""Dump all objects as a list of strings."""
lines = []
for name, obj in self.data.items():
lines.append("{}: {}".format(name, repr(obj)))
return lines
# The registry for global objects
global_registry = ObjectRegistry()
# The object registry of object registries.
meta_registry = ObjectRegistry()
meta_registry['global'] = global_registry
2014-09-28 22:13:14 +02:00
# The window registry.
window_registry = ObjectRegistry()
2014-09-28 22:13:14 +02:00
def _get_registry(scope, window):
"""Get the correct registry for a given scope."""
2014-09-28 22:13:14 +02:00
if window is not None and scope is not 'window':
raise TypeError("window is set with scope {}".format(scope))
if scope == 'global':
return global_registry
2014-09-25 07:43:50 +02:00
elif scope == 'tab':
2014-09-28 22:13:14 +02:00
app = get('app')
win = app.activeWindow()
tabbed_browser = get('tabbed-browser', scope='window', window=win)
widget = tabbed_browser.currentWidget()
2014-09-25 07:43:50 +02:00
if widget is None:
raise RegistryUnavailableError(scope)
try:
return widget.registry
except AttributeError:
raise RegistryUnavailableError(scope)
2014-09-28 22:13:14 +02:00
elif scope == 'window':
if window is None:
raise TypeError("window is None with scope window!")
if window is 'current':
app = get('app')
win = app.activeWindow()
if win is None:
raise RegistryUnavailableError(scope)
else:
try:
win = window_registry[window]
except KeyError:
raise RegistryUnavailableError(scope)
try:
return win.registry
except AttributeError:
raise RegistryUnavailableError(scope)
2014-09-25 07:45:30 +02:00
elif scope == 'meta':
return meta_registry
else:
raise ValueError("Invalid scope '{}'!".format(scope))
2014-09-28 22:13:14 +02:00
def get(name, default=_UNSET, scope='global', window=None):
2014-09-24 07:06:45 +02:00
"""Helper function to get an object.
Args:
default: A default to return if the object does not exist.
"""
2014-09-28 22:13:14 +02:00
reg = _get_registry(scope, window)
2014-09-24 07:06:45 +02:00
try:
return reg[name]
2014-09-24 07:06:45 +02:00
except KeyError:
if default is not _UNSET:
return default
else:
raise
2014-09-28 22:13:14 +02:00
def register(name, obj, update=False, scope=None, registry=None, window=None):
2014-09-24 07:06:45 +02:00
"""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'
2014-09-28 22:13:14 +02:00
reg = _get_registry(scope, window)
if not update and name in reg:
2014-09-24 07:06:45 +02:00
raise KeyError("Object '{}' is already registered ({})!".format(
name, repr(reg[name])))
reg[name] = obj
2014-09-24 07:06:45 +02:00
2014-09-28 22:13:14 +02:00
def delete(name, scope='global', window=None):
2014-09-24 07:06:45 +02:00
"""Helper function to unregister an object."""
2014-09-28 22:13:14 +02:00
reg = _get_registry(scope, window)
del reg[name]