-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
108 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# https://click.palletsprojects.com/en/8.1.x/complex/#defining-the-lazy-group | ||
import importlib | ||
import click | ||
|
||
|
||
class AliasedLazyGroup(click.Group): | ||
def __init__(self, *args, lazy_subcommands=None, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
# lazy_subcommands is a map of the form: | ||
# | ||
# {command-name} -> {module-name}.{command-object-name} | ||
# | ||
self.lazy_subcommands = lazy_subcommands or {} | ||
|
||
def list_commands(self, ctx): | ||
base = super().list_commands(ctx) | ||
lazy = sorted(self.lazy_subcommands.keys()) | ||
return base + lazy | ||
|
||
def _lazy_load(self, cmd_name): | ||
# lazily loading a command, first get the module name and attribute name | ||
import_path = self.lazy_subcommands[cmd_name] | ||
modname, cmd_object_name = import_path.rsplit(".", 1) | ||
# do the import | ||
mod = importlib.import_module(modname) | ||
# get the Command object from that module | ||
cmd_object = getattr(mod, cmd_object_name) | ||
# check the result to make debugging easier | ||
if not isinstance(cmd_object, click.Command): | ||
raise ValueError( | ||
f"Lazy loading of {import_path} failed by returning " | ||
"a non-command object" | ||
) | ||
return cmd_object | ||
|
||
def get_command(self, ctx, cmd_name): | ||
rv = click.Group.get_command(self, ctx, cmd_name) | ||
if rv is not None: | ||
return rv | ||
matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name)] | ||
if not matches: | ||
return None | ||
elif len(matches) != 1: | ||
ctx.fail(f"Too many matches: {', '.join(sorted(matches))}") | ||
return self._lazy_load(matches[0]) | ||
|
||
def resolve_command(self, ctx, args): | ||
# always return the full command name | ||
_, cmd, args = super().resolve_command(ctx, args) | ||
assert cmd | ||
return cmd.name, cmd, args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.