from zope.interface import Interface, implements, Attribute
from twisted.python import components
from twisted.internet import protocol
from twisted.application import service

class IMyService(Interface):

    def doSomething():
        pass

class MyService(service.Service):
    implements(IMyService)

    def doSomething(self):
        print "I'll do something"

class IConfigurationObject(Interface):
    username = Attribute("username")

class ConfigurationObject(object):
    implements(IConfigurationObject)

    def __init__(self, username):
        self.username = username

class IConfiguredService(Interface):
    service = Attribute("my service")
    configuration = Attribute("configuration")

class ConfiguredService(object):
    implements(IConfiguredService)

    def __init__(self, service, configuration):
        self.service = IMyService(service)
        self.configuration = IConfigurationObject(configuration)

class IAMQPFactory(Interface):

    def blah():
        pass

class AdaptToFactoryFromService(protocol.ClientFactory):
    implements(IAMQPFactory)

    # this needs a protocol, I know

    def __init__(self, configuredService):
        self.configuredService = configuredService

    def blah(self):
        # this will be called by protocol instances
        username = self.configuredService.configuration.username
        service = self.configuredService.service

        print "I'm %s" % username
        service.doSomething()

components.registerAdapter(AdaptToFactoryFromService,
    IConfiguredService, IAMQPFactory)

s = MyService()
c = ConfigurationObject("foo")
cs = ConfiguredService(s, c)

f = IAMQPFactory(cs)
f.blah()
