Easy access to `~/.config`
Project description
XDGConfig
Easy access to ~/.config
.
Usage
from xdgconfig import JsonConfig
# Instanciate the JsonConfig object
# If you'd rather use a different format, there also are config classes
# for TOML, YAML, INI (configparser), and XML.
# This will save your configuration under `~/.config/PROG/config
config = JsonConfig('PROG', autosave=True)
config['foo'] = 'bar' # Save a value to the config
# Access the value later on
print(config['foo'])
# It behaves like a collections.defaultdict as well
config['oof']['bar'] = 'baz'
# Prints {'oof': {'bar': 'baz'}, 'foo': 'bar'}
print(config)
Custom serializers
You can add custom serializers support by using a Mixin class, as well as
a serializer class which must have a dumps
and a loads
method, which will
be used to store and load data from the config file. The data is always
represented as a python dict
object, but you can serialize any data you want
inside of it.
Look at the following example for an implementation guide.
from typing import Any, Dict
from xdgconfig import Config
class MySerializer:
def dumps(data: Dict[str, Any]) -> str:
return '\n'.join(f'{k}:{v}' for k, v in data.items())
def loads(contents: str) -> Dict[str, Any]:
return dict(s.split(':') for s in contents.split('\n'))
class MySerializerMixin:
SERIALIZER = MySerializer
class MyConfig(MySerializerMixin, Config):
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
xdgconfig-0.0.2.tar.gz
(8.2 kB
view hashes)
Built Distribution
xdgconfig-0.0.2-py3-none-any.whl
(12.8 kB
view hashes)
Close
Hashes for xdgconfig-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4bf21ede8cfacf6f8e0b2dfcb8002e6cfd701e27214436d83e8a081d5602b0e |
|
MD5 | fea45e5b44b4f1289aa16496ae55b760 |
|
BLAKE2b-256 | 958f24d0e8776ba1d6092ce25eef812b202c696aed5451c5c38760be7d5e872f |