Make it possible to update an object in the object registry.

This commit is contained in:
Florian Bruhin 2014-09-24 06:25:08 +02:00
parent 908a69af18
commit a2d3ca6565
2 changed files with 16 additions and 8 deletions

View File

@ -395,12 +395,8 @@ class ObjectRegistry(collections.UserDict):
def __setitem__(self, name, obj): def __setitem__(self, name, obj):
"""Register an object in the object registry. """Register an object in the object registry.
Prevents duplicated registrations and sets a slot to remove QObjects Sets a slot to remove QObjects when they are destroyed.
when they are destroyed.
""" """
if name in self.data:
raise KeyError("Object '{}' is already registered ({})!".format(
name, repr(self.data[name])))
if isinstance(obj, QObject): if isinstance(obj, QObject):
obj.destroyed.connect(functools.partial(self.on_destroyed, name)) obj.destroyed.connect(functools.partial(self.on_destroyed, name))
super().__setitem__(name, obj) super().__setitem__(name, obj)

View File

@ -585,6 +585,18 @@ def get_object(name):
return QCoreApplication.instance().registry[name] return QCoreApplication.instance().registry[name]
def register_object(name, obj): def register_object(name, obj, update=False):
"""Helper function to register an object.""" """Helper function to register an object.
QCoreApplication.instance().registry[name] = obj
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.
"""
registry = QCoreApplication.instance().registry
if not update and name in registry:
raise KeyError("Object '{}' is already registered ({})!".format(
name, repr(registry[name])))
registry[name] = obj