Implements commands as extensions using stevedore; adds run and init command.

This commit is contained in:
Romain Dorgueil
2017-01-10 23:09:40 +01:00
parent f736e7e7e3
commit 9daefd7207
9 changed files with 115 additions and 17 deletions

View File

@ -0,0 +1,22 @@
import argparse
from stevedore import ExtensionManager
def entrypoint():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='command')
subparsers.required = True
def register_extension(ext):
parser = subparsers.add_parser(ext.name)
command = ext.plugin(parser)
parser.set_defaults(command=command)
mgr = ExtensionManager(namespace='bonobo.commands', )
mgr.map(register_extension)
args = parser.parse_args().__dict__
command = args.pop('command')
command(**args)

18
bonobo/commands/init.py Normal file
View File

@ -0,0 +1,18 @@
import os
def execute():
try:
import edgy.project
except ImportError as exc:
raise ImportError(
'You must install "edgy.project" to use this command.\n\n $ pip install edgy.project\n'
) from exc
from edgy.project.__main__ import handle_init
return handle_init(os.path.join(os.getcwd(), 'Projectfile'))
def register(parser):
return execute

29
bonobo/commands/run.py Normal file
View File

@ -0,0 +1,29 @@
import argparse
from bonobo import Graph, console_run
def execute(file):
with file:
code = compile(file.read(), file.name, 'exec')
context = {}
try:
exec(code, context)
except Exception as exc:
raise
graphs = dict((k, v) for k, v in context.items() if isinstance(v, Graph))
assert len(graphs) == 1, 'Having more than one graph definition in one file is unsupported for now, but it is ' \
'something that will be implemented in the future. '
name, graph = list(graphs.items())[0]
return console_run(graph)
def register(parser):
parser.add_argument('file', type=argparse.FileType())
return execute