Skip to main content

Reads and write json files as a configuration file, supports nested json values.

Project description

yet-another-json-config

Python class to reads and write json files as a configuration file, supports nested json values.

Examples

Initializing Config Class

from yet_another_json_config import Config

c = Config('./tests/test.json')

Listing Settings

from yet_another_json_config import Config

c = Config('./tests/test.json')

print(c.settings)

Output

{
    "test": "test",
    "varUserTest": "test_$user$",
    "nestedTest": {
        "test": "test"
    },
    "nestedTest2": {
        "nested": {
            "test": "test"
        }
    },
    "get": "test"
}

Get Setting

Settings can be obtained two ways through the method get

Class Method get

# setting a basic setting
c.get('test')

# setting a nested setting via list of strings
c.get('nestedTest2', 'nested', 'test')

# setting a nested setting via tuple
c.get(('nestedTest2', 'nested', 'test'))

key Method

# setting a basic setting
print(c['test'])

# setting a nested setting
print(c['nestedTest2']['nested']['test'])

Set Setting

Settings can be both created and modified using the same methods. If a setting does not exist and one of the following methods is used, it will be created. If the setting already exists, it will be updated.

Class Method set

This method supports *args for defining the keys for a setting. This means that a list of strings (not type of list) or a tuple can be passed to set the value.

# setting a basic setting
c.set('test', value='test2')

# setting a nested setting via list of strings
c.set('nestedTest2', 'nested', 'test', value='test2')

# setting a nested setting via tuple
c.set(('nestedTest2', 'nested', 'test'), value='test2')

key Method

# setting a basic setting
c['test'] = 'test2'

# setting a nested setting
c['nestedTest2']['nested']['test'] = 'test2'

Delete Setting

Settings can be deleted two ways through the method delete and the del statement. As well, nested settings can also be deleted. Below are an example of each.

Class Method delete

# deleting a basic setting
c.delete('test')

# deleting a nested setting
c.delete(('nestedTest2', 'nested', 'test'))

del Statement Method

# deleting a basic setting
del c['test']

# deleting a nested setting
del c['nestedTest2']['nested']['test']

Custom Config Class

Below is an example of a custom config class that is derived off the Config class. In this example it allows for the variable $user$ to be replaced at run time with the user id that is currently running the code. This could be expanded further as well as potential validation of the configuration file after loading via the validate method.

import getpass
from yet_another_json_config import Config

class CustomConfig(Config):
    def _load(self):
        super()._load()

        user = getpass.getuser()

        if 'varUserTest' in self._settings:
            # replace special character in filename with the username
            self.set('varUserTest', value=self.get('varUserTest').replace('$user$', user))

        print(self._settings)

        self.validate()

    def validate(self):
        pass


conf = CustomConfig('./tests/test.json')

print(conf.settings())

API Reference

class Config(config_file_path, file_must_exist=False)

Create an instance of a configuration file.

Arguments

  • config_file_path (str) - The path to the configuration file.
  • file_must_exist (bool, Optional) - Raises a FileNotFoundError exception if file does not exist. Default value is False.
get(*keys: str)

Returns the value of the keys specified.

  • *keys - (str) - List of str or Tuple of str traversing the settings and return a value if the setting exists. If the setting does not exist, a KeyError exception is raised.
set(*keys: str, value)

Sets the keys to the value specified.

  • *keys - (str) - List of str or Tuple of str traversing the settings to set the specified key to the specified value. If the setting does not exist, the setting is created.
settings()

Returns all settings in the configuration.

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

yet_another_json_config-1.0.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

yet_another_json_config-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file yet_another_json_config-1.0.1.tar.gz.

File metadata

File hashes

Hashes for yet_another_json_config-1.0.1.tar.gz
Algorithm Hash digest
SHA256 cf3c4c906227604f09fd500a0b82bf9d5382272a681d174e1966357b17fe2726
MD5 a54177a7fe856e60b85e38671d21dcf9
BLAKE2b-256 a9af2d534affe03ca896d14ef6c35cdc4de357eb439f4826cfa51b3832e0bd2d

See more details on using hashes here.

File details

Details for the file yet_another_json_config-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for yet_another_json_config-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8cb2cb1bfee159685ea7f70c9806fdf2e0b77f5378b6be7e62d714a092795e5c
MD5 a52418b9c3cb6af3136d611160681633
BLAKE2b-256 b6f8bc82408b66e03e7fd370f6da1cd6092d793a90d749db09b0ba98da488d92

See more details on using hashes here.

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