Metadata-Version: 2.4
Name: conf_base
Version: 0.0.4
Summary: 一个简洁的 Python 配置管理框架，支持加密存储、属性描述符、嵌套配置段等功能。
Home-page: https://gitee.com/kuangjianke/conf_base
Author: 加点盐
Author-email: 1431705288@qq.com
License: MIT
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: license
Dynamic: requires-python
Dynamic: summary

# ConfigBase

一个简洁的 Python 配置数据管理框架，支持加密存储、属性描述符、嵌套配置段等功能。


## 安装

```bash
pip install conf_base
```

## 快速开始
```python
from conf_base import ConfigMeta, Section, crypto

"""
测试使用示例
"""


class TestConfig(metaclass=ConfigMeta):
    # _crypto = crypto.PlainCrypto() # 默认使用
    # _crypto = crypto.XorCrypto()

    # 全局设置
    setting = Section('Setting')
    screen_size = setting.add(
        option='screen_size',
        default=3,
        validator=lambda x: x > 0  # 数据修改校验函数
    )
    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')

    complete_levels = level.add(
        option='中文',  # 配置项名称,在配置文件中显示
        default=[1, 2, 3],
        fix_type=False  # fix_type 默认为True 数据修改类型必须保持一致
    )

    play_selection_card = level.add(
        option='play_selection_card',
        default=['Peashooter', 'SunFlower']
    )


# 1.基础信息使用
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)

# 2.使用不同的配置项和数据修改
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)

# 3.重置数据
TestConfig.complete_levels.reset()
print(TestConfig.complete_levels.value)


# 4.数据校验函数
# TestConfig.screen_size.value = -1
# ValueError: Setting.screen_size 配置项校验失败，值 -1 未通过 validator 校验
# TestConfig.screen_size.value = 'a'
# TypeError: Setting.screen_size 配置项期望类型 int 实际为 str， 否则请修改配置项 fix_type=False


# 5.不同的配置类存储到不同的配置文件
class TestConfig2(metaclass=ConfigMeta):
    _crypto = crypto.PlainCrypto('settings_2.json')  # 不同的类配置项使用不同的名称，防止文件名称重叠造成数据覆盖

    setting = Section('Setting')
    bgm_volume = setting.add(option='bgm_volume', default=1.0)

```
