Skip to content

Commit

Permalink
Merge pull request #2744 from kecnry/downstream-registry-overwrite
Browse files Browse the repository at this point in the history
allow overwriting existing registry entries
  • Loading branch information
pllim authored Mar 8, 2024
2 parents 324c96f + f9c348e commit 43487aa
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions jdaviz/core/registries.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class UniqueDictRegistry(DictRegistry):
"""Base registry class that handles hashmap-like associations between a string
representation of a plugin and the class to be instantiated.
"""
def add(self, name, cls):
def add(self, name, cls, overwrite=False):
"""Add an item to the registry.
Parameters
Expand All @@ -46,23 +46,25 @@ def add(self, name, cls):
cls : type
The class definition (not instance) associated with the name given
in the first parameter.
overwrite : bool, optional
Whether to overwrite an existing entry with the same ``label``.
"""
if name in self.members:
if name in self.members and not overwrite:
raise ValueError(f"Viewer with the name {name} already exists, "
f"please choose a different name.")
f"please choose a different name or pass overwrite=True.")
else:
self.members[name] = cls


class ViewerRegistry(UniqueDictRegistry):
"""Registry containing references to custom viewers."""
def __call__(self, name=None, label=None):
def __call__(self, name=None, label=None, overwrite=False):
def decorator(cls):
self.add(name, cls, label)
self.add(name, cls, label, overwrite=overwrite)
return cls
return decorator

def add(self, name, cls, label=None):
def add(self, name, cls, label=None, overwrite=False):
"""Add an item to the registry.
Parameters
Expand All @@ -75,10 +77,12 @@ def add(self, name, cls, label=None):
in the first parameter.
label : str, optional
The label displayed in the tooltip when hovering over the tray tab.
overwrite : bool, optional
Whether to overwrite an existing entry with the same ``label``.
"""
if name in self.members:
if name in self.members and not overwrite:
raise ValueError(f"Viewer with the name {name} already exists, "
f"please choose a different name.")
f"please choose a different name or pass overwrite=True.")
else:
self.members[name] = {'label': label, 'cls': cls}

Expand All @@ -100,7 +104,7 @@ class TrayRegistry(UniqueDictRegistry):
}

def __call__(self, name=None, label=None, icon=None,
viewer_requirements=[]):
viewer_requirements=[], overwrite=False):
def decorator(cls):
# The class must inherit from `VuetifyTemplate` in order to be
# ingestible by the component initialization.
Expand All @@ -110,12 +114,12 @@ def decorator(cls):
f"registered components must inherit from "
f"`ipyvuetify.VuetifyTemplate`.")

self.add(name, cls, label, icon, viewer_requirements)
self.add(name, cls, label, icon, viewer_requirements, overwrite)
return cls
return decorator

def add(self, name, cls, label=None, icon=None,
viewer_requirements=[]):
viewer_requirements=[], overwrite=False):
"""Add an item to the registry.
Parameters
Expand All @@ -132,10 +136,12 @@ def add(self, name, cls, label=None, icon=None,
The name of the icon to render in the tray tab.
viewer_requirements : str, list of str
Required viewers for this plugin.
overwrite : bool, optional
Whether to overwrite an existing entry with the same ``label``.
"""
if name in self.members:
if name in self.members and not overwrite:
raise ValueError(f"Viewer with the name {name} already exists, "
f"please choose a different name.")
f"please choose a different name or pass overwrite=True.")
else:
# store the registry name/label so we can access them from the instantiated
# objects (when determining if a specific plugin is open, for example)
Expand Down Expand Up @@ -170,7 +176,7 @@ class ToolRegistry(UniqueDictRegistry):
"""Registry containing references to plugins which will populate the
application-level toolbar.
"""
def __call__(self, name=None):
def __call__(self, name=None, overwrite=False):
def decorator(cls):
# The class must inherit from `Widget` in order to be
# ingestible by the component initialization.
Expand All @@ -180,7 +186,7 @@ def decorator(cls):
f"registered tools must inherit from "
f"`ipywidgets.Widget`.")

self.add(name, cls)
self.add(name, cls, overwrite)
return cls
return decorator

Expand All @@ -189,7 +195,7 @@ class MenuRegistry(UniqueDictRegistry):
"""Registry containing references to plugins that will populate the
application-level menu bar.
"""
def __call__(self, name=None):
def __call__(self, name=None, overwrite=False):
def decorator(cls):
# The class must inherit from `VuetifyTemplate` in order to be
# ingestible by the component initialization.
Expand All @@ -199,7 +205,7 @@ def decorator(cls):
f"registered tools must inherit from "
f"`ipywidgets.Widget`.")

self.add(name, cls)
self.add(name, cls, overwrite)
return cls
return decorator

Expand Down

0 comments on commit 43487aa

Please sign in to comment.