A Tree Struct Configuration module for python, support serialization to/from JSON and TOML
Project description
A Tree Struct Configuration module for python, support serialization to/from JSON and TOML.
Install
pip install -U TreeStructConfig
Usage
Define class and create object
1from tree_struct_config import (
2IntLeaf,
3StringLeaf,
4BooleanLeaf,
5ListLeaf,
67
BranchNode,
89
RootNode,
10SerializationFormat,
11SerializationDecodeError,
12)
1314
15
class Config(RootNode):
16version = StringLeaf('0.1.0')
1718
class Auth(BranchNode):
19username = StringLeaf('rex')
20password = StringLeaf('password')
2122
class Wireless(BranchNode):
23class AP(BranchNode):
24enabled = BooleanLeaf(True)
25channel = IntLeaf(1)
26password = StringLeaf('password')
27mac_acl_list = ListLeaf([
28'00:00:00:00:00:00',
29])
3031
32
config = Config()
Access config value
>>> config.Auth.username rex >>> username = config.Auth.username >>> username rex
Update config value
>>> config.Auth.username = 'new_user' >>> config.Auth.username new_user >>> config.Wireless.AP.password = 'new_password' >>> config.Wireless.AP.password new_password
Dump config to JSON string
>>> config.dumps() { "Auth": { "password": "password", "username": "rex" }, "Wireless": { "AP": { "channel": 1, "enabled": true, "mac_acl_list": [ "00:00:00:00:00:00" ], "password": "new_password" } }, "version": "0.1.0" }
Load config from JSON string
>>> json_str = """ ... { ... "Auth": { ... "username": "new_user" ... }, ... "Wireless": { ... "AP": { ... "channel": 1, ... "enabled": true, ... "password": "new_password", ... } ... } ... } ... """ ... >>> config.Auth.username rex >>> config.loads(json_str) >>> config.Auth.username new_user
Dump config to JSON file
>>> with open('config.json', 'w') as f: ... config.dump(f)
Load config from JSON file
>>> with open('config.json') as f: ... config.load(f)
Dump to TOML and load from TOML string and file
>>> config.dumps(serialization_format=SerializationFormat.TOML) >>> config.loads(s, serialization_format=SerializationFormat.TOML)>>> with open('config.toml', 'w') as f: ... config.dump(f, serialization_format=SerializationFormat.TOML) >>> with open('config.toml') as f: ... config.load(f, serialization_format=SerializationFormat.TOML)
config.toml
version = "0.1.0"
[Auth]
password = "password"
username = "rex"
[Wireless.AP]
channel = 1
enabled = true
mac_acl_list = [ "00:00:00:00:00:00",]
password = "password"
Override dump() and load() function
1class AdvancedConfig(Config):
2"""override dump/load function"""
3_filename = None
45
def dump(self, fp=None, serialization_format=None):
6with open(self._filename, 'w') as fp:
7super().dump(fp, serialization_format)
89
return
1011
def load(self, fp=None, serialization_format=None):
12with open(self._filename) as fp:
13try:
14super().load(fp, serialization_format)
1516
except SerializationDecodeError:
17pass
1819
20
advanced_config = AdvancedConfig()
21advanced_config._filename = 'config.json'
22advanced_config.dump()
23advanced_config.load()
Demo
Demo source code: demo.py
Output
----------
password
new_password
----------
{
"Auth": {
"password": "password",
"username": "rex"
},
"Wireless": {
"AP": {
"channel": 1,
"enabled": true,
"mac_acl_list": [
"00:00:00:00:00:00"
],
"password": "new_password"
}
},
"version": "0.1.0"
}
----------
xxxxxxxx
Alternative
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
TreeStructConfig-0.2.0.tar.gz
(5.0 kB
view hashes)
Built Distribution
Close
Hashes for TreeStructConfig-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ccb5ae80a4630c4b6504dcb885bb76fa43cf40a96ad25a71ca6bd65adb17f43 |
|
MD5 | e52aced39992f2dfc49bd891af72fbf4 |
|
BLAKE2b-256 | cd0b891cae50a3fb4a7636b3d9218e32e0b7d1e1df50087652595e94822b17b9 |