
- Python Pyramid 教程
- Python Pyramid - 首頁
- Python Pyramid - 概述
- Pyramid - 環境設定
- Python Pyramid - Hello World
- Pyramid - 應用配置
- Python Pyramid - URL 路由
- Python Pyramid - 檢視配置
- Python Pyramid - 路由字首
- Python Pyramid - 模板
- Pyramid - HTML 表單模板
- Python Pyramid - 靜態資源
- Python Pyramid - 請求物件
- Python Pyramid - 響應物件
- Python Pyramid - 會話
- Python Pyramid - 事件
- Python Pyramid - 訊息閃現
- Pyramid - 使用 SQLAlchemy
- Python Pyramid - Cookiecutter
- Python Pyramid - 建立專案
- Python Pyramid - 專案結構
- Python Pyramid - 包結構
- 手動建立專案
- 命令列 Pyramid
- Python Pyramid - 測試
- Python Pyramid - 日誌
- Python Pyramid - 安全性
- Python Pyramid - 部署
- Python Pyramid 有用資源
- Python Pyramid - 快速指南
- Python Pyramid - 有用資源
- Python Pyramid - 討論
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