Use a normal UserDict for ObjectRegistry.
This commit is contained in:
parent
ca2be960df
commit
dc7ad3e2de
@ -130,10 +130,10 @@ class Application(QApplication):
|
|||||||
utilcmds.init()
|
utilcmds.init()
|
||||||
log.init.debug("Initializing cookies...")
|
log.init.debug("Initializing cookies...")
|
||||||
cookiejar = cookies.CookieJar(self)
|
cookiejar = cookies.CookieJar(self)
|
||||||
self.obj.register('cookiejar', cookiejar)
|
self.obj['cookiejar'] = cookiejar
|
||||||
log.init.debug("Initializing cache...")
|
log.init.debug("Initializing cache...")
|
||||||
diskcache = cache.DiskCache(self)
|
diskcache = cache.DiskCache(self)
|
||||||
self.obj.register('cache', diskcache)
|
self.obj['cache'] = diskcache
|
||||||
log.init.debug("Initializing commands...")
|
log.init.debug("Initializing commands...")
|
||||||
self._commandrunner = runners.CommandRunner()
|
self._commandrunner = runners.CommandRunner()
|
||||||
log.init.debug("Initializing search...")
|
log.init.debug("Initializing search...")
|
||||||
@ -757,7 +757,7 @@ class Application(QApplication):
|
|||||||
if hasattr(self, 'stateconfig'):
|
if hasattr(self, 'stateconfig'):
|
||||||
to_save.append(("window geometry", self.stateconfig.save))
|
to_save.append(("window geometry", self.stateconfig.save))
|
||||||
try:
|
try:
|
||||||
cookiejar = self.obj.get('cookiejar')
|
cookiejar = self.obj['cookiejar']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
@ -24,6 +24,7 @@ Module attributes:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import operator
|
import operator
|
||||||
|
import collections
|
||||||
import collections.abc
|
import collections.abc
|
||||||
import enum as pyenum
|
import enum as pyenum
|
||||||
|
|
||||||
@ -383,7 +384,7 @@ class Timer(QTimer):
|
|||||||
super().start()
|
super().start()
|
||||||
|
|
||||||
|
|
||||||
class ObjectRegistry:
|
class ObjectRegistry(collections.UserDict):
|
||||||
|
|
||||||
"""A registry of long-living objects in qutebrowser.
|
"""A registry of long-living objects in qutebrowser.
|
||||||
|
|
||||||
@ -394,55 +395,20 @@ class ObjectRegistry:
|
|||||||
cache: DiskCache instance.
|
cache: DiskCache instance.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __setitem__(self, name, obj):
|
||||||
self._objects = {}
|
|
||||||
self._objects['global'] = {}
|
|
||||||
|
|
||||||
def get(self, name, scope='global'):
|
|
||||||
"""Get an object from the object registry.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
name: The name of the object to get.
|
|
||||||
scope: The scope the object is registered in.
|
|
||||||
|
|
||||||
Return:
|
|
||||||
The registered object.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
objects = self._objects[scope]
|
|
||||||
except KeyError:
|
|
||||||
raise KeyError("Invalid scope '{}'.".format(scope))
|
|
||||||
try:
|
|
||||||
obj = objects[name]
|
|
||||||
except KeyError:
|
|
||||||
raise KeyError("No object '{}' in scope '{}'!".format(obj, scope))
|
|
||||||
return obj
|
|
||||||
|
|
||||||
def register(self, name, obj, scope='global'):
|
|
||||||
"""Register an object in the object registry.
|
"""Register an object in the object registry.
|
||||||
|
|
||||||
Args:
|
Prevents duplicated registrations.
|
||||||
name: The name to register the object as.
|
|
||||||
obj: The object to register.
|
|
||||||
scope: The scope to register the object in.
|
|
||||||
"""
|
"""
|
||||||
if scope not in self._objects:
|
if name in self.data:
|
||||||
raise KeyError("Invalid scope '{}'.".format(scope))
|
|
||||||
if name in self._objects[scope]:
|
|
||||||
existing_obj = self._objects[scope][name]
|
|
||||||
raise KeyError("Object '{}' is already registered in scope "
|
raise KeyError("Object '{}' is already registered in scope "
|
||||||
"'{}' ({})!".format(name, scope, existing_obj))
|
"'{}' ({})!".format(name, scope, self.data[name]))
|
||||||
self._objects[scope][name] = obj
|
super().__setitem__(name, obj)
|
||||||
|
|
||||||
def dump_objects(self):
|
def dump_objects(self):
|
||||||
"""Dump all objects as a string."""
|
"""Dump all objects as a string."""
|
||||||
lines = []
|
lines = []
|
||||||
count = 0
|
for name, obj in self.data.items():
|
||||||
for scope, objects in self._objects.items():
|
lines.append(" {}: {}".format(name, repr(obj)))
|
||||||
if objects:
|
lines.insert(0, '{} objects:'.format(len(self.data)))
|
||||||
lines.append("Scope {}:".format(scope))
|
|
||||||
for name, obj in objects.items():
|
|
||||||
lines.append(" {}: {}".format(name, repr(obj)))
|
|
||||||
count += 1
|
|
||||||
lines.insert(0, '{} qutebrowser objects:'.format(count))
|
|
||||||
return lines
|
return lines
|
||||||
|
@ -580,11 +580,11 @@ def is_enum(obj):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_object(name, scope='global'):
|
def get_object(name):
|
||||||
"""Helper function to get an object."""
|
"""Helper function to get an object."""
|
||||||
return QCoreApplication.instance().obj.get(name, scope)
|
return QCoreApplication.instance().obj[name]
|
||||||
|
|
||||||
|
|
||||||
def register_object(name, obj, scope='global'):
|
def register_object(name, obj):
|
||||||
"""Helper function to register an object."""
|
"""Helper function to register an object."""
|
||||||
return QCoreApplication.instance().obj.register(name, obj, scope)
|
QCoreApplication.instance().obj[name] = obj
|
||||||
|
Loading…
Reference in New Issue
Block a user