Skip to main content

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'}

Metadata

Release files for abir 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for abir 0.1.4
File Size Uploaded Provenance
abir-0.1.4.tar.gz 5.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for abir 0.1.4
File Interpreter ABI Platform
abir-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 11.2 kB

Release files / abir-0.1.4.tar.gz

Download URL abir-0.1.4.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5155a3906df61943f1894769d18ef9a02b4444ea6472f544ad7ae3b7c72f6968
BLAKE2b-256 checksum
How to use checksums
9e3ac999d61e211346f7a8ac43eac4109155300dcd797a7bab67c6629a3bfc6e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.9.6 Darwin/21.2.0

Release files / abir-0.1.4-py3-none-any.whl

Download URL abir-0.1.4-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a67263f20b3365550f055af56a6e6ad6de1bdc1efdf2807c45575234ee74c86c
BLAKE2b-256 checksum
How to use checksums
561b436db6ac911bc1df3d8a131d6f4a02988fc9e22cf9d3548e52e6bc204975
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.9.6 Darwin/21.2.0

Security for abir 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page