Created
March 2, 2022 15:45
-
-
Save schettino72/1a665303a866002a817dd87a140c3666 to your computer and use it in GitHub Desktop.
pydoit / click proof of concept integration
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
#! /usr/bin/env python3 | |
import sys | |
import inspect | |
from doit.cmd_base import ModuleTaskLoader | |
from doit.doit_cmd import DoitMain | |
import click | |
##################################### | |
# to be extracted into a doit_click.py. | |
def run_doit_task(task_name): | |
def callback(): | |
loader = ModuleTaskLoader(globals()) | |
doit_main = DoitMain(loader, extra_config={'GLOBAL': {'verbosity': 2}}) | |
sys.exit(doit_main.run([task_name])) | |
return callback | |
class ClickDoit(click.Group): | |
""" | |
subclass with an extra decorator used to create commands from doit task_creators (instead of plain function) | |
""" | |
def click_cmd(self, name=None, **attrs): | |
def decorator(creator): | |
task_name = creator.__name__[5:] # 5 is len('task_') | |
cmd_name = name if name else task_name | |
cmd_help = inspect.getdoc(creator) | |
cmd = click.Command(name=cmd_name, callback=run_doit_task(task_name), help=cmd_help) | |
self.add_command(cmd) | |
return creator # return original task_creator to be used by doit itself | |
return decorator | |
####################################### | |
# execute tasks through click based CLI | |
cli = ClickDoit() | |
@cli.command() | |
def initdb(): | |
"""init DB foo bar""" | |
click.echo('Iniitialize something without doit') | |
##################################### | |
# this project task defintions | |
@cli.click_cmd('my-echo') | |
def task_echo(): | |
"""echo some value""" | |
return { | |
'actions': ['echo xxxx'], | |
} | |
if __name__ == '__main__': | |
cli() | |
######### | |
## user interface with `./cli.py` | |
## full doit capabilities with `doit -f cli.py` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment