Skip to main content

Tree Struct Config - A Tree Struct Configuration module for python, support dump from/to JSON

Project description

https://img.shields.io/pypi/v/TreeStructConfig.svg https://img.shields.io/pypi/pyversions/TreeStructConfig.svg https://img.shields.io/pypi/dm/TreeStructConfig.svg

A Tree Struct Configuration module for python, support dump from/to JSON/TOML(todo).

Install

pip install TreeStructConfig

Usage

from tree_view_config import RootNode, BranchNode, StringLeaf, IntLeaf, BooleanLeaf


class Config(RootNode):
    username = StringLeaf('admin')
    password = StringLeaf('password')

    class Auth(BranchNode):
        username = StringLeaf('admin')
        password = StringLeaf('password')

    class Wireless(BranchNode):
        class AP(BranchNode):
            enabled = BooleanLeaf(True)

            interface = StringLeaf('uap0')  # DON'T CHANGE IT
            ssid = StringLeaf('PiRouter')
            password = StringLeaf('password')
            hw_mode = StringLeaf('n')
            channel = IntLeaf(1)

        class Client(BranchNode):
            enabled = BooleanLeaf(False)

            ssid = StringLeaf('ssid')
            password = StringLeaf('password')


data = '''
{
  "Auth": {
    "password": "xxxxxxxx",
    "username": "admin"
  },
  "Wireless": {
    "AP": {
      "channel": 1,
      "enabled": true,
      "hw_mode": "n",
      "interface": "uap0",
      "password": "xxxxxxxx",
      "ssid": "PiRouter"
    },
    "Client": {
      "enabled": false,
      "password": "xxxxxxxx",
      "ssid": "ssid"
    }
  }
}

'''

config = Config()
print(config.dumps())

print('--------')
config.loads(data)
print(config.Wireless.AP.password)
{
  "Auth": {
    "password": "password",
    "username": "admin"
  },
  "Wireless": {
    "AP": {
      "channel": 1,
      "enabled": true,
      "hw_mode": "n",
      "interface": "uap0",
      "password": "password",
      "ssid": "PiRouter"
    },
    "Client": {
      "enabled": false,
      "password": "password",
      "ssid": "ssid"
    }
  },
  "password": "password",
  "username": "admin"
}
--------
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.1.0.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

TreeStructConfig-0.1.0-py2.py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page