17.4. CherryPy and TurboGears Configuration
TurboGears configuration files differ from standard CherryPy configuration files, but even though they are in different places, and split up in different ways, they share the same syntax for configuring CherryPy-specific elements.
TurboGears uses ConfigObj (www.voidspace.org.uk/python/modules.shtml#configobj) to handle parsing the configuration files, and we cover ConfigObj in a bit more detail in Chapter 19, "The TurboGears Toolbox and Other Tools." And cover some of the best ways to configure CherryPy for production use in Chapter 18. But for now, let's take a look at where CherryPy-specific configuration information goes in a TurboGears project, and how to set things up the way you want them.
A TurboGears project has three main configuration files, dev.cfg and prod.cfg in the top-level directory and yourpackage/config/app.cfg. The dev.cfg and prod.cfg files are deployment configs and contain the settings that apply for a given deployment scenario (development and production, respectively).
That means things such as the port to listen on and how your application may or may not be configured behind another production web server are going to be contained in the dev.cfg and prod.cfg files. And because most of the settings for static filters and things such as that are going to be stable in both, app.cfg is your application config and contains settings that apply regardless of where your application is deployed.
In the dev.cfg file, you'll find the following CherryPy-related settings:
# SERVER # Some server parameters that you may want to tweak # server.socket_port=8080 # Enable the debug output at the end on pages. # log_debug_info_filter.on = False server.environment="development" autoreload.package="yourpackage" # Set to True if you'd like to abort execution if a controller gets an # unexpected parameter. False by default tg.strict_parameters = True
Most of this is self-explanatory. You can set the port for CherryPy to listen on and which package to check for changes when deciding when to auto-reload. The prod.cfg has similar server settings. But you'll configure filters in app.cfg because they are a persistent part of your application that should work the same way in production as development.
We've already looked briefly at the static file filter and how it is configured. But let's take another quick look at how filter configuration works:
[/static] static_filter.on = True static_filter.dir = "%(top_level_dir)s/static"
The first line tells CherryPy which paths it should apply this filter to, the second which filter to use, and the third provides filter-specific configuration information (in this case, where the static file directory is located).
You can have as many of these filter configuration elements in your app.cfg file as you need.