class A(Service): deps = Needs(['b1', 'x']) @provides def a1(self): """returns (b1 + x).""" return self.deps.b1() + self.deps.x()
class MyService(Service): # Should not raise deps = Needs(['stuff', 'fluff']) @provides def provider_a(self): return self.deps.stuff() + self._fluff() def _fluff(self): return self.deps.fluff()
class ServiceThatNeedsBandC(Service): deps = Needs(['b', 'c']) @provides def get_b(self): return self.deps.b() @provides def get_c(self): return self.deps.c()
class MyService(Service): # Should not raise deps = Needs(['stuff']) @provides def provider_a(self): return self.deps.stuff() + self._fluff() def _fluff(self): # intentional comments. do not remove. # self.deps.fluff() return 10 # + self.deps.gruff()
class B(Service): deps = Needs(['c1', 'x', 'y']) @provides def b1(self): """returns (c1 * 2).""" return self.deps.c1() * 2 @provides def b2(self): """return (x + y).""" return self.deps.x() + self.deps.y()
class Service(INeed, IProvide): __metaclass__ = ServiceMetaclass deps = Needs([]) # override me in subclass to define service needs def __init__(self): super(Service, self).__init__() self.deps = PortArray.replicate( self.__class__.deps ) # each service instance should have its own copy # ---- implement INeed ---- @classmethod def get_needs(cls): return cls.deps.get_ports() def _is_compatible_provider(self, port_name, provider): return True # no flag checking for now def _satisfy_need(self, port_name, func): self.deps.connect_port(port_name, func) # ---- implement IProvide ---- @classmethod def get_provides(cls): return cls.meta.get_provides() def get_provider_func(self, port_name): meta = self.__class__.meta method_name = meta.get_provider_method_name(port_name) return getattr(self, method_name) @classmethod def get_provider_flag(cls, port_name, flag_name): return cls.meta.get_provider_flag(port_name, flag_name) @classmethod def get_provider_flags(cls, port_name): return cls.meta.get_provider_flags(port_name)
class B(Service): deps = Needs(['b']) @provides def bbb(self): return self.deps.b()
class MultiplierService(Service): deps = Needs(['a1', 'b2']) @provides def multiplied(self): return self.deps.a1() * self.deps.b2()
class ServiceThatNeedsAandB(Service): deps = Needs(['a', 'b']) @provides def get_ab(self): return self.deps.a(), self.deps.b()
class MyService(Service): deps = Needs(['9_bad_port_name'])
class MyService(Service): deps = Needs(['stuff', 'fluff']) @provides def provider_a(self): return self.deps.stuff()
class MyService(Service): deps = Needs(['stuff', 'more_stuff', 'stuff'])
class MyService(Service): deps = Needs(ports=['a', 'b']) @provides def x(self): return self.deps.a() + self.deps.b()
class MyService(Service): deps = Needs(['get_needs'])
class B(Service): deps = Needs(['b']) # no interface, should still match @provides def bbb(self): return self.deps.b()
class SimpleService(Service): deps = Needs('some_number') @provides def multiplied_number(self): return self.deps.some_number() * 100
class Dog(Service): deps = Needs(['number']) @provides def bark(self): return ('woof ' * self.deps.number()).strip()