在倭黑猩猩中使用@use 装饰器

Using the @use decorator in bonobo

在 bonobo 文档中,他们有以下配置服务依赖性的示例:

from bonobo.config import use
@use('orders_database')
def select_all(database):
yield from database.query('SELECT * FROM foo;')

我尝试做类似的事情,但出现错误。这是我的脚本的一个非常简化的版本:

import bonobo
from bonobo.config import use
from ftplib import FTP

def get_services(**options):
    ftp_1 =  FTP('ftp.gnu.org')
    ftp_1.login()
    ftp_1.cwd('gnu/emacs')
    return{
        'ftp1': ftp_1,
    }

@use('ftp1')
def listen_for_file(ftp):
    test = ftp.nlst('README.olderversions')
    if test:
        print( 'Found file')
        return True
    else:
        print('File not found in ftp')
        return False

def get_graph(**options):
    graph = bonobo.Graph()
    graph.add_chain(listen_for_file)
    return graph

if __name__ == '__main__':
    parser = bonobo.get_argument_parser()
    with bonobo.parse_args(parser) as options:
        bonobo.run(get_graph(**options), services=get_services(**options))

如果我尝试 运行 这个,我会收到以下错误:

CRITICAL:bonobo.execution.contexts.base:<NodeExecutionContext(+listen_for_file) in=1 err=1> Traceback (most recent call last): File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\site-packages\bonobo\config\processors.py", line 102, in call bound = self._bind(_input) File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\site-packages\bonobo\config\processors.py", line 89, in _bind return bind(*self.args, *_input, **self.kwargs) File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\inspect.py", line 3015, in bind return args[0]._bind(args[1:], kwargs) File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\inspect.py", line 2930, in _bind raise TypeError(msg) from None TypeError: missing a required argument: 'ftp'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\site-packages\bonobo\execution\contexts\node.py", line 102, in loop self.step() File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\site-packages\bonobo\execution\contexts\node.py", line 129, in step results = self._stack(input_bag) File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\site-packages\bonobo\config\processors.py", line 114, in call ) from exc bonobo.errors.UnrecoverableTypeError: Input of <function listen_for_file at 0x000002A591047F78> does not bind to the node signature. Args: () Input: () Kwargs: {'ftp1': <ftplib.FTP object at 0x000002A5910CF708>} Signature: (ftp)

我可以将其设置为 运行 的唯一方法是在 listen_for_file 函数中将 ftp 更改为 ftp1。我在这里做错了什么?

bonobo 对变量命名很严格 - 尽管不应该如此。

要使用config.use,最好在函数签名中使用准确的服务名称,并将其作为最后一个参数。

要更改您要使用的实际服务提供商,您需要在 get_services 函数中更改地图。

import bonobo
from bonobo.config import use
from ftplib import FTP

def get_services(**options):
    ftp_1 =  FTP(options.get('ftp_server') or 'ftp.gnu.org')
    ftp_1.login()
    ftp_1.cwd('gnu/emacs')
    return{
        'ftp': ftp_1,
    }

@use('ftp')
def listen_for_file(ftp):
    test = ftp.nlst('README.olderversions')
    if test:
        print( 'Found file')
        return True
    else:
        print('File not found in ftp')
        return False

def get_graph(**options):
    graph = bonobo.Graph()
    graph.add_chain(listen_for_file)
    return graph

if __name__ == '__main__':
    parser = bonobo.get_argument_parser()
    parser.add('--ftp_server')
    with bonobo.parse_args(parser) as options:
        bonobo.run(get_graph(**options), services=get_services(**options))