def __init__(self, config): self.config = config # set logging level = LEVELS.get(config.loglevel, logging.NOTSET) logging.basicConfig(level=level) self.logger = logging.getLogger('mother') if config.logfile != '-': self.logger.addHandler(logging.FileHandler(config.logfile)) self.logger.info('Initializing mother') #.init database self.db = Storage(config.database) # load authentification #TODO: should be optional import mother.authentication self.db.create() self.plug = Pluggable(self.db, Context(self)) from mother import routing import functools routing.url = functools.partial(routing.url, self.plug)
class Mother(object): def __init__(self, config): self.config = config # set logging level = LEVELS.get(config.loglevel, logging.NOTSET) logging.basicConfig(level=level) self.logger = logging.getLogger('mother') if config.logfile != '-': self.logger.addHandler(logging.FileHandler(config.logfile)) self.logger.info('Initializing mother') #.init database self.db = Storage(config.database) # load authentification #TODO: should be optional import mother.authentication self.db.create() self.plug = Pluggable(self.db, Context(self)) from mother import routing import functools routing.url = functools.partial(routing.url, self.plug) def run(self, foreground=False): root = Resource() from mother.authentication import MotherRealm, AuthWrapper from twisted.cred.portal import Portal portal = Portal(MotherRealm(root)) from twisted.cred import checkers, credentials mycheck = checkers.InMemoryUsernamePasswordDatabaseDontUse() mycheck.addUser('foo', 'bar') portal.registerChecker(mycheck) # allow anonymous access (for login form) #portal.registerChecker(checkers.AllowAnonymousAccess(), credentials.IAnonymous) #self.plug.initialize(root) from twisted.web.guard import DigestCredentialFactory from twisted.web.guard import HTTPAuthSessionWrapper wrapper = AuthWrapper(portal, [DigestCredentialFactory('md5', 'example.org')]) # ssl on #wrapper = HTTPAuthSessionWrapper(portal, [DigestCredentialFactory('md5', 'example.org')]) #wrapper.putChild(root) #root = wrapper # /ssl # /ssl self.plug.initialize(root) #. FINAL!!! start listening on the network site = server.Site(root, logPath=self.config.accesslog) self.logger.info('Mother start listening...') if foreground: reactor.listenTCP(self.config.port, site) reactor.run() return application = service.Application('mother') svc = internet.TCPServer(self.config.port, site) #, interface='*') svc.setServiceParent(application) return application def list_plugins(self): return self.plug.list()