This project has been archived by its maintainers, and is no longer receiving any updates.
TreeStructConfig
A Tree Struct Configuration module for python, support serialization to/from JSON and TOML.
Install
Serialization with JSON
pip install -U TreeStructConfig
Serialization with TOML
pip install -U TreeStructConfig[toml]
Usage
Define class and create object
from tree_struct_config import (
IntLeaf,
StringLeaf,
BooleanLeaf,
ListLeaf,
BranchNode,
RootNode,
SerializationFormat,
SerializationDecodeError,
)
class Config(RootNode):
version = StringLeaf('0.1.0')
class Auth(BranchNode):
username = StringLeaf('rex')
password = StringLeaf('password')
class Wireless(BranchNode):
class AP(BranchNode):
enabled = BooleanLeaf(True)
channel = IntLeaf(1)
password = StringLeaf('password')
mac_acl_list = ListLeaf([
'00:00:00:00:00:00',
])
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
class AdvancedConfig(Config):
"""override dump/load function"""
_filename = None
def dump(self, fp=None, serialization_format=None):
with open(self._filename, 'w') as fp:
super().dump(fp, serialization_format)
return
def load(self, fp=None, serialization_format=None):
with open(self._filename) as fp:
try:
super().load(fp, serialization_format)
except SerializationDecodeError:
pass
advanced_config = AdvancedConfig()
advanced_config._filename = 'config.json'
advanced_config.dump()
advanced_config.load()
Demo
Demo source code: https://github.com/rexzhang/tree-struct-config/blob/master/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
Changelog
0.2.3
Add new implement, depend typing hint(draft)
0.2.2
Fix not exist branch crash
0.2.1
Fix bug
0.2.0
Support TOML format file
0.1.0
First release
Alternative
Metadata
Release files for TreeStructConfig 0.2.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 | Provenance | Actions |
|---|---|---|---|---|
| TreeStructConfig-0.2.3.tar.gz | 6.6 kB | Without provenance | Details for TreeStructConfig-0.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| TreeStructConfig-0.2.3-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for TreeStructConfig-0.2.3-py2.py3-none-any.whl |
Total release size: 12.6 kB
Release files / TreeStructConfig-0.2.3.tar.gz
| Download URL | TreeStructConfig-0.2.3.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7bd79af75d96f16844b81ea8fddb8fae3c751d1a382cef84be93857b255a9c3b
|
|
BLAKE2b-256 checksum How to use checksums |
0555286b2c470b99a0f23e7990a0e7360a05e65cf5420676b9965ac1e76a9769
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0
|
Release files / TreeStructConfig-0.2.3-py2.py3-none-any.whl
| Download URL | TreeStructConfig-0.2.3-py2.py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
c258f8dca9e48c3c665ca7bcf5bc44e53551737621f78dc6c6c453bc1d65f00d
|
|
BLAKE2b-256 checksum How to use checksums |
9a65152bc2a7cb2c34fea0dc42d2142751395247a89db1659186764f4b5fb530
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0
|
Security for TreeStructConfig 0.2.3
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.