Skip to content

Commit

Permalink
refactor: move override behavior into default select
Browse files Browse the repository at this point in the history
  • Loading branch information
nsprenkle committed Aug 8, 2024
1 parent 9d1bec8 commit 8334a0f
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions xblock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,9 @@ def __init__(self, all_entry_points):

def default_select(identifier, all_entry_points): # pylint: disable=inconsistent-return-statements
"""
Raise an exception when we have ambiguous entry points.
"""

if len(all_entry_points) == 0:
raise PluginMissingError(identifier)
if len(all_entry_points) == 1:
return all_entry_points[0]
elif len(all_entry_points) > 1:
raise AmbiguousPluginError(all_entry_points)
Select the appropriate entry point for a given block.
Honors the ability to override and take preference over the default entry point.

def select_with_overrides(identifier, all_entry_points): # pylint: disable=inconsistent-return-statements
"""
Return overridden entry point, if present. Otherwise returns entry point.
Raise an exception when we have ambiguous entry points.
"""

Expand All @@ -60,13 +49,13 @@ def select_with_overrides(identifier, all_entry_points): # pylint: disable=inco
# Overrides get priority
if len(overrides) == 1:
return overrides[0]
if len(overrides) > 1:
elif len(overrides) > 1:
raise AmbiguousPluginError(all_entry_points)

# ... then default behavior
if len(block_entry_points) == 0:
elif len(block_entry_points) == 0:
raise PluginMissingError(identifier)
if len(block_entry_points) == 1:
elif len(block_entry_points) == 1:
return all_entry_points[0]
elif len(block_entry_points) > 1:
raise AmbiguousPluginError(all_entry_points)
Expand Down Expand Up @@ -129,7 +118,7 @@ def select(identifier, all_entry_points):
if key not in PLUGIN_CACHE:

if select is None:
select = select_with_overrides
select = default_select

all_entry_points = [
*importlib.metadata.entry_points(group=f'{cls.entry_point}.overrides', name=identifier),
Expand Down

0 comments on commit 8334a0f

Please sign in to comment.