CherryPy:访问全局配置
我正在根据我在 BitBucket 存储库 上找到的内容开发 CherryPy 应用程序。
在此示例中,有两个配置文件:server.cfg(又名“全局”)和 app.cfg。
这两个配置文件都加载到 serve.py 文件中:
# Update the global settings for the HTTP server and engine
cherrypy.config.update(os.path.join(self.conf_path, "server.cfg"))
# ...
# Our application
from webapp.app import Twiseless
webapp = Twiseless()
# Let's mount the application so that CherryPy can serve it
app = cherrypy.tree.mount(webapp, '/', os.path.join(self.conf_path, "app.cfg"))
现在,我想添加数据库配置。 我的第一个想法是将其添加到 server.cfg 中(这是最好的地方吗?还是应该位于 app.cfg 中?)。
但是如果我在 server.cfg 中添加数据库配置,我不知道如何访问它。 使用 :
cherrypy.request.app.config['Database']
仅当 [Database] 参数位于 app.cfg 中时才有效。
我尝试打印cherrypy.request.app.config,它只显示app.cfg中定义的值,server.cfg中没有任何内容。
所以我有两个相关的问题:
- 最好将数据库连接放在 server.cfg 或 app.cfg 文件中
- 如何在我的代码中访问 server.cfg 配置(又名全局)
感谢您的帮助! :)
I'm working on a CherryPy application based on what I found on that BitBucket repository.
As in this example, there is two config files, server.cfg (aka "global") and app.cfg.
Both config files are loaded in the serve.py file :
# Update the global settings for the HTTP server and engine
cherrypy.config.update(os.path.join(self.conf_path, "server.cfg"))
# ...
# Our application
from webapp.app import Twiseless
webapp = Twiseless()
# Let's mount the application so that CherryPy can serve it
app = cherrypy.tree.mount(webapp, '/', os.path.join(self.conf_path, "app.cfg"))
Now, I'd like to add the Database configuration.
My first thought was to add it in the server.cfg (is this the best place? or should it be located in app.cfg ?).
But if I add the Database configuration in the server.cfg, I don't know how to access it.
Using :
cherrypy.request.app.config['Database']
Works only if the [Database] parameter is in the app.cfg.
I tried to print cherrypy.request.app.config, and it shows me only the values defined in app.cfg, nothing in server.cfg.
So I have two related question :
- Is it best to put the database connection in the server.cfg or app.cfg file
- How to access server.cfg configuration (aka global) in my code
Thanks for your help! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其放入应用程序配置中。帮助您决定将这些东西放在哪里的一个好问题是,“如果我在同一服务器上的
/blogs
上安装了一个不相关的博客应用程序,我是否希望它共享该配置?”如果是这样,请将其放入服务器配置中。如果没有,请将其放入应用程序配置中。另请注意,全局配置未分段,因此您无论如何都不能在其中粘贴
[Database]
部分。只有应用程序配置允许部分。如果您想将数据库设置保留在全局配置中,则必须考虑诸如“database_port”之类的配置条目名称。然后,您可以通过该名称直接访问它:cherrypy.config.get("database_port")
。Put it in the app config. A good question to help you decide where to put such things is, "if I mounted an unrelated blog app at
/blogs
on the same server, would I want it to share that config?" If so, put it in server config. If not, put it in app config.Note also that the global config isn't sectioned, so you can't stick a
[Database]
section in there anyway. Only the app config allows sections. If you wanted to stick database settings in the global config anyway, you'd have to consider config entry names like "database_port" instead. You would then access it directly by that name:cherrypy.config.get("database_port")
.