Slight changes in CLI structure for performance reasons

Having the plugin commands on the first level of --help proved to be
a bad idea since it basically kills every chance of lazy loading the (expensive)
plugin context. Using a sub command for anything plugin related allows us
to only fire up the plugin context if a plugin command is expected, saving us
some precious seconds of operation in all other cases.

For conformity reasons the dev sub commands were now restructured
similarly.
This commit is contained in:
Gina Häußge committed 2015-10-30 16:28:34 +01:00
1 parent 221b497932
commit 8ed3bcb094
3 files changed
+25 -29

No files matched your search

+12 -13
View File
@@ -15,8 +15,8 @@ class OctoPrintDevelCommands(click.MultiCommand):
based on availability of development dependencies.
"""
prefix = "dev"
sep = ":"
groups = ("plugin",)
def __init__(self, *args, **kwargs):
click.MultiCommand.__init__(self, *args, **kwargs)
@@ -47,10 +47,11 @@ class OctoPrintDevelCommands(click.MultiCommand):
yield result
def _get_commands(self):
commands = dict()
for command in self._get_commands_from_prefix_methods("group_"):
commands[self.prefix + self.sep + command.name] = command
return commands
result = dict()
for group in self.groups:
for command in self._get_commands_from_prefix_methods("{}_".format(group)):
result[group + self.sep + command.name] = command
return result
def list_commands(self, ctx):
result = [name for name in self._get_commands()]
@@ -61,13 +62,6 @@ class OctoPrintDevelCommands(click.MultiCommand):
commands = self._get_commands()
return commands.get(cmd_name, None)
def group_plugin(self):
command_group = click.Group("plugin",
help="Helpers for plugin developers")
for command in self._get_commands_from_prefix_methods("plugin_"):
command_group.add_command(command)
return command_group
def plugin_new(self):
try:
import cookiecutter.main
@@ -229,6 +223,11 @@ class OctoPrintDevelCommands(click.MultiCommand):
return command
@click.group(cls=OctoPrintDevelCommands)
@click.group()
def dev_commands():
pass
@dev_commands.group(name="dev", cls=OctoPrintDevelCommands)
def dev():
"""Additional commands for development tasks."""
pass
+6 -9
View File
@@ -27,7 +27,6 @@ class OctoPrintPluginCommands(click.MultiCommand):
The :class:`~octoprint.plugin.core.PluginManager` instance.
"""
prefix = "plugin"
sep = ":"
def __init__(self, *args, **kwargs):
@@ -77,26 +76,24 @@ class OctoPrintPluginCommands(click.MultiCommand):
for name, hook in self.hooks.items():
try:
plugin_info = self.plugin_manager.get_plugin_info(name, require_enabled=False)
command_group = click.Group(name=name, help="{} commands".format(plugin_info.name))
commands = hook(self, pass_octoprint_ctx)
for command in commands:
if not isinstance(command, click.Command):
self._logger.warn("Plugin {} provided invalid CLI command, ignoring it: {!r}".format(name, command))
continue
command_group.add_command(command)
result[self.prefix + self.sep + name] = command_group
result[name + self.sep + command.name] = command
except:
self._logger.exception("Error while retrieving cli commants for plugin {}".format(name))
return result
@click.group(cls=OctoPrintPluginCommands)
@click.group()
@pass_octoprint_ctx
def plugin_commands(obj):
"""Commands provided by plugins."""
logging.basicConfig(level=logging.DEBUG if obj.verbosity > 0 else logging.WARN)
@plugin_commands.group(name="plugins", cls=OctoPrintPluginCommands)
def plugins():
"""Additional commands provided by plugins."""
pass