pyyaml-erb


Namepyyaml-erb JSON
Version 1.0.6 PyPI version JSON
download
home_pagehttps://github.com/lyy289065406/pyyaml-erb
SummaryUsed to parse environment variables in the yaml configuration file
upload_time2023-01-08 12:30:02
maintainer
docs_urlNone
authorEXP
requires_python>=3.5, <4
license
keywords yaml erb environment
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            # pyyaml-erb

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

------

## 运行环境

![](https://img.shields.io/badge/Python-3.8%2B-brightgreen.svg)


## 介绍

模仿 Ruby 的 [ERB](https://docs.ruby-lang.org/en/2.3.0/ERB.html) 能力,把 yaml 配置文件中的环境变量做简单解析。


## 安装说明

执行脚本: 

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


## 使用说明

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

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

配置示例可参考 [settings.yml](./tests/config/settings.yml),使用教程可参考单元测试 [test_yaml_erb.py](./tests/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` 都可以


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/lyy289065406/pyyaml-erb",
    "name": "pyyaml-erb",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.5, <4",
    "maintainer_email": "",
    "keywords": "yaml,erb,environment",
    "author": "EXP",
    "author_email": "289065406@qq.com",
    "download_url": "https://files.pythonhosted.org/packages/b3/2a/175696695f9eb3aff2bf5e79fb4353c31faf622d030c3b28ce49e7e63770/pyyaml-erb-1.0.6.tar.gz",
    "platform": null,
    "description": "# pyyaml-erb\n\n> \u89e3\u6790 yaml \u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u7684\u73af\u5883\u53d8\u91cf\n\n------\n\n## \u8fd0\u884c\u73af\u5883\n\n![](https://img.shields.io/badge/Python-3.8%2B-brightgreen.svg)\n\n\n## \u4ecb\u7ecd\n\n\u6a21\u4eff Ruby \u7684 [ERB](https://docs.ruby-lang.org/en/2.3.0/ERB.html) \u80fd\u529b\uff0c\u628a yaml \u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u73af\u5883\u53d8\u91cf\u505a\u7b80\u5355\u89e3\u6790\u3002\n\n\n## \u5b89\u88c5\u8bf4\u660e\n\n\u6267\u884c\u811a\u672c\uff1a \n\n```\npython -m pip install --upgrade pip\npython -m pip install pyyaml-erb\n```\n\n\n## \u4f7f\u7528\u8bf4\u660e\n\n\u5728\u4ee3\u7801\u4e2d\u5f15\u5165 pyyaml-erb \u5305\uff0c\u8bfb\u53d6\u914d\u7f6e yml \u914d\u7f6e\u6587\u4ef6\u5373\u53ef\uff1a\n\n```python\nimport erb.yml as yaml\nwith open(SETTING_PATH, 'r', encoding='utf-8') as file :\n    settings = yaml.load(file.read())\n```\n\n\u914d\u7f6e\u793a\u4f8b\u53ef\u53c2\u8003 [settings.yml](./tests/config/settings.yml)\uff0c\u4f7f\u7528\u6559\u7a0b\u53ef\u53c2\u8003\u5355\u5143\u6d4b\u8bd5 [test_yaml_erb.py](./tests/test_yaml_erb.py)\u3002\n\n\u4f8b\u5982\u73af\u5883\u53d8\u91cf\u4e3a `JAVA_HOME`\uff0c\u53ea\u9700\u8981\u5728 yaml \u914d\u7f6e\u4e3a `<%= ENV[\"KEY\"] %>` \u6216 `<%= ${KEY} %>` \u7684\u503c\u8868\u8fbe\u5f0f\u5373\u53ef\u8bc6\u522b\u5e76\u89e3\u6790\u3002\n\n\u4e00\u822c\u800c\u8a00\uff0c\u503c\u8868\u8fbe\u5f0f\u6709\u4ee5\u4e0b\u51e0\u79cd\u914d\u7f6e\u6a21\u5f0f\uff1a\n\n- `key_1: <%= ENV[\"VAR_1\"] %>`\uff1a \u9ed8\u8ba4\u7684\u4f7f\u7528\u65b9\u5f0f\n- `key_2: <%= ENV[\"VAR_2\"] or None %>`\uff1a \u8ddf\u9ed8\u8ba4\u65b9\u5f0f\u4e00\u6837\uff0c\u591a\u4e86\u9ed8\u8ba4\u503c\u4e3a None\uff0c\u6ca1\u610f\u4e49\n- `key_3: <%= ENV[\"VAR_3\"] || null %>`\uff1a \u8ddf\u9ed8\u8ba4\u65b9\u5f0f\u4e00\u6837\uff0c\u591a\u4e86\u9ed8\u8ba4\u503c\u4e3a None\uff0c\u6ca1\u610f\u4e49\n- `key_4: <%= ENV[\"VAR_4\"] || \"nil\" %>`\uff1a \u8ddf\u9ed8\u8ba4\u65b9\u5f0f\u4e00\u6837\uff0c\u591a\u4e86\u9ed8\u8ba4\u503c\u4e3a None\uff0c\u6ca1\u610f\u4e49\n- `key_5: <%= ENV[\"VAR_5\"] || default %>`\uff1a \u82e5\u73af\u5883\u53d8\u91cf\u4e0d\u5b58\u5728\uff0c\u4f1a\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\n- `key_6: \"<%= ENV['VAR_6'] or 'default' %>\"`\uff1a \u82e5\u73af\u5883\u53d8\u91cf\u4e0d\u5b58\u5728\uff0c\u4f1a\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\n- `key_7: <%= ENV[\"VAR_7\"] || 7 %>`\uff1a \u82e5\u73af\u5883\u53d8\u91cf\u4e0d\u5b58\u5728\uff0c\u4f1a\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\uff0c\u4e14\u9ed8\u8ba4\u503c\u4f1a\u89e3\u6790\u4e3a\u6574\u578b\n- `key_8: <%= ENV[\"VAR_8\"] || 1.23 %>`\uff1a \u82e5\u73af\u5883\u53d8\u91cf\u4e0d\u5b58\u5728\uff0c\u4f1a\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\uff0c\u4e14\u9ed8\u8ba4\u503c\u4f1a\u89e3\u6790\u4e3a\u6d6e\u70b9\u578b\n- `key_9: <%= ENV[\"VAR_9\"] || true %>`\uff1a \u82e5\u73af\u5883\u53d8\u91cf\u4e0d\u5b58\u5728\uff0c\u4f1a\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\uff0c\u4e14\u9ed8\u8ba4\u503c\u4f1a\u89e3\u6790\u4e3a\u5e03\u5c14\u578b\n- `key_10: <%= ENV[\"VAR_10\"] || 'False' %>`\uff1a \u82e5\u73af\u5883\u53d8\u91cf\u4e0d\u5b58\u5728\uff0c\u4f1a\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\uff0c\u4e14\u9ed8\u8ba4\u503c\u4f1a\u89e3\u6790\u4e3a\u5e03\u5c14\u578b\n- `key_0: '<%= ENV[\"VAR_0\"] || ${VAR_11} or default %>'`\uff1a \u6df7\u5408\u6a21\u5f0f\n\n> \u5f15\u53f7\u7528\u53cc\u5f15\u53f7\u6216\u5355\u5f15\u53f7\u90fd\u53ef\u4ee5\uff0c\u503c\u8868\u8fbe\u5f0f\u5916\u56f4\u7528\u4e0d\u7528\u5f15\u53f7\u5305\u56f4\u90fd\u53ef\u4ee5\uff0c\u8868\u8fbe\u5f0f\u4e4b\u95f4\u7528 `||` \u6216 `or` \u90fd\u53ef\u4ee5\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Used to parse environment variables in the yaml configuration file",
    "version": "1.0.6",
    "split_keywords": [
        "yaml",
        "erb",
        "environment"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a7658c0b6f8940821f4875fd29e51c4d92c899201c3b9d64b2c0e5c3dedb57e3",
                "md5": "6852c7c76f68380d08110d425e41b71f",
                "sha256": "b5701de94c0961eeb3f3e2e63933669c484e339578c0244427230d5a0fb03aa4"
            },
            "downloads": -1,
            "filename": "pyyaml_erb-1.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6852c7c76f68380d08110d425e41b71f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5, <4",
            "size": 5414,
            "upload_time": "2023-01-08T12:30:00",
            "upload_time_iso_8601": "2023-01-08T12:30:00.759477Z",
            "url": "https://files.pythonhosted.org/packages/a7/65/8c0b6f8940821f4875fd29e51c4d92c899201c3b9d64b2c0e5c3dedb57e3/pyyaml_erb-1.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b32a175696695f9eb3aff2bf5e79fb4353c31faf622d030c3b28ce49e7e63770",
                "md5": "dceb35a3845f70e1986f5f2e37d1434d",
                "sha256": "2d0ebcd66944f49b765d866cb6b01a72b2a4ddfbd1cdd74e6f2d3d8fc18686a6"
            },
            "downloads": -1,
            "filename": "pyyaml-erb-1.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "dceb35a3845f70e1986f5f2e37d1434d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5, <4",
            "size": 9463,
            "upload_time": "2023-01-08T12:30:02",
            "upload_time_iso_8601": "2023-01-08T12:30:02.233198Z",
            "url": "https://files.pythonhosted.org/packages/b3/2a/175696695f9eb3aff2bf5e79fb4353c31faf622d030c3b28ce49e7e63770/pyyaml-erb-1.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-08 12:30:02",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "lyy289065406",
    "github_project": "pyyaml-erb",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pyyaml-erb"
}
        
EXP
Elapsed time: 0.12027s