-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add lazy commands * Add Lazy group
- Loading branch information
Showing
13 changed files
with
162 additions
and
112 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
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,12 @@ | ||
import dotenv | ||
|
||
dotenv.load_dotenv() | ||
|
||
from fluid.scheduler.cli import TaskManagerCLI # isort:skip # noqa: E402 | ||
|
||
task_manager_cli = TaskManagerCLI( | ||
"examples.tasks:task_app", lazy_subcommands={"db": "examples.db.cli:cli"} | ||
) | ||
|
||
|
||
task_manager_cli() |
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,3 @@ | ||
from examples.db import get_db | ||
|
||
cli = get_db().cli() |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
"""initial | ||
Revision ID: 1f91ed2b2f26 | ||
Revises: | ||
Revises: | ||
Create Date: 2024-08-18 10:19:38.372777 | ||
""" | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""Reusable server side python modules""" | ||
|
||
__version__ = "1.2.2" | ||
__version__ = "1.2.3" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from importlib import import_module | ||
from typing import Any | ||
|
||
import click | ||
|
||
|
||
class LazyGroup(click.Group): | ||
def __init__( | ||
self, | ||
*, | ||
lazy_subcommands: dict[str, str] | None = None, | ||
**kwargs: Any, | ||
): | ||
super().__init__(**kwargs) | ||
self.lazy_subcommands = lazy_subcommands or {} | ||
|
||
def list_commands(self, ctx: click.Context) -> list[str]: | ||
commands = super().list_commands(ctx) | ||
commands.extend(self.lazy_subcommands) | ||
return sorted(commands) | ||
|
||
def get_command(self, ctx: click.Context, cmd_name: str) -> click.Command | None: | ||
if cmd_name in self.lazy_subcommands: | ||
return self._lazy_load(cmd_name) | ||
return super().get_command(ctx, cmd_name) | ||
|
||
def _lazy_load(self, cmd_name: str) -> click.Command: | ||
# 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 = 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 |
Oops, something went wrong.