Skip to main content

ConfigStore

A Python module for handling config files. It helps handles persist config files and also giving the ability to set, get, update and delete config settings

Easily load and persist config without having to think about where and how

It's built base on nodejs configstore

Config is stored in a JSON file located in $XDG_CONFIG_HOME or ~/.localconfig.
Example: ~/.localconfig/configstore/name.json

Installation

pip install pyconfigstore

Usage

from pyconfigstore import ConfigStore

# create a Configstore instance with a unique name e.g. gnit
# Package name and optionally some default values
conf = ConfigStore("Gnit", {"foo": 'bar'})

print(conf.get('foo'))
#>>> 'bar'

conf.set('awesome', True)
print(conf.get('awesome'))
#>>> True

# Use dot-notation to set nested properties
conf.set('bar.baz', True)
print(conf.get('bar'))
#>>> {"baz": True}

# escape dot-notation to set nested properties
conf.set('bar.baz\\.bag', True)
print(conf.get('bar'))
#>>> {"baz.bag": True}

conf.delete('awesome')
print(conf.get('awesome'))
#>>>

API

Configstore(packageName, [defaults], globalConfigPath)

Returns a new instance.

packageName

Type: str

Name of your package.

defaults

Type: dicts

Default config.

globalConfigPath

Type: bool
Default: False

Store the config at $CONFIG/package-name/config.json instead of the default $CONFIG/configstore/package-name.json. This is not recommended as you might end up conflicting with other tools, rendering the "without having to think" idea moot.

Features

You can use dot-notation to set, get, update and delete nested dict properties

.set(key, value)

Set an item.

.set(dict)

Set multiple items at once.

.get(key)

Get an item.

.has(key)

Check if an item exists.

.delete(key)

Delete an item.

.clear()

Delete all items.

.all()

Get all the config as a dict or replace the current config with an object:

conf.all({
	hello: 'world'
}) 

.size

Get the item count.

.path

Get the path to the config file. Can be used to show the user where the config file is located or even better open it for them.

Contribute

Yes, you can contribute. Just dm on twitter:@OyetokeT

TODO

There are couple of things I still need to add

  1. Dot-notation: Currently, you can only set configs using this feature. (get, delete)

  2. Stream: I planned to add a param that'll indicate that you want it to hit the file for every operation. Well that's how it works currently though. But to make it smarter, we don't need to hit the file for (size, get, has, all) operation. We are going to call the .all() once to get the configs in dicts and do the operation just using dict properties.

and more...

License

Copyright - 2018

Oyetoke Toby twitter:@OyetokeT

MIT LICENSE

Metadata

Release files for pyconfigstore3 1.0.1

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

Source distribution (sdist)

Source distribution for pyconfigstore3 1.0.1
File Size Uploaded
pyconfigstore3-1.0.1.tar.gz 4.9 kB Details

Release files / pyconfigstore3-1.0.1.tar.gz

Download URL pyconfigstore3-1.0.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
92763a4dc4d2b9521aa13c0a80cbdd0e87c5c00b1c65b96f8adb90e622b70046
BLAKE2b-256 checksum
How to use checksums
ce51513a839a4359821f8602e303697e44b940c6003f31291d0eeafe3b277eea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.8.0 tqdm/4.46.0 CPython/3.7.7

Release history Release notifications | RSS feed

This release

1.0.1 This release

1 release file

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