Skip to main content

python project load config from yaml and environment.

Project description

Abir--Python项目配置方案 yaml/environ

安装

pip install abir

快速上手

django project

  1. settings.py 中添加
import abir  

# other settings

abir.load() # at the end of settings.py
  1. 在项目根文件夹下添加config.yaml

添加后的项目结构如下:

├── project
│   ├── project
│   |   ├── __init__.py
│   |   ├── asgi.py
│   |   ├── wsgi.py
│   |   ├── urls.py
│   |   ├── settings.py
│   ├── manange.py
│   ├── config.yaml  # 添加到根下

在yaml中添加对应的配置项

# settings 中已配置,只希望修改部分配置项时,使用 dot 查询并修改:
DATABASES.default.NAME: 'db_name'
DATABASES.default.HOST: 'dh_host'  # 未配置时,会添加配置项
DATABASES.default.PORT: 'port'
DATABASES.default.USER: 'db_user_name'
DATABASES.default.PASSWORD: 'db_password'

# settings中无配置,或已配置,但希望全部替换,不使用 dot 查询:
CACHE: 
  default:
    BACKEND: 'django_redis.cache.RedisCache'
    LOCATION: 'redis://127.0.0.1:6379/1'
    OPTIONS:
      CLIENT_CLASS: 'django_redis.client.DefaultClient'
LANGUAGE_CODE: 'zh-CN'
USE_TZ: true
ALLOWED_HOSTS:
  - *

⚠️ dot.将会查询settings.py,并更新查询路径下的值。

  1. 启动服务。
python manmage.py runserver
# or wsgi

其他python项目

假设项目结构如下:

├── project
│   ├── packagges
│   ├── modules 
…… 
  1. 添加config_module.py (module名称可自定义)

    如下添加代码

    import abir
    abir.load(base_dir=BASE_DIR, conf_module='conf_module')  
    # 如果config_module不在根下,输入完整查询路径即可,如:project.packageA.moduleB
    # confi_module 也可以是任何可设置property的对象:getattr and setattr
    
  2. 添加config.yaml

    添加后的项目结构如下:

    ├── project
    │   ├── config_module.py
    │   ├── config.yaml # 添加到根下
    
  3. 执行应用,即可获取配置

environment 通过环境变量来进行配置

⚠️ 环境变量拥有最高优先级:当yaml/settings中存在配置,且环境变量中也存在,优先取环境变量的配置值,即:environ > yaml > settings(当load()在conf_module末尾调用时)

前缀

abir通过前缀 ABIR_捕获环境变量。

1. 字符串类型
ABIR_LANGUAGE_CODE=es-us
2. 其他类型

abir读取环境变量时,会识别 :定义,当定义为 :json ,将运行 json.loads进行值转换,因此可以通过赋值环境变量为json-string的方式,来满足非字符串类型的配置

ABIR_LANGUAGE_CODE=zh-CN
ABIR_USE_TZ:json=false
ABIR_TIMEOUT:json=20
ABIR_BLACK_UIDS:json=[101,39,847,11]
ABIR_LIFETIME:json={"days": 1, "key": "some-key"}  # 注意 json-string 与 前端书写json的区别。

以上配置,将会被abir解读为:

LANGUAGE_CODE='zh-CN'
USE_TZ=False
TIMEOUT=20
BLACK_UIDS=[101,39,847,11]
LIFETIME={'days': 1, 'key': 'some-key'}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

abir-0.1.4.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

abir-0.1.4-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file abir-0.1.4.tar.gz.

File metadata

  • Download URL: abir-0.1.4.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.6 Darwin/21.2.0

File hashes

Hashes for abir-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5155a3906df61943f1894769d18ef9a02b4444ea6472f544ad7ae3b7c72f6968
MD5 9262d3c8b08e40569a046cdd5b4274e6
BLAKE2b-256 9e3ac999d61e211346f7a8ac43eac4109155300dcd797a7bab67c6629a3bfc6e

See more details on using hashes here.

File details

Details for the file abir-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: abir-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.6 Darwin/21.2.0

File hashes

Hashes for abir-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a67263f20b3365550f055af56a6e6ad6de1bdc1efdf2807c45575234ee74c86c
MD5 76f2a1e377345c3e4a43304648d9ed34
BLAKE2b-256 561b436db6ac911bc1df3d8a131d6f4a02988fc9e22cf9d3548e52e6bc204975

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page