Catalog PyPI
PACKAGE EXPLORERView in registry

abir

PyPI

python project load config from yaml and environment.

configconfiguredjangopythonpyyamlyaml
Versions collected1
Direct dependencies1From release 0.1.4
Last observed20 Oct, 17:26 UTC

README

Version 0.1.4 documentation

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

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 1

Collected releases
VersionPublishedDirect dependenciesOpen version
0.1.45 Jan 20221

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
jioone/abir

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 17:26 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:fef9ac3501b3e3925ce587743bbf83d2446c8e3981b73f5ad654f7b77846c9c5