Merge remote-tracking branch 'upstream/develop' into feature/io_pickle
This commit is contained in:
@ -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)
|
||||||
|
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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.')
|
||||||
Reference in New Issue
Block a user