from ZServer.datatypes import ServerFactory

class ServerFactory:
    def __init__(self, address=None):
        if address is None:
            self.host = None
            self.port = None
        else:
            self.host, self.port = address

    def prepare(self, defaulthost=None, dnsresolver=None,
                module=None, env=None, portbase=None):
        if defaulthost is not None:
            self._set_default_host(defaulthost)
        self.dnsresolver = dnsresolver
        self.module = module
        self.cgienv = env
        if portbase and self.port is not None:
            self.port += portbase

    def _set_default_host(self, host):
        if not self.host:
            self.host = host

    def servertype(self):
        s = self.__class__.__name__
        if s.endswith("Factory"):
            s = s[:-7]
        return s

    def create(self):
        raise NotImplementedError(
            "Concrete ServerFactory classes must implement create().")

class SMTPServerFactory(ServerFactory):
    def __init__(self, section):
        ServerFactory.__init__(self, section.address)
                                                                                                           
    def create(self):
        from smtpserver.SMTPServer import SMTPServer
        return SMTPServer(self.host, self.port, self.module)
