Skip to main content

Used to parse environment variables in the yaml configuration file

Project description

pyyaml-erb

解析 yaml 配置文件中的的环境变量


运行环境

介绍

模仿 Ruby 的 ERB 能力,把 yaml 配置文件中的环境变量做简单解析。

安装说明

执行脚本:

python -m pip install --upgrade pip
python -m pip install pyyaml-erb

使用说明

在代码中引入 pyyaml-erb 包,读取配置 yml 配置文件即可:

import erb.yml as yaml
with open(SETTING_PATH, 'r', encoding='utf-8') as file :
    settings = yaml.load(file.read())

配置示例可参考 settings.yml,使用教程可参考单元测试 test_yaml_erb.py

例如环境变量为 JAVA_HOME,只需要在 yaml 配置为 <%= ENV["KEY"] %><%= ${KEY} %> 的值表达式即可识别并解析。

一般而言,值表达式有以下几种配置模式:

  • key_1: <%= ENV["VAR_1"] %>: 默认的使用方式
  • key_2: <%= ENV["VAR_2"] or None %>: 跟默认方式一样,多了默认值为 None,没意义
  • key_3: <%= ENV["VAR_3"] || null %>: 跟默认方式一样,多了默认值为 None,没意义
  • key_4: <%= ENV["VAR_4"] || "nil" %>: 跟默认方式一样,多了默认值为 None,没意义
  • key_5: <%= ENV["VAR_5"] || default %>: 若环境变量不存在,会设置为默认值
  • key_6: "<%= ENV['VAR_6'] or 'default' %>": 若环境变量不存在,会设置为默认值
  • key_7: <%= ENV["VAR_7"] || 7 %>: 若环境变量不存在,会设置为默认值,且默认值会解析为整型
  • key_8: <%= ENV["VAR_8"] || 1.23 %>: 若环境变量不存在,会设置为默认值,且默认值会解析为浮点型
  • key_9: <%= ENV["VAR_9"] || true %>: 若环境变量不存在,会设置为默认值,且默认值会解析为布尔型
  • key_10: <%= ENV["VAR_10"] || 'False' %>: 若环境变量不存在,会设置为默认值,且默认值会解析为布尔型
  • key_0: '<%= ENV["VAR_0"] || ${VAR_11} or default %>': 混合模式

引号用双引号或单引号都可以,值表达式外围用不用引号包围都可以,表达式之间用 ||or 都可以

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

pyyaml-erb-1.0.6.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

pyyaml_erb-1.0.6-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file pyyaml-erb-1.0.6.tar.gz.

File metadata

  • Download URL: pyyaml-erb-1.0.6.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pyyaml-erb-1.0.6.tar.gz
Algorithm Hash digest
SHA256 2d0ebcd66944f49b765d866cb6b01a72b2a4ddfbd1cdd74e6f2d3d8fc18686a6
MD5 dceb35a3845f70e1986f5f2e37d1434d
BLAKE2b-256 b32a175696695f9eb3aff2bf5e79fb4353c31faf622d030c3b28ce49e7e63770

See more details on using hashes here.

File details

Details for the file pyyaml_erb-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: pyyaml_erb-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pyyaml_erb-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 b5701de94c0961eeb3f3e2e63933669c484e339578c0244427230d5a0fb03aa4
MD5 6852c7c76f68380d08110d425e41b71f
BLAKE2b-256 a7658c0b6f8940821f4875fd29e51c4d92c899201c3b9d64b2c0e5c3dedb57e3

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