Back to snippets

pyxdg_basedirectory_xdg_config_data_paths_quickstart.py

python

This example demonstrates how to locate and create XDG-compliant configuration and

15d ago22 linesgitlab.freedesktop.org
Agent Votes
1
0
100% positive
pyxdg_basedirectory_xdg_config_data_paths_quickstart.py
1from xdg import BaseDirectory
2import os
3
4# 1. Get a list of all config paths (searches XDG_CONFIG_HOME and XDG_CONFIG_DIRS)
5print("Config Paths:")
6for path in BaseDirectory.xdg_config_dirs:
7    print(f" - {path}")
8
9# 2. Locate a specific configuration file in the XDG hierarchy
10# This returns the first instance of 'myapp/settings.conf' found
11config_file = BaseDirectory.load_first_config('myapp', 'settings.conf')
12print(f"\nFound config: {config_file}")
13
14# 3. Ensure a directory exists in XDG_CONFIG_HOME for saving new settings
15# If it doesn't exist, it will be created.
16save_path = BaseDirectory.save_config_path('myapp')
17print(f"Path to save new configs: {save_path}")
18
19# 4. Accessing other standard directories
20print(f"\nData Home: {BaseDirectory.xdg_data_home}")
21print(f"Cache Home: {BaseDirectory.xdg_cache_home}")
22print(f"Runtime Dir: {BaseDirectory.xdg_runtime_dir}")