Last active
November 24, 2019 08:58
-
-
Save SpotlightKid/dd6e9b4c8525cfa48cc09caff75f7eb9 to your computer and use it in GitHub Desktop.
Get application data files according to XDG basedir spec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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