1
- from bonobo ._version import __version__
2
-
3
- __all__ = [
4
- '__version__' ,
5
- ]
1
+ import warnings
6
2
3
+ from bonobo .basics import Limit , PrettyPrint , Tee , count , identity , noop , pprint
4
+ from bonobo .strategies import create_strategy
7
5
from bonobo .structs import Bag , Graph
6
+ from bonobo .util .objects import get_name
7
+ from bonobo .io import CsvReader , CsvWriter , FileReader , FileWriter , JsonReader , JsonWriter
8
8
9
- __all__ + = ['Bag' , 'Graph' ]
9
+ __all__ = []
10
10
11
- # Filesystem. This is a shortcut from the excellent filesystem2 library, that we make available there for convenience.
12
- from fs import open_fs as _open_fs
13
11
14
- open_fs = lambda url , * args , ** kwargs : _open_fs (str (url ), * args , ** kwargs )
15
- __all__ += ['open_fs' ]
12
+ def register_api (x , __all__ = __all__ ):
13
+ __all__ .append (get_name (x ))
14
+ return x
16
15
17
- # Basic transformations.
18
- from bonobo .basics import *
19
- from bonobo .basics import __all__ as _all_basics
20
16
21
- __all__ += _all_basics
17
+ def register_api_group (* args ):
18
+ for attr in args :
19
+ register_api (attr )
22
20
23
- # Execution strategies.
24
- from bonobo .strategies import create_strategy
25
21
26
- __all__ += ['create_strategy' ]
22
+ @register_api
23
+ def run (graph , * chain , strategy = None , plugins = None , services = None ):
24
+ """
25
+ Main entry point of bonobo. It takes a graph and creates all the necessary plumbery around to execute it.
26
+
27
+ The only necessary argument is a :class:`Graph` instance, containing the logic you actually want to execute.
28
+
29
+ By default, this graph will be executed using the "threadpool" strategy: each graph node will be wrapped in a
30
+ thread, and executed in a loop until there is no more input to this node.
31
+
32
+ You can provide plugins factory objects in the plugins list, this function will add the necessary plugins for
33
+ interactive console execution and jupyter notebook execution if it detects correctly that it runs in this context.
34
+
35
+ You'll probably want to provide a services dictionary mapping service names to service instances.
36
+
37
+ :param Graph graph: The :class:`Graph` to execute.
38
+ :param str strategy: The :class:`bonobo.strategies.base.Strategy` to use.
39
+ :param list plugins: The list of plugins to enhance execution.
40
+ :param dict services: The implementations of services this graph will use.
41
+ :return bonobo.execution.graph.GraphExecutionContext:
42
+ """
43
+ if len (chain ):
44
+ warnings .warn ('DEPRECATED. You should pass a Graph instance instead of a chain.' )
45
+ from bonobo import Graph
46
+ graph = Graph (graph , * chain )
27
47
28
- # Extract and loads from stdlib.
29
- from bonobo .io import *
30
- from bonobo .io import __all__ as _all_io
48
+ strategy = create_strategy (strategy )
49
+ plugins = plugins or []
31
50
32
- __all__ += _all_io
51
+ if _is_interactive_console ():
52
+ from bonobo .ext .console import ConsoleOutputPlugin
53
+ if ConsoleOutputPlugin not in plugins :
54
+ plugins .append (ConsoleOutputPlugin )
33
55
56
+ if _is_jupyter_notebook ():
57
+ from bonobo .ext .jupyter import JupyterOutputPlugin
58
+ if JupyterOutputPlugin not in plugins :
59
+ plugins .append (JupyterOutputPlugin )
34
60
35
- # XXX This may be belonging to the bonobo.examples package.
36
- def get_examples_path (* pathsegments ):
37
- import os
38
- import pathlib
39
- return str (pathlib .Path (os .path .dirname (__file__ ), 'examples' , * pathsegments ))
61
+ return strategy .execute (graph , plugins = plugins , services = services )
40
62
41
63
42
- def open_examples_fs (* pathsegments ):
43
- return open_fs (get_examples_path (* pathsegments ))
64
+ # bonobo.structs
65
+ register_api_group (Bag , Graph )
66
+
67
+ # bonobo.strategies
68
+ register_api (create_strategy )
69
+
70
+
71
+ # Shortcut to filesystem2's open_fs, that we make available there for convenience.
72
+ @register_api
73
+ def open_fs (fs_url , * args , ** kwargs ):
74
+ """
75
+ Wraps :func:`fs.open_fs` function with a few candies.
76
+
77
+ :param str fs_url: A filesystem URL
78
+ :param parse_result: A parsed filesystem URL.
79
+ :type parse_result: :class:`ParseResult`
80
+ :param bool writeable: True if the filesystem must be writeable.
81
+ :param bool create: True if the filesystem should be created if it does not exist.
82
+ :param str cwd: The current working directory (generally only relevant for OS filesystems).
83
+ :param str default_protocol: The protocol to use if one is not supplied in the FS URL (defaults to ``"osfs"``).
84
+ :returns: :class:`~fs.base.FS` object
85
+ """
86
+ from fs import open_fs as _open_fs
87
+ return _open_fs (str (fs_url ), * args , ** kwargs )
44
88
45
- __all__ .append (get_examples_path .__name__ )
46
- __all__ .append (open_examples_fs .__name__ )
89
+
90
+ # bonobo.basics
91
+ register_api_group (
92
+ Limit ,
93
+ PrettyPrint ,
94
+ Tee ,
95
+ count ,
96
+ identity ,
97
+ noop ,
98
+ pprint ,
99
+ )
100
+
101
+ # bonobo.io
102
+ register_api_group (CsvReader , CsvWriter , FileReader , FileWriter , JsonReader , JsonWriter )
47
103
48
104
49
105
def _is_interactive_console ():
@@ -58,27 +114,13 @@ def _is_jupyter_notebook():
58
114
return False
59
115
60
116
61
- # @api
62
- def run (graph , * chain , strategy = None , plugins = None , services = None ):
63
- if len (chain ):
64
- warnings .warn ('DEPRECATED. You should pass a Graph instance instead of a chain.' )
65
- from bonobo import Graph
66
- graph = Graph (graph , * chain )
67
-
68
- strategy = create_strategy (strategy )
69
- plugins = []
70
-
71
- if _is_interactive_console ():
72
- from bonobo .ext .console import ConsoleOutputPlugin
73
- if ConsoleOutputPlugin not in plugins :
74
- plugins .append (ConsoleOutputPlugin )
75
-
76
- if _is_jupyter_notebook ():
77
- from bonobo .ext .jupyter import JupyterOutputPlugin
78
- if JupyterOutputPlugin not in plugins :
79
- plugins .append (JupyterOutputPlugin )
80
-
81
- return strategy .execute (graph , plugins = plugins , services = services )
117
+ @register_api
118
+ def get_examples_path (* pathsegments ):
119
+ import os
120
+ import pathlib
121
+ return str (pathlib .Path (os .path .dirname (__file__ ), 'examples' , * pathsegments ))
82
122
83
123
84
- __all__ .append (run .__name__ )
124
+ @register_api
125
+ def open_examples_fs (* pathsegments ):
126
+ return open_fs (get_examples_path (* pathsegments ))
0 commit comments