toml|yaml|json|ini|py
およびカスタマイズ可能なローダー。[default, development, testing, production]
init, list, write, validate, export
などの一般的な操作用の CLI。$ 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
コマンドは、パブリック リポジトリでの公開を避けるために.gitignore
に.secrets.*
を配置しますが、それをローカル環境で安全に保管するのはユーザーの責任です。また、運用環境では、ビルドされたパスワードとトークンの 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 ガイドをご覧ください。
Rust プロジェクトで使用する Dynaconf に似たものを探している場合: https://github.com/rubik/hydroconf
そして、ロゴを提供してくれたCanecoに特別な感謝を捧げます。