ConfigBase
一个简洁的 Python 配置管理框架,支持加密存储、属性描述符、嵌套配置段等功能。
特性
- 单例模式:全局唯一的配置实例
- 加密支持:支持明文和 XOR 加密配置内容
- 描述符管理:通过 Python 描述符优雅访问配置
- 嵌套配置:支持多层嵌套的配置段
- 类型自动转换:自动处理配置值的类型
- 脏标记机制:自动检测配置变更,批量写入
架构
configbase/
├── crypto.py # 加密模块(PlainCrypto、XorCrypto)
├── descriptor.py # 配置属性描述符
├── json_config.py # 核心配置管理(单例)
├── metaclass.py # 配置元类
├── section.py # 配置段管理
└── singleton.py # 单例元类
安装
pip install conf_base
快速开始
1. 定义配置类
from conf_base import ConfigMeta, Section, crypto
class TestConfig(metaclass=ConfigMeta):
# _crypto = crypto.PlainCrypto() # 默认使用明文json格式
# _crypto = crypto.XorCrypto()
# 全局设置
setting = Section('Setting')
screen_size = setting.add(option='screen_size', default=3)
full_screen = setting.add(option='full_screen', default=False)
bgm_volume = setting.add(option='bgm_volume', default=1.0)
sound_volume = setting.add(option='sound_volume', default=1.0)
# 关卡设置
level = Section('Level', 'Level2')
# fix_type 默认为True 数据修改类型必须保持一致
complete_levels = level.add(option='中文', default=[1, 2, 3], fix_type=False)
play_selection_card = level.add(
option='play_selection_card',
default=['Peashooter', 'SunFlower']
)
print('section: ', TestConfig.screen_size.section)
print('option: ', TestConfig.screen_size.option)
print('value: ', TestConfig.screen_size.value)
print('default: ', TestConfig.screen_size.default)
print('-' * 20)
# 使用不同的配置项和数据修改
TestConfig.level.set_use_section('Level2')
TestConfig.complete_levels.value = {1: 1}
print(TestConfig.complete_levels.value)
TestConfig.level.set_use_section('Level')
TestConfig.complete_levels.value = {2: 2}
print(TestConfig.complete_levels.value)
TestConfig.level.set_use_section('Level2')
TestConfig.complete_levels.value = {3: 3}
print(TestConfig.complete_levels.value)
# 重置数据
TestConfig.complete_levels.reset()
print(TestConfig.complete_levels.value)
# 如何快速进行读写操作
Metadata
Release files for conf-base 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| conf_base-0.0.3.tar.gz | 9.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| conf_base-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.0 kB
Release files / conf_base-0.0.3.tar.gz
| Download URL | conf_base-0.0.3.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6afbfc185ddcebab7444a97af12dbec1817c64cd297e06ce1cd58339a612a6c9
|
|
BLAKE2b-256 checksum How to use checksums |
3e640d031531587e986e03021a53732187e38aad599741028d328742d89b30a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.9
|
Release files / conf_base-0.0.3-py3-none-any.whl
| Download URL | conf_base-0.0.3-py3-none-any.whl |
|---|---|
| Size | 9.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
08d05bb8fac7f09e611e29cd84d66794f543c08b71ce5a5be63ac5badd2edc8a
|
|
BLAKE2b-256 checksum How to use checksums |
f0fced96b4fa4e0438e4594e076ee0ca724c743c7f46fef01111d4abe6c9c3c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.9
|