Good question. Pyramid can use PasteDeploy configuration files for application configuration. Therefore it offers pyramid.paster for that. Consider you want to setup your database tables before running your web application the first time, or have a maintenance script that runs on a daily basis. You want both scripts to take the connection string from the development.ini/production.ini as well. This is considered good practice.
This is a common yourwebapp.__init__.py
you already know. PasteDeploy configuration is passed into main() function by running bin\pserve development.ini
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from .models import DBSession, Base
def main(global_config, **settings):
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
config = Configurator(settings=settings,
root_factory='tutorial.models.Root')
config.include('pyramid_chameleon')
config.add_route('wiki_view', '/')
config.add_route('wikipage_add', '/add')
config.add_route('wikipage_view', '/{uid}')
config.add_route('wikipage_edit', '/{uid}/edit')
config.add_static_view('deform_static', 'deform:static/')
config.scan('.views')
return config.make_wsgi_app()
To have acccess to your application configuration in another script say some_script.py
as well use methods from pyramid.paster to read settings and init logging from settings as well.
from sqlalchemy import engine_from_config
from pyramid.paster import (
get_appsettings,
setup_logging,
)
from .models import (
DBSession,
Base,
)
def do_your_stuff(settings)
"your custom script logic that extracts a certain settings and does something"
def main(argv=sys.argv):
config_uri = argv[1]
setup_logging(config_uri)
settings = get_appsettings(config_uri)
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.create_all(engine)
do_your_stuff(settings)
To wire up everything you need to add an entry point for a command-line script in your setup.py. Read carefully the console_script
section. You define a name for script and point to 'main' function of your some_script.py. This is want you need to customize for every application and script.
from setuptools import setup
requires = [
'pyramid',
'pyramid_chameleon',
'deform',
'sqlalchemy',
'pyramid_tm',
'zope.sqlalchemy'
]
setup(name='tutorial',
install_requires=requires,
entry_points="""\
[paste.app_factory]
main = tutorial:main
[console_scripts]
initialize_db = yourwebapp.some_script:main
""",
)
Because your changed setup.py you need to run the command below to create the console script that you will find in 'bin' folder (same folder where you call pserve from). I take for granted that are running a virtualenv.
$ $VENV/bin/python setup.py develop
Afterwards you can run you script with your configuration file
$ $VENV/bin/initialize_db development.ini
In production take another configuration file. That's the beauty.
$ $VENV/bin/initialize_db production.ini
Several third-party libraries offer special helper function (factories) that are driven by paster configuration files. Examples are sqlalchemy, pyramid.zodbconn usually have methods with names like *_from_config(settings)
.
References
I tried to make it a bit shorter for you. Watchout for PasteDeploy topics and pyramid.paster stuff in other pyramid apps.