{"id":34106844,"url":"https://github.com/pengcunfu/pcf-config","last_synced_at":"2026-04-08T12:33:03.712Z","repository":{"id":317559341,"uuid":"1067813824","full_name":"pengcunfu/pcf-config","owner":"pengcunfu","description":"一个简单灵活的 Python YAML 配置文件管理库，支持嵌套键、默认值、热重载和读写操作","archived":false,"fork":false,"pushed_at":"2025-11-11T00:24:19.000Z","size":48,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-03-09T18:12:21.257Z","etag":null,"topics":["config-management","configuration","configuration-file","python","yaml"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pengcunfu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-10-01T12:33:07.000Z","updated_at":"2025-11-11T00:26:20.000Z","dependencies_parsed_at":"2025-10-01T18:09:03.912Z","dependency_job_id":"2dbfd749-6e00-48d4-9be4-a475a30f9c31","html_url":"https://github.com/pengcunfu/pcf-config","commit_stats":null,"previous_names":["pengcunfu/pcf-config"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/pengcunfu/pcf-config","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengcunfu%2Fpcf-config","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengcunfu%2Fpcf-config/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengcunfu%2Fpcf-config/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengcunfu%2Fpcf-config/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pengcunfu","download_url":"https://codeload.github.com/pengcunfu/pcf-config/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengcunfu%2Fpcf-config/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31556232,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T10:21:54.569Z","status":"ssl_error","status_checked_at":"2026-04-08T10:21:38.171Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["config-management","configuration","configuration-file","python","yaml"],"created_at":"2025-12-14T18:03:56.071Z","updated_at":"2026-04-08T12:33:03.703Z","avatar_url":"https://github.com/pengcunfu.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PCF Config\n\n[![PyPI version](https://badge.fury.io/py/pcf-config.svg)](https://badge.fury.io/py/pcf-config)\n[![Python versions](https://img.shields.io/pypi/pyversions/pcf-config.svg)](https://pypi.org/project/pcf-config/)\n[![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n\n[English](README_EN.md) | 简体中文\n\n一个简单灵活的 Python 应用程序 YAML 配置文件管理库。PCF Config 提供了简单易用的方式来管理配置文件，支持嵌套键、默认值和读写操作。\n\n## 特性\n\n- 🔧 **简单 API**：易于使用的获取和设置配置值的方法\n- 🏗️ **嵌套键**：支持点号分隔的嵌套键访问（如 `database.host`）\n- 🛡️ **默认值**：优雅处理缺失键并提供默认值\n- 🔄 **热重载**：运行时重新加载配置文件\n- ✏️ **读写操作**：设置配置值并保存到文件\n- 🪶 **轻量级**：最小依赖（仅需 PyYAML）\n- 🎯 **类型提示**：完整的类型提示支持，提供更好的 IDE 体验\n- 🚀 **非单例**：支持多个实例，提供更大的灵活性\n\n## 安装\n\n从 PyPI 安装：\n\n```bash\npip install pcf-config\n```\n\n## 快速开始\n\n1. **创建配置文件** (`config.yaml`)：\n\n```yaml\ndatabase:\n  host: localhost\n  port: 5432\n  name: myapp\n\napi:\n  host: 0.0.0.0\n  port: 8000\n  debug: true\n```\n\n2. **在 Python 代码中使用**：\n\n```python\nfrom pcf_config import Config\n\n# 使用文件路径创建配置实例\nconfig = Config(\"config.yaml\")\n\n# 获取配置值\ndb_host = config.get(\"database.host\")  # 返回: \"localhost\"\ndb_port = config.get(\"database.port\")  # 返回: 5432\n\n# 使用默认值获取\nredis_host = config.get(\"redis.host\", \"localhost\")\ntimeout = config.get(\"api.timeout\", 30)\n\n# 设置新值\nconfig.set(\"database.timeout\", 30)\nconfig.set(\"api.debug\", True)\n\n# 保存更改到文件\nconfig.save()\n\nprint(f\"数据库: {db_host}:{db_port}\")\nprint(f\"Redis: {redis_host}\")\n```\n\n## API 参考\n\n### Config 类\n\n配置管理的主要类：\n\n```python\nfrom pcf_config import Config\n\n# 使用文件路径创建配置实例\nconfig = Config(\"config.yaml\")\n\n# 获取配置值\nvalue = config.get(\"key\")\nvalue_with_default = config.get(\"key\", \"default\")\n\n# 设置配置值\nconfig.set(\"new.key\", \"value\")\n\n# 保存更改到文件\nconfig.save()\n\n# 从文件重新加载\nconfig.reload()\n\n# 检查键是否存在\nif config.has_key(\"key\"):\n    print(\"键存在\")\n```\n\n#### 方法\n\n##### `__init__(config_file: str)`\n使用配置文件路径初始化 Config 实例。\n\n**参数：**\n- `config_file` (str)：YAML 配置文件的路径\n\n##### `get(key: str, default: Any = ...) -\u003e Any`\n通过键获取配置值。支持使用点号分隔的嵌套键。\n\n**参数：**\n- `key` (str)：配置键，支持点号分隔的嵌套键\n- `default` (Any)：如果键不存在时的默认值\n\n**返回：**\n- `Any`：配置值或默认值\n\n**抛出：**\n- `KeyError`：如果键不存在且未提供默认值\n\n##### `set(key: str, value: Any) -\u003e None`\n设置配置值。如果需要会创建嵌套结构。\n\n**参数：**\n- `key` (str)：配置键，支持点号分隔的嵌套键\n- `value` (Any)：要设置的值\n\n##### `save() -\u003e None`\n将当前配置保存到文件。\n\n##### `reload() -\u003e None`\n从磁盘重新加载配置文件。\n\n##### `has_key(key: str) -\u003e bool`\n检查配置键是否存在。\n\n**参数：**\n- `key` (str)：要检查的配置键\n\n**返回：**\n- `bool`：如果键存在返回 True，否则返回 False\n\n## 配置文件位置\n\nPCF Config 要求您在创建 Config 实例时指定配置文件路径：\n\n```python\nconfig = Config(\"path/to/your/config.yaml\")\n```\n\n## 示例\n\n### 基本用法\n\n```python\nfrom pcf_config import Config\n\n# 创建配置实例\nconfig = Config(\"config.yaml\")\n\n# config.yaml:\n# app:\n#   name: \"我的应用\"\n#   version: \"1.0.0\"\n#   debug: true\n# database:\n#   host: \"localhost\"\n#   port: 5432\n\napp_name = config.get(\"app.name\")           # \"我的应用\"\napp_version = config.get(\"app.version\")     # \"1.0.0\"\ndb_host = config.get(\"database.host\")       # \"localhost\"\n\n# 对缺失的键使用默认值\ncache_ttl = config.get(\"cache.ttl\", 3600)  # 3600 (默认值)\ndebug_mode = config.get(\"app.debug\", False)  # True (来自配置)\n```\n\n### 错误处理\n\n```python\nfrom pcf_config import Config\n\nconfig = Config(\"config.yaml\")\n\ntry:\n    api_key = config.get(\"api.secret_key\")\nexcept KeyError:\n    print(\"API 密钥未配置！\")\n    api_key = None\n\n# 或使用默认值避免异常\napi_key = config.get(\"api.secret_key\", None)\nif not api_key:\n    print(\"警告: API 密钥未配置！\")\n```\n\n### 热重载\n\n```python\nfrom pcf_config import Config\n\nconfig = Config(\"config.yaml\")\n\n# 初始加载\nprint(config.get(\"app.name\"))\n\n# 外部修改 config.yaml 文件...\n\n# 重新加载配置\nconfig.reload()\nprint(config.get(\"app.name\"))  # 更新后的值\n```\n\n### 读写操作\n\n```python\nfrom pcf_config import Config\n\nconfig = Config(\"config.yaml\")\n\n# 读取现有值\ndb_host = config.get(\"database.host\")\n\n# 设置新值\nconfig.set(\"database.timeout\", 30)\nconfig.set(\"api.rate_limit\", 1000)\n\n# 保存更改到文件\nconfig.save()\n\n# 验证更改\nprint(config.get(\"database.timeout\"))  # 30\nprint(config.get(\"api.rate_limit\"))    # 1000\n```\n\n### 复杂配置\n\n```yaml\n# config.yaml\napp:\n  name: \"我的应用程序\"\n  version: \"2.1.0\"\n  \ndatabase:\n  primary:\n    host: \"db1.example.com\"\n    port: 5432\n    credentials:\n      username: \"admin\"\n      password: \"secret\"\n  \n  replica:\n    host: \"db2.example.com\"\n    port: 5432\n    \nservices:\n  - name: \"auth\"\n    url: \"https://auth.example.com\"\n  - name: \"payment\"\n    url: \"https://payment.example.com\"\n```\n\n```python\nfrom pcf_config import Config\n\nconfig = Config(\"config.yaml\")\n\n# 访问嵌套配置\nprimary_db = config.get(\"database.primary.host\")\nusername = config.get(\"database.primary.credentials.username\")\n\n# 访问数组元素\nservices = config.get(\"services\")\nauth_service = services[0][\"url\"]  # \"https://auth.example.com\"\n```\n\n## 开发\n\n### 设置开发环境\n\n```bash\n# 克隆仓库\ngit clone https://github.com/pengcunfu/pcf-config.git\ncd pcf-config\n\n# 以开发模式安装\npip install -e .[dev]\n\n# 运行测试\npytest\n\n# 运行代码检查\nblack .\nflake8\nmypy pcf_config\n```\n\n### 运行测试\n\n```bash\n# 运行所有测试\npytest\n\n# 运行覆盖率测试\npytest --cov=pcf_config --cov-report=html\n\n# 运行特定测试文件\npytest tests/test_config.py\n```\n\n## 贡献\n\n欢迎贡献！请随时提交 Pull Request。对于重大更改，请先打开 issue 讨论您想要更改的内容。\n\n1. Fork 仓库\n2. 创建您的功能分支 (`git checkout -b feature/amazing-feature`)\n3. 提交您的更改 (`git commit -m 'Add some amazing feature'`)\n4. 推送到分支 (`git push origin feature/amazing-feature`)\n5. 打开 Pull Request\n\n## 许可证\n\n本项目采用 Apache 2.0 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。\n\n## 更新日志\n\n### v1.1.0\n- **重大变更**：移除单例模式 - Config 现在需要明确的文件路径\n- **重大变更**：移除便利函数 `get_config` 和 `get_config_with_default`\n- **新增**：添加 `set()` 方法用于修改配置值\n- **新增**：添加 `save()` 方法用于将更改持久化到文件\n- **改进**：`get()` 方法现在支持使用 `...` 哨兵值的默认值\n- **改进**：`reload()` 方法不再需要文件路径参数\n- **移除**：loguru 依赖 - 现在仅使用标准日志\n- **移除**：源码中的所有中文字符\n- **改进**：更好的错误处理和类型提示\n\n### v1.0.0\n- 初始发布\n- 从 YAML 文件加载基本配置\n- 支持点号分隔的嵌套键\n- 默认值支持\n- 热重载功能\n- 单例模式实现\n- 可选的 loguru 集成\n\n## 支持\n\n如果您遇到任何问题或有疑问，请在 GitHub 上[打开 issue](https://github.com/pengcunfu/pcf-config/issues)。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpengcunfu%2Fpcf-config","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpengcunfu%2Fpcf-config","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpengcunfu%2Fpcf-config/lists"}