Skip to content

Commit

Permalink
perf: make ape --help faster (#2011)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Apr 16, 2024
1 parent 9dea7d9 commit 4776fb1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/ape/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ def format_commands(self, ctx, formatter) -> None:
"Plugin": [],
"3rd-Party Plugin": [],
}
metadata = PluginMetadataList.load(ManagerAccessMixin.plugin_manager)

pl_metadata = PluginMetadataList.load(
ManagerAccessMixin.plugin_manager, include_available=False
)

for cli_name, cmd in commands:
help = cmd.get_short_help_str(limit)
plugin = metadata.get_plugin(cli_name)
plugin = pl_metadata.get_plugin(cli_name)
if not plugin:
continue

Expand Down
26 changes: 18 additions & 8 deletions src/ape/plugins/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,17 @@ class PluginMetadataList(BaseModel):
third_party: "PluginGroup"

@classmethod
def load(cls, plugin_manager):
registered_plugins = plugin_manager.registered_plugins
available_plugins = github_client.available_plugins
return cls.from_package_names(registered_plugins.union(available_plugins))
def load(cls, plugin_manager, include_available: bool = True):
plugins = plugin_manager.registered_plugins
if include_available:
plugins = plugins.union(github_client.available_plugins)

return cls.from_package_names(plugins, include_available=include_available)

@classmethod
def from_package_names(cls, packages: Iterable[str]) -> "PluginMetadataList":
def from_package_names(
cls, packages: Iterable[str], include_available: bool = True
) -> "PluginMetadataList":
PluginMetadataList.model_rebuild()
core = PluginGroup(plugin_type=PluginType.CORE)
available = PluginGroup(plugin_type=PluginType.AVAILABLE)
Expand All @@ -140,11 +144,17 @@ def from_package_names(cls, packages: Iterable[str]) -> "PluginMetadataList":
plugin = PluginMetadata(name=name.strip(), version=version)
if plugin.in_core:
core.plugins[name] = plugin
elif plugin.is_available and not plugin.is_installed:
continue

# perf: only check these once.
is_installed = plugin.is_installed
is_available = include_available and plugin.is_available

if include_available and is_available and not is_installed:
available.plugins[name] = plugin
elif plugin.is_installed and not plugin.in_core and not plugin.is_available:
elif is_installed and not plugin.in_core and not is_available:
third_party.plugins[name] = plugin
elif plugin.is_installed:
elif is_installed:
installed.plugins[name] = plugin
else:
logger.error(f"'{plugin.name}' is not a plugin.")
Expand Down

0 comments on commit 4776fb1

Please sign in to comment.