Merge pull request #73 from hartym/feature/cli_run

bonobo run <path> / bonobo run -m (#72)
This commit is contained in:
Romain Dorgueil
2017-05-22 07:03:41 -07:00
committed by GitHub
4 changed files with 50 additions and 30 deletions

View File

@ -44,15 +44,20 @@ def run(graph, strategy=None, plugins=None, services=None):
plugins = plugins or [] plugins = plugins or []
if _is_interactive_console(): # pragma: no cover from bonobo import settings
from bonobo.ext.console import ConsoleOutputPlugin
if ConsoleOutputPlugin not in plugins:
plugins.append(ConsoleOutputPlugin)
if _is_jupyter_notebook(): # pragma: no cover settings.check()
from bonobo.ext.jupyter import JupyterOutputPlugin
if JupyterOutputPlugin not in plugins: if not settings.QUIET: # pragma: no cover
plugins.append(JupyterOutputPlugin) if _is_interactive_console():
from bonobo.ext.console import ConsoleOutputPlugin
if ConsoleOutputPlugin not in plugins:
plugins.append(ConsoleOutputPlugin)
if _is_jupyter_notebook():
from bonobo.ext.jupyter import JupyterOutputPlugin
if JupyterOutputPlugin not in plugins:
plugins.append(JupyterOutputPlugin)
return strategy.execute(graph, plugins=plugins, services=services) return strategy.execute(graph, plugins=plugins, services=services)

View File

@ -1,12 +1,14 @@
import argparse
import os import os
import runpy
import bonobo import bonobo
DEFAULT_SERVICES_FILENAME = '_services.py' DEFAULT_SERVICES_FILENAME = '_services.py'
DEFAULT_SERVICES_ATTR = 'get_services' DEFAULT_SERVICES_ATTR = 'get_services'
DEFAULT_GRAPH_FILENAME = '__main__.py'
DEFAULT_GRAPH_ATTR = 'get_graph'
def get_default_services(filename, services=None): def get_default_services(filename, services=None):
dirname = os.path.dirname(filename) dirname = os.path.dirname(filename)
@ -29,24 +31,24 @@ def get_default_services(filename, services=None):
return services or {} return services or {}
def execute(file, quiet=False): def execute(filename, module, quiet=False, verbose=False):
with file: from bonobo import settings
code = compile(file.read(), file.name, 'exec')
# TODO: A few special variables should be set before running the file: if quiet:
# settings.QUIET = True
# 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: if verbose:
exec(code, context) settings.DEBUG = True
except Exception as exc:
raise if filename:
if os.path.isdir(filename):
filename = os.path.join(filename, DEFAULT_GRAPH_FILENAME)
context = runpy.run_path(filename, run_name='__bonobo__')
elif module:
context = runpy.run_module(module, run_name='__bonobo__')
filename = context['__file__']
else:
raise RuntimeError('UNEXPECTED: argparse should not allow this.')
graphs = dict((k, v) for k, v in context.items() if isinstance(v, bonobo.Graph)) graphs = dict((k, v) for k, v in context.items() if isinstance(v, bonobo.Graph))
@ -63,12 +65,16 @@ def execute(file, quiet=False):
graph, graph,
plugins=[], plugins=[],
services=get_default_services( services=get_default_services(
file.name, context.get(DEFAULT_SERVICES_ATTR)() if DEFAULT_SERVICES_ATTR in context else None filename, context.get(DEFAULT_SERVICES_ATTR)() if DEFAULT_SERVICES_ATTR in context else None
) )
) )
def register(parser): def register(parser):
parser.add_argument('file', type=argparse.FileType()) source_group = parser.add_mutually_exclusive_group(required=True)
parser.add_argument('--quiet', action='store_true') source_group.add_argument('filename', nargs='?', type=str)
source_group.add_argument('--module', '-m', type=str)
verbosity_group = parser.add_mutually_exclusive_group()
verbosity_group.add_argument('--quiet', '-q', action='store_true')
verbosity_group.add_argument('--verbose', '-v', action='store_true')
return execute return execute

View File

@ -37,6 +37,7 @@ class ContextProcessor(Option):
... yield counter.get() ... yield counter.get()
""" """
@property @property
def __name__(self): def __name__(self):
return self.func.__name__ return self.func.__name__

View File

@ -9,8 +9,16 @@ def to_bool(s):
return False return False
# Debug mode. # Debug/verbose mode.
DEBUG = to_bool(os.environ.get('BONOBO_DEBUG', 'f')) DEBUG = to_bool(os.environ.get('BONOBO_DEBUG', 'f'))
# Profile mode. # Profile mode.
PROFILE = to_bool(os.environ.get('BONOBO_PROFILE', 'f')) PROFILE = to_bool(os.environ.get('BONOBO_PROFILE', 'f'))
# Quiet mode.
QUIET = to_bool(os.environ.get('BONOBO_QUIET', 'f'))
def check():
if DEBUG and QUIET:
raise RuntimeError('I cannot be verbose and quiet at the same time.')