-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first proof of concept for cli plugin
- Loading branch information
Showing
9 changed files
with
87 additions
and
12 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
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,44 @@ | ||
from importlib.metadata import entry_points | ||
from importlib import import_module | ||
from argparse import ArgumentParser, Namespace | ||
|
||
from typing import Callable, Set, Optional, Dict, Any, TypedDict | ||
|
||
|
||
# cli plugin is called once to populate the parser and possible a second time with the received args | ||
TCliPlugin = Callable[[Optional[ArgumentParser], Optional[Namespace]], None] | ||
|
||
|
||
class CliPlugin(TypedDict): | ||
func: TCliPlugin | ||
cli_help: str | ||
|
||
|
||
# registered plugins | ||
_DLT_PLUGINS: Set[str] = set() | ||
_DLT_CLI_PLUGINS: Dict[str, CliPlugin] = {} | ||
_CURRENT_PLUGIN: Optional[str] = None | ||
|
||
|
||
def cli_plugin(subcommand: str, cli_help: str) -> Callable[[TCliPlugin], None]: | ||
"""decorator for creating a dlt cli subcommand""" | ||
|
||
def register(cli_plugin: TCliPlugin) -> None: | ||
# print("Registering cli command") | ||
assert _CURRENT_PLUGIN | ||
assert ( | ||
subcommand not in _DLT_CLI_PLUGINS | ||
), f"CLI Plugin with name {subcommand} already exists" | ||
_DLT_CLI_PLUGINS[subcommand] = {"func": cli_plugin, "cli_help": cli_help} | ||
|
||
return register | ||
|
||
|
||
def discover_plugins() -> None: | ||
global _CURRENT_PLUGIN | ||
for entry_point in entry_points().select(group="dlt.plugin"): # type: ignore | ||
# print(f"Found dlt plugin {entry_point.name}") | ||
_CURRENT_PLUGIN = entry_point.name | ||
import_module(entry_point.value) | ||
_DLT_PLUGINS.add(entry_point.name) | ||
_CURRENT_PLUGIN = None |
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,5 +1,8 @@ | ||
|
||
install-example-plugin: | ||
pip uninstall example_plugin | ||
uninstall-example-plugin: | ||
pip uninstall example_plugin -y | ||
|
||
install-example-plugin: uninstall-example-plugin | ||
# this builds and installs the example plugin | ||
poetry build | ||
pip install dist/example_plugin-0.1.0-py3-none-any.whl |
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 +1,22 @@ | ||
print("HELLO") | ||
import dlt | ||
import dlt.plugins | ||
|
||
from argparse import ArgumentParser, Namespace | ||
|
||
|
||
@dlt.plugins.cli_plugin(subcommand="generate-stuff", cli_help="Let's generate some stuff") | ||
def register_dlt_plugin(parser: ArgumentParser = None, namespace: Namespace = None) -> None: | ||
# setup command | ||
if parser: | ||
parser.description = "HELLO" | ||
parser.add_argument( | ||
"--example-flag", | ||
action="store_true", | ||
help="This is how we add example flags.", | ||
default=False, | ||
) | ||
|
||
# execute stuff | ||
if namespace: | ||
print("Doing some stuff") | ||
print(namespace.example_flag) |
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,2 +1 @@ | ||
|
||
|