Python Pyramid - 项目结构



如前所述,外部 testproj 文件夹包含 testproj 和 test 包。此外,它还包含其他用于描述、运行和测试应用程序的文件。这些文件包括:

  • MANIFEST.in 包含要包含在包源代码分发中的文件列表。

  • development.ini 是一个 PasteDeploy 配置文件,可用于在开发过程中执行应用程序。

  • production.ini 是一个 PasteDeploy 配置文件,可用于在生产环境中执行应用程序。

  • pytest.ini 是用于运行测试的配置文件。

  • setup.py 是标准的 Setuptools setup.py 文件,用于测试和分发应用程序。

  • testing.ini 是用于执行应用程序测试的配置文件。

“.ini” 文件是 Cookiecutter 实用程序用于生成 Pyramid 应用程序结构的配置。这些文件使用名为 PasteDeploy 的系统,该系统由 Ian Bicking 开发。此库与 Pyramid 一起自动安装。

虽然可以在没有 PasteDeploy 支持的情况下开发 Pyramid 应用程序,但它提供了一种标准化的方法来启动、调试和测试应用程序。

预定义设置从配置文件(扩展名为 .ini)中读取。这些文件主要包含应用程序配置设置、服务器设置和日志设置。

development.ini

如前所示,使用 Cookiecutter 构建的 Pyramid 应用程序通过以下命令调用:

pserve development.ini

development.ini 包含应用程序的 PasteDeploy 配置规范。此文件中的配置规范具有各种部分,例如 [app:main]、[server:main]、[loggers] 等。

最重要的部分是 [app:main]。它指定了应用程序的起点。

[app:main]
use = egg:testproj

pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes = pyramid_debugtoolbar
   
sqlalchemy.url = sqlite:///%(here)s/testproj.sqlite

retry.attempts = 3

第一项“use = egg:testproj”指示 Pyramid WSGI 应用程序对象 main 的名称。它在 textproj 包的 __init__.py 文件(位于 testproj 项目文件夹内)中声明。此部分包含其他启动时配置设置。

例如,“pyramid.includes” 设置指定要包含在运行时的包。在上面的示例中,包含了 debugtoolbar 包,以便在单击 Pyramid 徽标时激活调试面板。我们在前面部分看到了它的功能。

我们还看到,此应用程序中要使用的数据库的 URL 也已指定。

[server:main] 部分指定了侦听 TCP 端口 6543 的 WSGI 服务器的配置。它被配置为仅侦听本地主机 (127.0.0.1)。

[server:main]
use = egg:waitress#main
listen = localhost:6543

其他各种与日志相关的部分使用 Python 的日志记录库。这些“.ini” 文件部分传递到日志记录模块的配置文件配置引擎。

production.ini

当应用程序在生产模式下部署时,此文件用于服务应用程序,而不是“development.ini”。这两个文件相似。但是,在“production.ini” 中,调试工具栏已禁用,重新加载选项已禁用并关闭调试选项。

以下是典型“production.ini”文件的简化版本:

[app:main]
use = egg:testproj
pyramid.reload_templates = false
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
sqlalchemy.url = sqlite:///%(here)s/testproj.sqlite
retry.attempts = 3
[pshell]
setup = testproj.pshell.setup
[alembic]
script_location = testproj/alembic
file_template = %%(year)d%%(month).2d%%(day).2d_%%(rev)s
[server:main]
use = egg:waitress#main
listen = *:6543
[loggers]
keys = root, testproj, sqlalchemy, alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
[logger_testproj]
level = WARN
handlers =
qualname = testproj
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = WARN
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s
广告

© . All rights reserved.