Skip to content

Commit

Permalink
Make a single pass through menus to build command->menu map
Browse files Browse the repository at this point in the history
  • Loading branch information
jni committed Jun 21, 2024
1 parent 139a5c1 commit a7f2e29
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/npe2/_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,24 +366,18 @@ def register(
self.events.plugins_registered.emit({manifest})

def _populate_command_menu_map(self, manifest: PluginManifest):
self._command_menu_map[manifest.name] = defaultdict(dict)
for command in manifest.contributions.commands or ():
# map of manifest -> command -> menu_id -> list[items]
self._command_menu_map[manifest.name] = defaultdict(
lambda: defaultdict(list)
)
menu_map = self._command_menu_map[manifest.name] # just for conciseness below
for menu_id, menu_items in manifest.contributions.menus.items() or ():
# command IDs are keys in map
# each value is a dict menu_id: list of MenuCommands
# for the command and menu
associated = self._get_associated_menus(manifest, command.id)
self._command_menu_map[manifest.name][command.id] = associated

def _get_associated_menus(
self, manifest: PluginManifest, command_id: str
) -> Dict[str, List[MenuCommand]]:
menus = manifest.contributions.menus or dict()
associated_menus = defaultdict(list)
for menu_id, items in menus.items():
for item in items:
if getattr(item, "command", "") == command_id:
associated_menus[menu_id].append(item)
return associated_menus
for item in menu_items:
if (command_id := getattr(item, "command", None)) is not None:
menu_map[command_id][menu_id].append(item)

def unregister(self, key: PluginName):
"""Unregister plugin named `key`."""
Expand Down

0 comments on commit a7f2e29

Please sign in to comment.