Skip to main content

ConfJ

Package is used to load config from json files. Path to config can lead either to directory with a bunch of json files, or to a single json file. Contents is parsed and stored into config object, allowing attribute-based access to different options.

Build Status Say Thanks!

Installation

Package can be installed with pip:

pip install confj

Usage

Usage of this utility is very simple: import Config class and load your config with load method. Path to configs might be set via environment variable JSON_CONFIG_PATH or passed directly to the load call:

from confj import Config


config = Config()
config.load('/path/to/config')

If path to config is a directory, each file will be available as an attribute of config object. Otherwise, if path is a file, config object will store parsed json from given file, allowing access to data via attributes.

Config can be loaded during initialization step, just set autoload parameter to True. Optionally path to load configs from might be passed during initialization.

from confj import Config


config = Config(default_config_path='/path/to/config', autoload=True)

After config is loaded, it's options are accessible via attributes of config object, ot dict-like syntax:

>>> config.username
'user'

>>> config['username']
'user'

>>> config.get('username')
'user'

Config search precedence

  1. If you directly call load method with config_path passed as parameter, then this path is used.
  2. If no config_path provided or autoload option was used, then default_config_path from initialization step is used.
  3. The last option is to set JSON_CONFIG_PATH environment value. If the search is failed on all three steps, then ConfigException is raised.

Load from python object

Config may be loaded from python object:

from confj import Config


config = Config()
data = {'username': 'user', 'password': 'pass'}


config.load_from_obj(data)

assert config.username == 'username'
assert config.password == 'pass'

Set/change config data

Config data may be changed or added to current config with set method:

from confj import Config


config = Config()
data = {'username': 'user', 'password': 'pass'}


config.load_from_obj(data)
config.set('connection', {'port': 5432, 'host': 'localhost'})

assert config.connetcion.port == 5432
assert config.connection.host == 'localhost'

Available methods

All config object's method names are starting with c_, to avoid possible clash with possible config options

  • c_format returns pretty formatted sting representing config;
  • c_pprint outputs formatted config to stdout;
  • c_validate validates config against given json schema. See Validation section below.

Validation

Config must be a valid json object, so we can validate it against provided json schema. To use config validation jsonschema package must be installed. It might be done as a separate step as

pip install jsonschema

or during confj installation time:

pip install confj[validation]

Then config validation can be done with c_validate method:

from confj import Config

config = Config(default_config_path='/path/to/config', autoload=True)
schema = {"type": "object"}
is_valid = config.c_validate(schema)

By default c_validate will return either True of False. To see actual validation error just pass do_raise=True as additional parameter, and catch ValidationError later:

from confj import Config
from jsonschema import ValidationError

config = Config(default_config_path='/path/to/config', autoload=True)
schema = {"type": "object"}

try:
    config.c_validate(schema, do_raise=True)
except ValidationError as err:
    print(f'Looks like config is invalid: {err}')

To read more on json validation one can at json-schema.org

Metadata

Release files for confj 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for confj 0.2.4
File Size Uploaded Provenance Actions
confj-0.2.4.tar.gz 5.4 kB Without provenance Details for confj-0.2.4.tar.gz

Release files / confj-0.2.4.tar.gz

Download URL confj-0.2.4.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
342711edcdab3cd13911c86d54d408c91f8ff18b4dbb00687192796e8e1cf518
BLAKE2b-256 checksum
How to use checksums
16fbc0dfac5bd5ad7a8dd96e52e63542e6025182f1d93bd9653b6f7651d574be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.1

Security for confj 0.2.4

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page