# etiniconf **Repository Path**: ssy2020/etiniconf ## Basic Information - **Project Name**: etiniconf - **Description**: 用来解析 ini 文件 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-12-20 - **Last Updated**: 2025-12-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: devops, Python, ini ## README ## 介绍 一个解析 ini 文件的库 ## 特性 1. API 简洁性: 属性 / 字典双模式访问 1.1 属性访问模式 1.2 字典访问模式 1.3 混合访问模式 1.4 支持 in 操作符 2. 异常处理 2.1 使用异常策略(默认) 2.2 使用结果策略 2.3 使用静默策略 ## 使用环境 1. 系统: Linux/Windows 2. 语言: Python 3.9+ ## 安装 ``` # 运行安装命令 pip install etiniconf ``` ## 使用样例 1. INI文件 config.ini ``` [server] host = 192.168.0.10 port = 22 ``` 2. 使用样例 ``` import etiniconf def main(filepath): """ 异常策略(默认): conf = etiniconf.ConfiniParserFactory.create_with_exception_strategy(filepath) 结果策略: conf = etiniconf.ConfiniParserFactory.create_with_result_strategy(filepath) 静默策略: conf = etiniconf.ConfiniParserFactory.create_with_silent_strategy(filepath) """ # 使用静默策略 conf = etiniconf.ConfiniParserFactory.create_with_silent_strategy(filepath) # 1. 属性访问模式 try: host = conf.server.host port = conf.server.port print(f"host={host}, port={port}") except AttributeError as e: print(f"Error: {e}") # 2. 字典访问模式 try: host = conf['server']['host'] port = conf['server']['port'] print(f"host={host}, port={port}") except etiniconf.ConfiniError as e: print(f"Error: {e}") # 3. 混合访问模式 try: host = conf.server['host'] port = conf['server'].port print(f"host={host}, port={port}") except (AttributeError, etiniconf.ConfiniError) as e: print(f"Error: {e}") # 4. 支持 in 操作符 section = 'server1' if section in conf: print(f"The {section} does exist") else: print(f"The {section} does not exist") if __name__ == '__main__': f = 'config.ini' main(f) ``` ## 测试 ``` # 运行所有测试 pytest tests/ -v # 生成覆盖率报告 pytest --cov=etiniconf --cov-report=html --cov-report=term tests/ -v ``` ## 测试报告 ==================== test session starts ==================== platform linux -- Python 3.9.15, pytest-8.4.2, pluggy-1.6.0 -- plugins: cov-7.0.0 collected 18 items tests/test_confini.py::test_confini_parser_with_exception_strategy PASSED [ 5%] tests/test_confini.py::test_confini_parser_with_result_strategy PASSED [ 11%] tests/test_confini.py::test_confini_parser_with_silent_strategy PASSED [ 16%] tests/test_confini.py::test_confini_parser_factory PASSED [ 22%] tests/test_confini.py::test_set_value_and_add_section PASSED [ 27%] tests/test_confini.py::test_remove_section PASSED [ 33%] tests/test_confini.py::test_get_sections PASSED [ 38%] tests/test_confini.py::test_get_options PASSED [ 44%] tests/test_confini.py::test_get_options_items PASSED [ 50%] tests/test_confini.py::test_contains_operator PASSED [ 55%] tests/test_confini.py::test_invalid_error_strategy PASSED [ 61%] tests/test_confini.py::test_section_proxy_access PASSED [ 66%] tests/test_confini.py::test_empty_config_file PASSED [ 72%] tests/test_confini.py::test_unknown_error_strategy PASSED [ 77%] tests/test_confini.py::test_section_proxy_edge_cases PASSED [ 83%] tests/test_confini.py::test_basic_class_functionality PASSED [ 88%] tests/test_confini.py::test_add_section_method PASSED [ 94%] tests/test_confini.py::test_get_options_with_nonexistent_section PASSED [100%] ==================== tests coverage ===================== ## 测试覆盖率报告 ____ coverage: platform linux, python 3.9.15-final-0 ___ | Name | Stmts | Miss | Cover | |-----------------------------|-------|------|-------| | etiniconf/__init__.py | 6 | 0 | 100% | | etiniconf/confini_parser.py | 109 | 1 | 99% | | TOTAL | 115 | 1 | 99% | Coverage HTML written to dir htmlcov ## 许可证 (License) 本项目采用 [MIT](LICENSE) 许可证。 This project is licensed under the [MIT](LICENSE) License.