dynaconf
3.2.6
toml|yaml|json|ini|py
以及可自訂的載入器。[default, development, testing, production]
init, list, write, validate, export
。$ pip install dynaconf
$ cd path/to/your/project/
$ dynaconf init -f toml
Configuring your Dynaconf environment
------------------------------------------
? The file `config.py` was generated.
?️ settings.toml created to hold your settings.
? .secrets.toml created to hold your secrets.
? the .secrets.* is also included in `.gitignore`
beware to not push your secrets to a public repo.
? Dynaconf is configured! read more on https://dynaconf.com
提示:您可以在
dynaconf init -f <fileformat>
上選擇toml|yaml|json|ini|py
toml是預設配置,也是最推薦的配置格式。
.
├── config.py # This is from where you import your settings object (required)
├── .secrets.toml # This is to hold sensitive data like passwords and tokens (optional)
└── settings.toml # This is to hold your application settings (optional)
在檔案config.py
上,Dynaconf init 產生以下樣板文件
from dynaconf import Dynaconf
settings = Dynaconf (
envvar_prefix = "DYNACONF" , # export envvars with `export DYNACONF_FOO=bar`.
settings_files = [ 'settings.yaml' , '.secrets.yaml' ], # Load files in the given order.
)
提示:您可以自己建立文件,而不是使用上面所示的
init
命令,並且您可以提供任何您想要的名稱,而不是預設的config.py
(該文件必須位於您的可導入python 路徑中) -查看您可以的更多選項傳遞給 https://dynaconf.com 上的Dynaconf
類別初始值設定項
將您的設定放在settings.{toml|yaml|ini|json|py}
上
username = " admin "
port = 5555
database = { name = ' mydb ' , schema = ' main ' }
將敏感資訊放在.secrets.{toml|yaml|ini|json|py}
password = " secret123 "
重要提示:
dynaconf init
命令將.secrets.*
放入您的.gitignore
中,以避免它暴露在公共存儲庫中,但您有責任保證其在本地環境中的安全,而且生產環境的建議是使用內置的支援 Hashicorp Vault 服務的密碼和令牌。
現在您可以選擇使用環境變數來覆寫每次執行或每個環境的值。
# override `port` from settings.toml file and automatically casts as `int` value.
export DYNACONF_PORT=9900
在您的程式碼中匯入settings
對象
from path . to . project . config import settings
# Reading the settings
settings . username == "admin" # dot notation with multi nesting support
settings . PORT == 9900 # case insensitive
settings [ 'password' ] == "secret123" # dict like access
settings . get ( "nonexisting" , "default value" ) # Default values just like a dict
settings . databases . name == "mydb" # Nested key traversing
settings [ 'databases.schema' ] == "main" # Nested key traversing
您還可以做更多事情,請閱讀文件: http://dynaconf.com
主要討論發生在“討論”選項卡上,以了解有關如何參與 CONTRIBUTING.md 指南的更多信息
如果您正在尋找類似 Dynaconf 的東西在 Rust 專案中使用:https://github.com/rubik/Hydroconf
特別感謝 Caneco 的標誌。