[config] Refactoring of configurables, allowing partially configured objects.

Configurables did not allow more than one "method" option, and mixed
scenarios (options+methods+...) were sometimes flaky, forcing the user
to know what order was the right one. Now, all options work the same,
sharing the same "order" namespace.

Backward incompatible change: Options are now required by default,
unless a default is provided.

Also adds a few candies for debugging/testing, found in the
bonobo.util.inspect module.
This commit is contained in:
Romain Dorgueil
2017-07-05 11:15:03 +02:00
parent 67b4227436
commit 5062221e78
19 changed files with 573 additions and 120 deletions

View File

@ -1,7 +1,5 @@
import pytest
from bonobo.config import Configurable, Method, Option
from bonobo.errors import ConfigurationError
from bonobo.util.inspect import inspect_node
class MethodBasedConfigurable(Configurable):
@ -13,22 +11,56 @@ class MethodBasedConfigurable(Configurable):
self.handler(*args, **kwargs)
def test_one_wrapper_only():
with pytest.raises(ConfigurationError):
def test_multiple_wrapper_suppored():
class TwoMethods(Configurable):
h1 = Method(required=True)
h2 = Method(required=True)
class TwoMethods(Configurable):
h1 = Method()
h2 = Method()
with inspect_node(TwoMethods) as ci:
assert ci.type == TwoMethods
assert not ci.instance
assert len(ci.options) == 2
assert not len(ci.processors)
assert not ci.partial
@TwoMethods
def OneMethod():
pass
with inspect_node(OneMethod) as ci:
assert ci.type == TwoMethods
assert not ci.instance
assert len(ci.options) == 2
assert not len(ci.processors)
assert ci.partial
@OneMethod
def transformation():
pass
with inspect_node(transformation) as ci:
assert ci.type == TwoMethods
assert ci.instance
assert len(ci.options) == 2
assert not len(ci.processors)
assert not ci.partial
def test_define_with_decorator():
calls = []
@MethodBasedConfigurable
def Concrete(self, *args, **kwargs):
calls.append((args, kwargs, ))
def my_handler(*args, **kwargs):
calls.append((args, kwargs,))
Concrete = MethodBasedConfigurable(my_handler)
assert callable(Concrete.handler)
assert Concrete.handler == my_handler
with inspect_node(Concrete) as ci:
assert ci.type == MethodBasedConfigurable
assert ci.partial
t = Concrete('foo', bar='baz')
assert callable(t.handler)
@ -37,13 +69,29 @@ def test_define_with_decorator():
assert len(calls) == 1
def test_late_binding_method_decoration():
calls = []
@MethodBasedConfigurable(foo='foo')
def Concrete(*args, **kwargs):
calls.append((args, kwargs,))
assert callable(Concrete.handler)
t = Concrete(bar='baz')
assert callable(t.handler)
assert len(calls) == 0
t()
assert len(calls) == 1
def test_define_with_argument():
calls = []
def concrete_handler(*args, **kwargs):
calls.append((args, kwargs, ))
calls.append((args, kwargs,))
t = MethodBasedConfigurable('foo', bar='baz', handler=concrete_handler)
t = MethodBasedConfigurable(concrete_handler, 'foo', bar='baz')
assert callable(t.handler)
assert len(calls) == 0
t()
@ -55,7 +103,7 @@ def test_define_with_inheritance():
class Inheriting(MethodBasedConfigurable):
def handler(self, *args, **kwargs):
calls.append((args, kwargs, ))
calls.append((args, kwargs,))
t = Inheriting('foo', bar='baz')
assert callable(t.handler)
@ -71,8 +119,8 @@ def test_inheritance_then_decorate():
pass
@Inheriting
def Concrete(self, *args, **kwargs):
calls.append((args, kwargs, ))
def Concrete(*args, **kwargs):
calls.append((args, kwargs,))
assert callable(Concrete.handler)
t = Concrete('foo', bar='baz')