Skip to main content

Another XDG Base Directory Specification utility.

Project description

xdgenvpy

xdgenvpy is yet another Python utility for the XDG Base Directory Specification.

{:toc}

How to use

Python

There are three main ways to use xdgenvpy as a Python package,

  1. Retrieve XDG environment variables, or the specification defaults.
  2. Determine package specific directories based on the XDG spec.
  3. Or pedantically create package specific directories before attempting to use the directory.

To use xdgenvpy as a simple XDG base directory getter, simply create a new xdgenvpy.XDG object and use the properties it exposes.

from xdgenvpy import XDG
xdg = XDG()
print(xdg.XDG_DATA_HOME)        # /home/user/.local/share
print(xdg.XDG_CONFIG_HOME)      # /home/user/.config
print(xdg.XDG_CACHE_HOME)       # /home/user/.cache
print(xdg.XDG_RUNTIME_DIR)      # /run/user/1000
print(xdg.XDG_DATA_DIRS)        # /home/user/.local/share:/usr/local/share/:/usr/share/
print(xdg.XDG_CONFIG_DIRS)      # /home/user/.config:/etc/xdg

But sometimes you want to use package specific directories derived from the XDG base directories. This can be done with the xdgenvpy.XDGPackage class.

from xdgenvpy import XDGPackage
xdg = XDGPackage('mypackage') 
print(xdg.XDG_DATA_HOME)        # /home/user/.local/share/mypackage
print(xdg.XDG_CONFIG_HOME)      # /home/user/.config/mypackage
print(xdg.XDG_CACHE_HOME)       # /home/user/.cache/mypackage
print(xdg.XDG_RUNTIME_DIR)      # /run/user/1000/mypackage
print(xdg.XDG_DATA_DIRS)        # /home/user/.local/share/mypackage:/usr/local/share/:/usr/share/
print(xdg.XDG_CONFIG_DIRS)      # /home/user/.config/mypackage:/etc/xdg')

Lastly, you could also use xdgenvpy.XDGPedanticPackage to ensure each of the package specific directories exist before the calling code attempts to use the directory. Instances of the xdgenvpy.XDGPedanticPackage class will not create system level directories, only package directories on the DATA, CONFIG, CACHE, and RUNTIME variables.

from xdgenvpy import XDGPedanticPackage
xdg = XDGPedanticPackage('mypackage')
print(xdg.XDG_DATA_HOME)        # /home/user/.local/share/mypackage
print(xdg.XDG_CONFIG_HOME)      # /home/user/.config/mypackage
print(xdg.XDG_CACHE_HOME)       # /home/user/.cache/mypackage
print(xdg.XDG_RUNTIME_DIR)      # /run/user/1000/mypackage
print(xdg.XDG_DATA_DIRS)        # /home/user/.local/share/mypackage:/usr/local/share/:/usr/share/
print(xdg.XDG_CONFIG_DIRS)      # /home/user/.config/mypackage:/etc/xdg

CLI

xdgenvpy also includes a runnable module, which is easily accessible via the script xdg-env. Pip will normally install scripts under something like: ~/.local/bin

The installed xdg-env command essentially takes a list of XDG variables, and an optional package name. For each XDG variable specified, xdg-env will print its corresponding value based on the specification. It can optionally take the name of a package and include that into the variable's values.

But can't we just echo the XDG variables like so?

echo ${XDG_DATA_HOME}
echo ${XDG_CONFIG_HOME}
echo ${XDG_CACHE_HOME}
echo ${XDG_RUNTIME_DIR}
echo ${XDG_DATA_DIRS}
echo ${XDG_CONFIG_DIRS}

Well, yes. But there is a problem when the variables are not defined. The xdg-env command will always print a value that adheres to the spec. If the environment variable does not exist, then the default value will be returned, as defined by the XDG Base Directory Specification.

Although the Python package supports a pedantic mode, the xdg-env command will not change the file system. Even if a package name is supplied and the directories do not exist, xdg-env will not create any files/directories. This was simply a design decision to keep the shell command as file-system safe as possible.

How to install

Install locally as a normal user:

pip3 install --user xdgenvpy

Or install globally as the all powerful root:

sudo pip3 install xdgenvpy

A Word About Windows

The XDG Base Directory Specification. does not mention how the spec should be implemented on Windows-based platforms. That said, many applications on Windows still follow a very similar convention to the XDG base directory spec. And that is to generally place config files under %APPDATA%/MyPackage/configs.

If we squint, it kind of looks like we can simply replace the POSIX tilde ~ with the Windows %APPDATA% variable. Then there's a directory that is the application's name. And finally, any configs or data files the application needs to save is under that directory.

Generally, xdgenvpy works in this way on Windows-based platforms. Though this is not a perfect solution as Windows applications can put configs and data files under any of the directories Local, LocalLow, and Roaming (where the Roaming directory typically is pointed to by %APPDATA%). Additionally, some XDG variables do not make much sense on Windows-based platforms. XDG_RUNTIME_DIR is one such example. On Unix systems it defaults to /run/user/USERID. There is no close equivalent to a Windows-based directory. As such, xdgenvpy does not do anything fancy other than prepend %APPDATA% to most directories and drop any . prefixes for hidden directories/files.

That said, if you use xdgenvpy extensively on Windows platforms and would like better support, create GitLab issues on the project or submit Merge Requests. Let's all make xdgenvpy as useful as possible, even if it needs to implement XDG base directory spec-like features on non-Unix platforms.

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

xdgenvpy-2.0.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

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

xdgenvpy-2.0.0-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file xdgenvpy-2.0.0.tar.gz.

File metadata

  • Download URL: xdgenvpy-2.0.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.4

File hashes

Hashes for xdgenvpy-2.0.0.tar.gz
Algorithm Hash digest
SHA256 2531fe5a905d119d869e45bf5e231bd5dff7a49a2c7b4852c33985041ee5db7d
MD5 f97cd6d38e575741a4b44a25f6872e81
BLAKE2b-256 e8b4053107c67aa82fb6d89a2580b38ce6f5bce9d683891737814a698d662cef

See more details on using hashes here.

File details

Details for the file xdgenvpy-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: xdgenvpy-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.4

File hashes

Hashes for xdgenvpy-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a56c19da48cc8bb5bc1e8934128a1510733d70533c10722922f29cb06a4be3d8
MD5 959ad9ab4e499be9458138ba2b778344
BLAKE2b-256 1db30ac1abe798a011c004c6bc38231e2d9c47c7aa9e23f2a68fc819a3d090a1

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