Esempio n. 1
0
def test():
    assert isinstance(Foo.impl00, AutowireField)
    assert isinstance(Foo.impl01, AutowireField)
    assert isinstance(Foo.impl02, AutowireField)
    assert isinstance(Foo.impl03, AutowireField)

    registry = ServiceRegistry()
    enable_autowire(registry)

    impl00 = object()
    impl01 = object()
    impl02 = object()
    impl03 = object()

    registry.register_singleton(impl00, Iface00)
    registry.register_singleton(impl01, name="impl01")
    registry.register_singleton(impl02, name="r_impl02")
    registry.register_singleton(impl03, context=Ctxt)
    registry.register_autowire(
        Foo,
        IFoo,
        namespace=dict(n_impl02="r_impl02", n_impl03=Ctxt()),
        lazy=False,
    )

    container = registry.create_container()
    foo = container.get(IFoo)
    assert foo.impl00 is impl00
    assert foo.impl01 is impl01
    assert foo.impl02 is impl02
    assert foo.impl03 is impl03
Esempio n. 2
0
def assert_lazy(cls_args, cls_kwargs):
    assert isinstance(Foo.impl00, AutowireField)
    assert isinstance(Foo.impl01, AutowireField)
    assert isinstance(Foo.impl02, AutowireField)
    assert isinstance(Foo.impl03, AutowireField)
    assert isinstance(Foo.impl04, AutowireField)
    assert isinstance(Foo.impl05, AutowireField)
    assert isinstance(Foo.impl06, AutowireField)

    registry = ServiceRegistry()
    enable_autowire(registry)

    impl00 = object()
    impl01 = object()
    impl02 = object()
    impl03 = object()
    impl04 = object()
    impl05 = object()
    impl06 = object()

    registry.register_singleton(impl00, Iface00)
    registry.register_singleton(impl01, name="impl01")
    registry.register_singleton(impl02, name="r_impl02")
    registry.register_singleton(impl03, context=Ctxt)
    registry.register_singleton(impl04, name="pv_impl04")
    registry.register_singleton(impl05, context=Ctxt2)
    registry.register_singleton(impl06, context=IFoo)
    registry.register_autowire(
        Foo,
        IFoo,
        namespace=dict(n_impl02="r_impl02", n_impl03=Ctxt()),
        cls_args=cls_args,
        cls_kwargs=cls_kwargs,
        lazy=True,
    )

    container = registry.create_container()
    foo = container.get(IFoo)
    assert foo.impl00 is impl00
    assert foo.impl01 is impl01
    assert foo.impl02 is impl02
    assert foo.impl03 is impl03
    assert foo.impl04 is impl04
    assert foo.impl05 is impl05
    assert foo.impl06 is impl06
Esempio n. 3
0
def enable_autowire(registry: ServiceRegistry) -> None:
    registry.register_autowire = MethodType(register_autowire, registry)