Skip to main content

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. The names of both files are configurable. The above names are the defaults.

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 pattern of using __file__ to find config files in the same directory as the program.

config = confjson.Config(__file__)
config = confjson.Config(".")
config = confjson.Config(
	"./configs",
	user_config_filename=f"{os.getenv('username')}.cfg",
	default_config_filename="global.cfg",
)

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.3.0

  • Made filenames of both user.config.json and default.config.json configurable.
  • Added use_placeholders argument to Config class. When True, doesn't raise KeyError on attempted access to a nonexistent key, instead returning a placeholder. Assignments of keys to a placeholder will propagate up through the hierarchy and add real dicts as needed.

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.

Metadata

Release files for confjson 1.3.0

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

Source distribution (sdist)

Source distribution for confjson 1.3.0
File Size Uploaded Provenance Actions
confjson-1.3.0.tar.gz 9.2 kB Without provenance Details for confjson-1.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for confjson 1.3.0
File Interpreter ABI Platform Actions
confjson-1.3.0-py3-none-any.whl Python 3 none any Details for confjson-1.3.0-py3-none-any.whl

Total release size: 22.4 kB

Release files / confjson-1.3.0.tar.gz

Download URL confjson-1.3.0.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
346a79bb2356716f0fab8e2615af8e678c6b98895bd5e9c2206ee064fa6ae833
BLAKE2b-256 checksum
How to use checksums
38f6328681e86b8a4dad302da4d05136c51abec91965915783e0f610c0a6c8d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.20.0

Release files / confjson-1.3.0-py3-none-any.whl

Download URL confjson-1.3.0-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
55335ce7cf61bb2ae7b28a5d5c4d276b4ddb24fc3845a014a11f62b92f8de00e
BLAKE2b-256 checksum
How to use checksums
9b39194dcc3157f9218b59cc3f7d48b7aacb1679cdaa210b1d01a579ad977ac9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.20.0

Security for confjson 1.3.0

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