Skip to main content

Vendor-namespaced XDG Base Directory paths for Linux desktop apps.

Project description

codechu-xdg

Vendor-namespaced XDG Base Directory paths for Linux desktop apps. Tiny — ~80 LOC, stdlib-only, no dependencies.

pip install codechu-xdg

What it gives you

  • 5 standard XDG path types: config, cache, data, state, runtime
  • Mandatory vendor + product namespace — every path is $XDG_BASE/<vendor>/<product>/, so multiple products from the same publisher live under one directory (one ~/.config/<vendor>/ reveals all your products)
  • ensure()mkdir -p for all 5 dirs
  • migrate() — idempotent legacy → new path mover (run once at startup to upgrade users from earlier layouts)

Quick examples

Basic

from codechu_xdg import App

app = App(vendor="codechu", product="disk-cleaner")
app.ensure()  # create all 5 dirs

# Paths
settings  = app.config_dir  / "settings.json"
db_cache  = app.cache_dir   / "du_cache.db"
snapshots = app.data_dir    / "snapshots.db"
log_file  = app.state_dir   / "app.log"
pid_file  = app.runtime_dir / "watchdog.pid"
sock_file = app.runtime_dir / "control.sock"

Convenience helpers

For common file types, helpers eliminate dir / "name" boilerplate:

app.settings_file()                # config_dir / settings.json (default name)
app.settings_file("themes.json")   # config_dir / themes.json
app.cache_file("du_cache.db")
app.data_file("snapshots.db")
app.log_file()                     # state_dir / app.log
app.runtime_file("watchdog.pid")

Cleanup at shutdown / on demand

# Wipe stale sockets and pid files — recreated on next run.
app.remove_runtime()

# Or full cache wipe (regeneratable). Returns count of removed entries.
n = app.remove_cache()
print(f"cleared {n} cache entries")

Migration from earlier layouts

When you change directory conventions across versions, migrate() moves files idempotently — runs safely on every startup:

from pathlib import Path

moved = app.migrate({
    # v0.0 layout: everything under ~/.config/<product>/
    Path.home() / ".config" / "disk_cleaner" / "settings.json":
        app.config_dir / "settings.json",
    Path.home() / ".config" / "disk_cleaner" / "du_cache.db":
        app.cache_dir / "du_cache.db",
    Path.home() / ".config" / "disk_cleaner" / "snapshots.db":
        app.data_dir / "snapshots.db",
})
print(f"migrated {moved} files")

Files at the new path are never overwritten — if new exists, the old is left in place. This makes it safe to run on every startup.

Why a vendor namespace

Single-vendor apps put files at ~/.config/<product>/ — fine for one product. Multi-product publishers benefit from grouping:

~/.config/codechu/
├── disk-cleaner/
├── file-explorer/
└── system-monitor/

One directory, full inventory. Pattern used by JetBrains (~/.config/JetBrains/) and Mozilla (~/.mozilla/).

Path resolution

Standard XDG environment variables:

Path Env var Default
config XDG_CONFIG_HOME ~/.config
cache XDG_CACHE_HOME ~/.cache
data XDG_DATA_HOME ~/.local/share
state XDG_STATE_HOME ~/.local/state
runtime XDG_RUNTIME_DIR /run/user/$UID

Honored at module import time. Re-import / reload for env changes during tests.

Validation

  • vendor and product must be non-empty and not contain /.
  • All directories use safe mkdir -p semantics — no race-on-create.

Multi-platform?

This library is Linux-only (XDG is a Linux/Unix spec). For macOS or Windows in addition to Linux, see platformdirs on PyPI — note that it does not enforce a vendor namespace on Linux.

License

MIT — see LICENSE.

Part of Codechu.

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

codechu_xdg-0.1.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

codechu_xdg-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file codechu_xdg-0.1.0.tar.gz.

File metadata

  • Download URL: codechu_xdg-0.1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for codechu_xdg-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6134b4aec77b95b3f8a012c8aa34946a67ce81429e0bb3aa6a06098fc311b7a9
MD5 c52e838d1e90cf3fa8ed0138010306a4
BLAKE2b-256 4bc3202c445fcbd97e116507ec2c2e40bbeb861305551d61812c239ad16db436

See more details on using hashes here.

File details

Details for the file codechu_xdg-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: codechu_xdg-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for codechu_xdg-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c2e9cce99f392c6e18bff3b137a5656be13ae60973f5507f1a6ae32e87d9a07e
MD5 4d01a2489eee8ce563546af30224397e
BLAKE2b-256 e6cebba949fd6cff33d2c6a3b1f2776c1ae5990d9ab771b71cf84b338588d1b0

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