37. SQLALCHEMY + URL DISPATCH WIKI TUTORIAL
1 def main(global_config, **settings):
2""" This function returns a Pyramid WSGI application.
3"""
4engine = engine_from_config(settings, ’sqlalchemy.’)
5DBSession.configure(bind=engine)
6config = Configurator(settings=settings)
7 config.add_static_view(’static’, ’static’, cache_max_age=3600)
8config.add_route(’home’, ’/’)
9config.scan()
10 return config.make_wsgi_app()
When you invoke the pserve development.ini command, the main function above is executed. It accepts some settings and returns a WSGI application. (See Startup for more about pserve.)
The main function first creates a SQLAlchemy database engine using engine_from_config from the sqlalchemy. prefixed settings in the development.ini file’s [app:main] section. This will be a URI (something like sqlite://):
1engine = engine_from_config(settings, ’sqlalchemy.’)
main then initializes our SQL database using SQLAlchemy, passing it the engine:
DBSession.configure(bind=engine)
The next step of main is to construct a Configurator object:
config = Configurator(settings=settings)
settings is passed to the Configurator as a keyword argument with the dictionary values passed as the **settings argument. This will be a dictionary of settings parsed from the .ini file, which contains deployment-related values such as pyramid.reload_templates, db_string, etc.
main now calls pyramid.config.Configurator.add_static_view() with two arguments: static (the name), and static (the path):
config.add_static_view(’static’, ’static’, cache_max_age=3600)