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
تلميح: يمكنك تحديد
toml|yaml|json|ini|py
علىdynaconf init -f <fileformat>
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 القابل للاستيراد) - شاهد المزيد من الخيارات التي يمكنك القيام بها انتقل إلى مُهيئ فئةDynaconf
على https://dynaconf.com
ضع إعداداتك على 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 على الشعار.