Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CherryPy combine file and dictionary based configuration

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?

like image 895
jamesaharvey Avatar asked Oct 21 '10 16:10

jamesaharvey


1 Answers

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.

like image 146
fumanchu Avatar answered Sep 20 '22 23:09

fumanchu