Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get application data files according to XDG basedir spec
#!/usr/bin/env python
import os
from os.path import expanduser, isdir, isfile, join, sep as pathsep
XDG_DATA_HOME = os.environ.get('XDG_DATA_HOME')
if not XDG_DATA_HOME:
XDG_DATA_HOME = join(expanduser('~'), '.local', 'share')
XDG_DATA_DIRS = os.environ.get('XDG_DATA_DIRS')
if not XDG_DATA_DIRS:
XDG_DATA_DIRS = [XDG_DATA_HOME, '/usr/local/share', '/usr/share']
else:
XDG_DATA_DIRS = [XDG_DATA_HOME] + [path for path in XDG_DATA_DIRS.split(':') if path]
def get_data_files(subdirs=None, ext=None):
result = {}
for directory in XDG_DATA_DIRS:
if subdirs:
directory = join(directory, *subdirs)
if isdir(directory):
for root, dirs, files in os.walk(directory):
subdir = root[len(directory.rstrip(pathsep)) + 1:]
for file in files:
if ext is not None and not file.endswith(ext):
continue
path = join(root, file)
if os.access(path, os.R_OK):
result[(file, subdir)] = path
return result.items()
def _test():
print('Application desktop files:\n')
for fn, path in sorted(get_data_files(['applications'], '.desktop')):
print("{}: {}".format(fn, path))
print('')
print('Vital presets:\n')
for fn, path in sorted(get_data_files(['vital', 'Presets'], '.vital')):
print("{}: {}".format(fn, path))
if __name__ == '__main__':
_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.