Skip to main content

🍇 XDG standard config files 🍇

Project description

Simple, correct handling of config, data and cache files

Like everyone else, I wrote a lot of programs which saved config files as dotfiles in the user’s home directory like ~/.my-program-name and now everyone’s home directory has dozens of these.

Then I read this article.

Great was my embarrasment to discover that there was a neat little specification for data, config and cache directories in Linux that prevents this problem, and that I was not using it:

So I implemented a small and simple Python API as a single file, cfgs.py.

It works on all versions of Python from 2.7 to 3.7, has complete test coverage, and all the functionality is reachable from a single class, cfgs.App

How it works in one sentence

Create a cfgs.App for your application, project, or script which handles finding, reading and writing your data and config files, and managing your cache directories.

How to install

You can either use pip:

pip install cfgs

Or if you don’t like dependencies (and who does?), you can drop the source file cgfs.py right into your project.

Usage examples

import cfgs
app = cfgs.App('my-project')
print(app.xdg.XDG_CACHE_HOME)
#   /home/tom/.cache

app.xdg.XDG_CONFIG_DIRS
#   /etc/xdg

with app.config.open() as f:
    f.contents.update(name='oliver', species='dog')
    f.contents['description'] = {'size': 'S', 'fur': 'brown'}
    print(f.filename)
#    /home/tom/.config/my-project/my-project.json

# Later:
with app.config.open() as f:
    print(f.contents['name'])
#    oliver

    print(f.as_dict())
#     {'name': 'oliver', 'species': 'dog',
#      'description': {'size': 'S', 'fur': 'brown'}

Cache

import cfgs
cache_size = 0x10000000
app = cfgs.App('my-project')
directory = app.cache.directory(cache_size=cache_size)

with directory.open('cache') as f:
    f.write('cache data')

# TODO: rewrite cache or add features.

Using cfgs In legacy code

If you already have code to handle your config, data and cache files, then you can just use cgfs to get the XDG variables

from cfgs import XDG

xdg = XDG()
config_dir = xdg.XDG_CONFIG_HOME

# Your code here - eg:
my_config_file = os.path.join(config_dir, 'my-file.json')
with open(my_config_file) as f:
    legacy_write_my_file(f)

cfgs automatically handles data and config files, and independently, cache directories.

API Documentation

API documentation is here.


pic1

pic2

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

cfgs-0.14.0.tar.gz (37.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cfgs-0.14.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file cfgs-0.14.0.tar.gz.

File metadata

  • Download URL: cfgs-0.14.0.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cfgs-0.14.0.tar.gz
Algorithm Hash digest
SHA256 b4d83d043c724da3dc79eeed1dbca3a415e9c46f5c3d5eb69be420132fb9491b
MD5 df1d70a4ede1b49828766c09955a5851
BLAKE2b-256 7d1c2b2a2168a6068badea0e6a6d05b4c13095b52a7d8b2c77cb293f17400ef0

See more details on using hashes here.

File details

Details for the file cfgs-0.14.0-py3-none-any.whl.

File metadata

  • Download URL: cfgs-0.14.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cfgs-0.14.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1844a9ed14caee768c497c3daecf6cd420d1ab0bcbcadb80e3f61eb27cabfd00
MD5 279a543744e53c79a46bdfa0fb5a3db6
BLAKE2b-256 bc1b616cc63d62a33667a839485983a84f0b79762d7459f74b7d6cfb505ff612

See more details on using hashes here.

Supported by

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