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.2
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.2.tar.gz | 9.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| conf_base-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.5 kB
Release files / conf_base-0.0.2.tar.gz
| Download URL | conf_base-0.0.2.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c2b97dc8c2bcf5b9add76912c6972065ba0c48a3a40f298d877a4c88e475c8b6
|
|
BLAKE2b-256 checksum How to use checksums |
2374786f1a07829626f19f613f0fffabdcd70ffc3dccc65717da8af2880df75f
|
| 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.2-py3-none-any.whl
| Download URL | conf_base-0.0.2-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
37c4f941f6a1b1bf860ae3b8c0321b52ca996abed55596b53db9777c4506c69e
|
|
BLAKE2b-256 checksum How to use checksums |
3d46c0a940ae37e2118924910ba1ebc1133b8d8a61da961f97e7c59fc84ff883
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.9
|