xdg_user_dirs.py 1.5 KB
Newer Older
Ryan Shipp's avatar
Ryan Shipp committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import subprocess


class UserDirs():

    def __init__(self):
        home = os.path.expanduser('~')
        xdg_dirs = ['DESKTOP', 'DOWNLOAD', 'TEMPLATES', 'PUBLICSHARE',
                    'DOCUMENTS', 'MUSIC', 'PICTURES', 'VIDEOS']

        if not os.path.exists(home + '/.config/user-dirs.locale'):
            # Only continue if xdg-user-dirs-update has never been run.
            # Record the user's initial configuration.
            original_dirs = []
            for file in os.listdir(home):
                if os.path.isdir(file) and not file.startswith('.'):
                    original_dirs += file
            # Call xdg-user-dirs-update, which creates all the folders. :(
            subprocess.call('xdg-user-dirs-update')
            # Reset the user's home directory to the initial state,
            # keeping only the config files written by
            # xdg-user-dirs-update.
            for file in os.listdir(home):
                if os.path.isdir(file) and not file.startswith('.'):
                    if file not in original_dirs:
                        # Delete non-empty dirs.
                        try:
                            os.rmdir(file)
                        except OSError:
                            pass

    def get(self, directory):
        """directory must be one of the XDG directory names, all
           caps.
        """
        return subprocess.check_output(['xdg-user-dir',
                directory]).replace('\n', '')