How do you access configuration outside of a request in CherryPy?

I have a CherryPy-based webapp that needs to access CherryPy configuration files before a user makes a request. The docs say use:

host = cherrypy.request.app.config ['database'] ['host']

But this will not work outside of the user request. You can also use the application object when starting the application as follows:

...
application = cherrypy.tree.mount (root, '/', app_conf)
host = application.config ['database'] ['host']
...

But I do not see access to the "application" from other classes outside the user request.

I ask, because our application scans several databases, and we configure them when the application starts, and not at the request of the user. I have a feeling that it would be useful elsewhere; so is there a way to save the link to the โ€œapplicationโ€ somewhere or access it through the CherryPy API?

+3
source share
2 answers

is there any way to keep the link to the "application" somewhere ...

Just use regular Python. For a package called "myapp":

# __init__.py
...
application = cherrypy.tree.mount(root, '/', app_conf)
...

# notarequest.py
import myapp
host = myapp.application.config['database']['host']

However, I would recommend using config to configure your database objects, and then check these database objects, rather than checking the configuration.

+1
source

cherrypy.tree, script ( ):

application = cherrypy.tree.apps['']
host = application.config['database']['host']

, , , .

+3

Source: https://habr.com/ru/post/1747553/


All Articles