{"id":20635706,"url":"https://github.com/exp-codes/pyyaml-erb","last_synced_at":"2025-08-12T17:45:20.067Z","repository":{"id":57458678,"uuid":"435525060","full_name":"EXP-Codes/pyyaml-erb","owner":"EXP-Codes","description":"解析 yaml 配置文件中的的环境变量","archived":false,"fork":false,"pushed_at":"2023-01-16T17:28:28.000Z","size":77,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-15T21:33:40.482Z","etag":null,"topics":["environment","erb","programming","pyyaml-erb","yaml"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/pyyaml-erb/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EXP-Codes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":["https://lyy289065406.github.io/sponsor/"]}},"created_at":"2021-12-06T14:20:01.000Z","updated_at":"2023-05-03T14:33:36.000Z","dependencies_parsed_at":"2023-02-10T05:16:12.657Z","dependency_job_id":null,"html_url":"https://github.com/EXP-Codes/pyyaml-erb","commit_stats":null,"previous_names":["lyy289065406/pyyaml-erb"],"tags_count":2,"template":false,"template_full_name":"EXP-Codes/pypi-template","purl":"pkg:github/EXP-Codes/pyyaml-erb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EXP-Codes%2Fpyyaml-erb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EXP-Codes%2Fpyyaml-erb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EXP-Codes%2Fpyyaml-erb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EXP-Codes%2Fpyyaml-erb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EXP-Codes","download_url":"https://codeload.github.com/EXP-Codes/pyyaml-erb/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EXP-Codes%2Fpyyaml-erb/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270108869,"owners_count":24528772,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-12T02:00:09.011Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["environment","erb","programming","pyyaml-erb","yaml"],"created_at":"2024-11-16T15:06:39.093Z","updated_at":"2025-08-12T17:45:20.016Z","avatar_url":"https://github.com/EXP-Codes.png","language":"Python","readme":"# pyyaml-erb\n\n\u003e 解析 yaml 配置文件中的的环境变量\n\n------\n\n## 运行环境\n\n![](https://img.shields.io/badge/Python-3.8%2B-brightgreen.svg)\n\n\n## 介绍\n\n模仿 Ruby 的 [ERB](https://docs.ruby-lang.org/en/2.3.0/ERB.html) 能力，把 yaml 配置文件中的环境变量做简单解析。\n\n\n## 安装说明\n\n执行脚本： \n\n```\npython -m pip install --upgrade pip\npython -m pip install pyyaml-erb\n```\n\n\n## 使用说明\n\n在代码中引入 pyyaml-erb 包，读取配置 yml 配置文件即可：\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配置示例可参考 [settings.yml](./tests/config/settings.yml)，使用教程可参考单元测试 [test_yaml_erb.py](./tests/test_yaml_erb.py)。\n\n例如环境变量为 `JAVA_HOME`，只需要在 yaml 配置为 `\u003c%= ENV[\"KEY\"] %\u003e` 或 `\u003c%= ${KEY} %\u003e` 的值表达式即可识别并解析。\n\n一般而言，值表达式有以下几种配置模式：\n\n- `key_1: \u003c%= ENV[\"VAR_1\"] %\u003e`： 默认的使用方式\n- `key_2: \u003c%= ENV[\"VAR_2\"] or None %\u003e`： 跟默认方式一样，多了默认值为 None，没意义\n- `key_3: \u003c%= ENV[\"VAR_3\"] || null %\u003e`： 跟默认方式一样，多了默认值为 None，没意义\n- `key_4: \u003c%= ENV[\"VAR_4\"] || \"nil\" %\u003e`： 跟默认方式一样，多了默认值为 None，没意义\n- `key_5: \u003c%= ENV[\"VAR_5\"] || default %\u003e`： 若环境变量不存在，会设置为默认值\n- `key_6: \"\u003c%= ENV['VAR_6'] or 'default' %\u003e\"`： 若环境变量不存在，会设置为默认值\n- `key_7: \u003c%= ENV[\"VAR_7\"] || 7 %\u003e`： 若环境变量不存在，会设置为默认值，且默认值会解析为整型\n- `key_8: \u003c%= ENV[\"VAR_8\"] || 1.23 %\u003e`： 若环境变量不存在，会设置为默认值，且默认值会解析为浮点型\n- `key_9: \u003c%= ENV[\"VAR_9\"] || true %\u003e`： 若环境变量不存在，会设置为默认值，且默认值会解析为布尔型\n- `key_10: \u003c%= ENV[\"VAR_10\"] || 'False' %\u003e`： 若环境变量不存在，会设置为默认值，且默认值会解析为布尔型\n- `key_0: '\u003c%= ENV[\"VAR_0\"] || ${VAR_11} or default %\u003e'`： 混合模式\n\n\u003e 引号用双引号或单引号都可以，值表达式外围用不用引号包围都可以，表达式之间用 `||` 或 `or` 都可以\n\n","funding_links":["https://lyy289065406.github.io/sponsor/"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexp-codes%2Fpyyaml-erb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fexp-codes%2Fpyyaml-erb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexp-codes%2Fpyyaml-erb/lists"}