Minor fixes and update documentation. Preparing the upcoming 0.2 release.
This commit is contained in:
@ -1,22 +1,25 @@
|
||||
import argparse
|
||||
|
||||
import logging
|
||||
from stevedore import ExtensionManager
|
||||
|
||||
|
||||
def entrypoint():
|
||||
def entrypoint(args=None):
|
||||
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)
|
||||
commands = {}
|
||||
def register_extension(ext, commands=commands):
|
||||
try:
|
||||
parser = subparsers.add_parser(ext.name)
|
||||
commands[ext.name] = ext.plugin(parser)
|
||||
except Exception:
|
||||
logging.exception('Error while loading command {}.'.format(ext.name))
|
||||
|
||||
mgr = ExtensionManager(namespace='bonobo.commands', )
|
||||
mgr.map(register_extension)
|
||||
|
||||
args = parser.parse_args().__dict__
|
||||
command = args.pop('command')
|
||||
command(**args)
|
||||
args = parser.parse_args(args).__dict__
|
||||
commands[args.pop('command')](**args)
|
||||
|
||||
@ -1,13 +1,21 @@
|
||||
import argparse
|
||||
|
||||
from bonobo import Graph, console_run
|
||||
from bonobo import Graph, run
|
||||
|
||||
|
||||
def execute(file):
|
||||
def execute(file, quiet=False):
|
||||
with file:
|
||||
code = compile(file.read(), file.name, 'exec')
|
||||
|
||||
context = {}
|
||||
# TODO: A few special variables should be set before running the file:
|
||||
#
|
||||
# See:
|
||||
# - https://docs.python.org/3/reference/import.html#import-mod-attrs
|
||||
# - https://docs.python.org/3/library/runpy.html#runpy.run_module
|
||||
context = {
|
||||
'__name__': '__bonobo__',
|
||||
'__file__': file.name,
|
||||
}
|
||||
|
||||
try:
|
||||
exec(code, context)
|
||||
@ -16,14 +24,19 @@ def execute(file):
|
||||
|
||||
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. '
|
||||
assert len(graphs) == 1, ('Having zero or more than one graph definition in one file is unsupported for now, '
|
||||
'but it is something that will be implemented in the future.\n\nExpected: 1, got: {}.').format(
|
||||
len(graphs))
|
||||
|
||||
name, graph = list(graphs.items())[0]
|
||||
|
||||
return console_run(graph)
|
||||
# todo if console and not quiet, then add the console plugin
|
||||
# todo when better console plugin, add it if console and just disable display
|
||||
|
||||
return run(graph)
|
||||
|
||||
|
||||
def register(parser):
|
||||
parser.add_argument('file', type=argparse.FileType())
|
||||
parser.add_argument('--quiet', action='store_true')
|
||||
return execute
|
||||
|
||||
Reference in New Issue
Block a user