import logging
log = logging.getLogger(__name__)

from pyramid.config import Configurator
from sqlalchemy import engine_from_config

from pyramid.session import UnencryptedCookieSessionFactoryConfig
from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy

from models import DBSession

from sqlalchemy.exc import DBAPIError

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    config = Configurator(settings=settings)

    if not 'pyramid.sess.secret' in settings:
        log.error('pyramid.sess.secret is not set, unable to start.')
        exit(-1)

    if not 'pyramid.auth.secret' in settings:
        log.error('pyramid.auth.secret is not set. Refusing to start.')
        exit(-1)

    if not 'pyramid.upload_path' in settings:
        log.error('pyramid.upload_path us not set. Refusing to start.')
        exit(-1)

    _session_factory = UnencryptedCookieSessionFactoryConfig(settings['pyramid.sess.secret'],
            cookie_httponly=True,
            cookie_max_age=864000
            )

    _authn_policy = AuthTktAuthenticationPolicy(
            settings['pyramid.auth.secret'],
            max_age=864000,
            http_only=True,
            hashalg='sha512',
            callback=lambda x : None,
            )

    _authz_policy = ACLAuthorizationPolicy()


    config.set_session_factory(_session_factory)
    config.set_authentication_policy(_authn_policy)
    config.set_authorization_policy(_authz_policy)

    config.include(add_routes)
    config.include(add_views)
    config.include(add_events)

    return config.make_wsgi_app()

def add_routes(config):
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('alexandria.home', '/')

def add_views(config):
    config.add_view('alexandria.views.home.home', route_name='alexandria.home',
            renderer='home.mako')

    # Error pages
    config.add_view('alexandria.views.errors.db_failed', context=DBAPIError, renderer='error_db_failed.mako')
    config.add_notfound_view('alexandria.views.errors.not_found', renderer='error_not_found.mako', append_slash=False)

def add_events(config):
    pass


@@ -130,2204 +130,341 @@
 ig%0A%0A
-from pyramid.session import UnencryptedCookieSessionFactoryConfig%0Afrom pyramid.authentication import AuthTktAuthenticationPolicy%0Afrom pyramid.authorization import ACLAuthorizationPolicy%0A%0Afrom models import DBSession%0A%0Afrom sqlalchemy.exc import DBAPIError%0A%0Adef main(global_config, **settings):%0A    %22%22%22 This function returns a Pyramid WSGI application.%0A    %22%22%22%0A    engine = engine_from_config(settings, 'sqlalchemy.')%0A    DBSession.configure(bind=engine)%0A    config = Configurator(settings=settings)%0A%0A    if not 'pyramid.sess.secret' in settings:%0A        log.error('pyramid.sess.secret is not set, unable to start.')%0A        exit(-1)%0A%0A    if not 'pyramid.auth.secret' in settings:%0A        log.error('pyramid.auth.secret is not set. Refusing to start.')%0A        exit(-1)%0A%0A    if not 'pyramid.upload_path' in settings:%0A        log.error('pyramid.upload_path us not set. Refusing to start.')%0A        exit(-1)%0A%0A    _session_factory = UnencryptedCookieSessionFactoryConfig(settings%5B'pyramid.sess.secret'%5D,%0A            cookie_httponly=True,%0A            cookie_max_age=864000%0A            )%0A%0A    _authn_policy = AuthTktAuthenticationPolicy(%0A            settings%5B'pyramid.auth.secret'%5D,%0A            max_age=864000,%0A            http_only=True,%0A            hashalg='sha512',%0A            callback=lambda x : None,%0A            )%0A%0A    _authz_policy = ACLAuthorizationPolicy()%0A%0A%0A    config.set_session_factory(_session_factory)%0A    config.set_authentication_policy(_authn_policy)%0A    config.set_authorization_policy(_authz_policy)%0A%0A    config.include(add_routes)%0A    config.include(add_views)%0A    config.include(add_events)%0A%0A    return config.make_wsgi_app()%0A%0Adef add_routes(config):%0A    config.add_static_view('static', 'static', cache_max_age=3600)%0A    config.add_route('alexandria.home', '/')%0A%0Adef add_views(config):%0A    config.add_view('alexandria.views.home.home', route_name='alexandria.home',%0A            renderer='home.mako')%0A%0A    # Error pages%0A    config.add_view('alexandria.views.errors.db_failed', context=DBAPIError, renderer='error_db_failed.mako')%0A    config.add_notfound_view('alexandria.views.errors.not_found', renderer='error_not_found.mako', append_slash=False)%0A%0Adef add_events(config):%0A    pass
+%0A%0Adef main(global_config, **settings):%0A    %22%22%22 This function returns a Pyramid WSGI application.%0A    %22%22%22%0A    engine = engine_from_config(settings, 'sqlalchemy.')%0A    DBSession.configure(bind=engine)%0A    config = Configurator(settings=settings)%0A%0A%0A%0A        exit(-1)%0A%0A%0A    config.add_static_view('static', 'static', cache_max_age=3600)%0A%0A%0A
 %0A%0A

Remove useless code in __init__.py