ConfigBase
一个简洁的 Python 配置数据管理框架,支持加密存储、属性描述符、嵌套配置段等功能。
安装
pip install conf_base
快速开始
from conf_base import ConfigMeta, Section, crypto
"""
基础的修改 读取 重置功能测试
"""
class TestConfig(metaclass=ConfigMeta):
_crypto = crypto.PlainJson('mysql_settings.json')
mysql = Section('Mysql')
host = mysql.add('host', '127.0.0.1', read_only=False)
port = mysql.add('port', 3306, validator=lambda x: x > 0)
user = mysql.add('user', 'root')
password = mysql.add('password', '<PASSWORD>')
print('配置块信息')
print(TestConfig.mysql)
print(TestConfig.mysql.section_tuple) # ('Mysql',)
print(TestConfig.mysql.get_options()) # ['host', 'port', 'user', 'password']
print('\n配置项信息读取')
print(TestConfig.host) # <class 'Mysql:host'>
print(TestConfig.host.section) # Mysql
print(TestConfig.host.option) # host
print(TestConfig.host.default) # 127.0.0.1
print(TestConfig.host.value) # 33.33.33.101
print('\n配置项信息修改')
TestConfig.host.value = '11.11.11.101'
print(TestConfig.host.value, TestConfig.mysql.host, TestConfig.mysql['host']) # 11.11.11.101
assert (TestConfig.host.value == '11.11.11.101' and
TestConfig.mysql.host == '11.11.11.101' and
TestConfig.mysql['host'] == '11.11.11.101')
TestConfig.host.set_value('33.33.33.101')
print(TestConfig.host.value) # 33.33.33.101
assert TestConfig.host.value == '33.33.33.101'
print('\n批量修改')
TestConfig.mysql.update(
host='123.456.789.10',
port=3333,
user='root2',
password='<PASSWORD2>',
)
print(TestConfig.mysql) # 输出该配置块的修改后的全部数据
assert str(TestConfig.mysql) == '<Section: Mysql={host=123.456.789.10, port=3333, user=root2, password=<PASSWORD2>}>'
print(TestConfig.mysql.default_options) # 获取默认配置
# {'host': '127.0.0.1', 'port': 3306, 'user': 'root', 'password': '<PASSWORD>'}
print('\n批量读取')
print(TestConfig.mysql.current_options)
assert (TestConfig.mysql.current_options ==
{'host': '123.456.789.10',
'port': 3333,
'user': 'root2',
'password': '<PASSWORD2>'}
)
print('\n全部重置')
TestConfig.mysql.reset()
print(TestConfig.mysql)
assert str(TestConfig.mysql) == '<Section: Mysql={host=127.0.0.1, port=3306, user=root, password=<PASSWORD>}>'
if 'host' not in TestConfig.mysql:
raise Exception('host not in TestConfig.mysql')
if 'xxx' in TestConfig.mysql:
raise Exception('xxx in TestConfig.mysql')
print('\n全部数据读取')
print(TestConfig.to_dict())
assert TestConfig.to_dict() == {
'Mysql': {
'host': '127.0.0.1',
'port': 3306,
'user': 'root',
'password': '<PASSWORD>'
}
}
``````python
import conf_base.Error
from conf_base import ConfigMeta, Section, crypto
"""
测试配置项的设置 校验 切换功能 类型校验
"""
class TestConfig(metaclass=ConfigMeta):
_crypto = crypto.PlainJson('settings_test.json')
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, read_only=True)
# 关卡设置
level = Section('Level', 'Level2')
complete_levels = level.add(
option='中文', # 配置项名称,在配置文件中显示
default=[1, 2, 3],
)
try:
TestConfig.screen_size.value = -1
except ValueError as e:
print('数据校验函数 验证通过')
# ValueError: Setting.screen_size 配置项校验失败,值 -1 未通过 validator 校验
try:
TestConfig.screen_size.value = 'a'
except TypeError as e:
print('配置项期望类型 验证通过')
# TypeError: Setting.screen_size 配置项期望类型 int 实际为 str
try:
TestConfig.bgm_volume.value = 2
except conf_base.Error.WriteError as e:
print('只读功能 验证通过')
# 2.使用不同的配置项和数据修改
TestConfig.level.set_use_section('Level2')
TestConfig.complete_levels.value = [4, 5, 6]
assert TestConfig.complete_levels.value == [4, 5, 6]
TestConfig.level.set_use_section('Level')
TestConfig.complete_levels.value = [7, 8, 9]
assert TestConfig.complete_levels.value == [7, 8, 9]
TestConfig.level.set_use_section('Level2')
TestConfig.complete_levels[1] = 3
assert TestConfig.complete_levels.value == [4, 3, 6]
print('配置项使用 正常')
# 重置数据
TestConfig.setting.reset()
# 关卡数据重置
print(TestConfig.setting)
print(TestConfig.level)
TestConfig.level.reset()
print(TestConfig.level)
# <Section:Level [中文=[1, 2, 3]], Level2 [中文=[1, 2, 3]]>
print(TestConfig.level.current_options)
print('\n全部数据输出')
print(TestConfig.to_dict())
print('\n修改调用')
def func1(section, option, value):
# 当数据修改时,自动调用该函数
print(f'func1调用 section:{section}, option:{option}, value:{value}')
TestConfig.level.set_use_section('Level2')
# 普通写法,通过on_change方法进行绑定函数 section='Level2' 可选只监听的配置块,数据修改时调用func1
TestConfig.complete_levels.on_change(func1, section='Level2')
TestConfig.complete_levels.value = [1]
@TestConfig.complete_levels.on_change(section='Level') # 装饰器写法,section='Level' 可选只监听的配置块
def func2(section, option, value):
# 当数据修改时,自动调用该函数
print(f'func2调用 section:{section}, option:{option}, value:{value}')
# 切换 Level 然后修改,调用func2函数
TestConfig.level.set_use_section('Level')
TestConfig.complete_levels.value = [1]
Metadata
Release files for conf-base 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| conf_base-0.0.6.tar.gz | 13.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| conf_base-0.0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.9 kB
Release files / conf_base-0.0.6.tar.gz
| Download URL | conf_base-0.0.6.tar.gz |
|---|---|
| Size | 13.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
25dc2884a99b5b9c93a49d98d06c5fbd618ee55b7329bd78dbf2fa48decf5047
|
|
BLAKE2b-256 checksum How to use checksums |
fcd93d986a6e9d586398895f2fee74ab7c0b5198629825fc9f18366a07bb753a
|
| 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.6-py3-none-any.whl
| Download URL | conf_base-0.0.6-py3-none-any.whl |
|---|---|
| Size | 16.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4368868431e88e670bbeb7cea5839eb93216f9974d6c01023f6079cd092c2c64
|
|
BLAKE2b-256 checksum How to use checksums |
f29cf6d9e60c08d05c877058b2525c25f969f54583b94a781c4bd06dff96688d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.9
|
Security for conf-base 0.0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.