Skip to main content

A bafflingly simple, JSON-backed user configuration manager.

Project description

confjson

Build Status

What is it?

A bafflingly simple, JSON-backed configuration manager for Python programs.

Why is it?

I kept implementing roughly the same thing from scratch in my various projects, so I figured it was time to do it properly.

User guide

The confjson.Config class is similar to a ChainMap, and works by means of two JSON files in the same directory:

  • default.config.json
    • The default config file is read-only as far as confjson is concerned. To add default settings, manually edit the file.
    • If the file does not exist, there are no defaults.
    • This file will typically be version controlled along with the code.
  • user.config.json
    • Any changes made to the config (and saved) at runtime end up in this file.
    • If the file does not exist, it will be created when changes are saved.
    • User settings take priority over default settings.
    • This file should probably be added to .gitignore or such.

Initialization

The path given when initializing the Config object can be either a directory or a file. If it refers to a file, confjson will look for config files in the containing directory. The reason for this is that it enables the following pattern, using __file__ to find config files in the same directory as the program.

config = confjson.Config(__file__)

Data access

Items in the confjson config are accessed as in a dict.

if "username" in config["user"]:
	do_something(config["user"]["username"])
	config["user"]["something_count"] += 1

It is also possible to access items as attributes, unless they share a name with a method of the Config class.

config.my_key = "my value"

Persistence

The load() method (re-)loads the Config object with values from the backing JSON files. Loading is also performed on initialization, so this is mainly for discarding changes.

config.load()

The save() method saves any changed or added items to user.config.json only.

config.save()

Version history

1.2.2

  • Added keys(), get(key, default) and __contains__(key) to ConfigItemProxy to fix a few problems caused by the previous change.

1.2.1

  • Improved attribute-style access and enabled mixed style access.

1.2.0

  • Enabled attribute-style access to config items.

1.1.0

  • Fixed handling of nested dicts.
  • Added check to prevent insertion of JSON-incompatible values right away rather than fail on save.
  • Added some more dict-like dunder methods.

1.0.1

  • Changed __init__ to also load the config files.

1.0.0

  • Initial release.

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

confjson-1.2.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

confjson-1.2.2-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file confjson-1.2.2.tar.gz.

File metadata

  • Download URL: confjson-1.2.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for confjson-1.2.2.tar.gz
Algorithm Hash digest
SHA256 48001e50db90145cd65f8e67151a358d416a0aebf7dae05d996547e7a57a2d6a
MD5 5cad780b11d8fe31ee9c0400918c9d48
BLAKE2b-256 62866866c6045457359c4df6244f4a6bef9d8c6840bd9688b6d77952eb556cca

See more details on using hashes here.

File details

Details for the file confjson-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: confjson-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for confjson-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c203c848542ff0899b5b780efd232e221e4d09559aed193c15e6fb7871b13654
MD5 eaf5eda24ad02ec90fe500489a52efa3
BLAKE2b-256 d09d3f020bb317dea5a627411e878209a9dc64b858fb6d6749b5815a057921bf

See more details on using hashes here.

Supported by

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