views:

34

answers:

2

I'm setting up a CherryPy application and would like to have the majority of my configuration settings in a .conf file like this:

[global]
server.socketPort = 8080
server.threadPool = 10
server.environment = "production"

However I would also like to setup a few with a dictionary in code like this:

conf = {'/': {'tools.staticdir.on': True, 
              'tools.staticdir.dir': os.path.join(current_dir, 'templates')}}             

cherrypy.quickstart(HelloWorld(), config=conf)

Is it possible to combine both configs into one and then pass it into the config quickstart option?

A: 

Those are two different configurations. Cherrypy has two configurations: One is the global config and the other is application config. You can use both normally:

cherrypy.config.update('my_file.ini')
cherrypy.quickstart(HelloWorld(), config=conf)

Please note that your example config file is wrong -- instead of server.socketPort it should be server.socket_port and instead of server.threadPool it should be server.threadpool. Check config docs for more information.

nosklo
Ok, but what I really want to do is specify most config values in the my_file.ini, with a couple of additional that require code. Is it possible to do both?
jamesaharvey
+2  A: 

quickstart is for quick sites. If you're doing anything as complex as having multiple configs, it's time to graduate. Look at the source code for the quickstart function (it's not scary!): you're going to unpack that into your startup script. So instead of quickstart, write this:

cherrypy.config.update(conffile)
cherrypy.config.update(confdict)

app = cherrypy.tree.mount(HelloWorld(), '/', conffile)
app.merge(confdict)

if hasattr(cherrypy.engine, "signal_handler"):
    cherrypy.engine.signal_handler.subscribe()
if hasattr(cherrypy.engine, "console_control_handler"):
    cherrypy.engine.console_control_handler.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()

We've essentially added two lines to the quickstart code. First, we have an extra call to config.update; that merges the config dict into the global config. Second, app.merge(confdict); that's for merging multiple configs into each app.

It's perfectly OK to do these in the opposite order if you want the file config to override the dict. It's also OK to stick the dict-based config in HelloWorld._cp_config as described in the docs.

fumanchu
Works great. Thanks!
jamesaharvey