def get_xdg_path(filename, data_type, default_directories=None,
                 create_missing_dir=False):
    """Get the full path to a file using XDG file layout spec.

    Follows XDG Base Directory Specification.
    (http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html).
    Tries default_directories and DEFAULT_GOOGLECL_DIR if no file is found
    via XDG spec.

    Keyword arguments:
      filename: Filename of the file.
      data_type: One of 'config' for config files or 'data' for data files.
      default_directories: List of directories to check if no file is found
          in directories specified by XDG spec and DEFAULT_GOOGLECL_DIR.
          Default None.
      create_missing_dir: Whether or not to create a missing config subdirectory
          in the default base directory. Default False.

    Returns:
      Path to config file, which may not exist. If create_missing_dir,
      the directory where the config file should be will be created if it
      does not exist.

    """
    data_type = data_type.upper()
    if data_type not in ('CONFIG', 'DATA'):
        raise Exception('Invalid value for data_type: ' + data_type)
    xdg_home_dir = os.environ.get('XDG_' + data_type + '_HOME')
    if not xdg_home_dir:
        home_dir = os.path.expanduser('~')
        if data_type == 'DATA':
            xdg_home_dir = os.path.join(home_dir, '.local', 'share')
        elif data_type == 'CONFIG':
            # No variable defined, using $HOME/.config
            xdg_home_dir = os.path.join(home_dir, '.config')
    xdg_home_dir = os.path.join(xdg_home_dir, SUBDIR_NAME)

    xdg_dir_list = os.environ.get('XDG_' + data_type + '_DIRS')
    if not xdg_dir_list:
        if data_type == 'DATA':
            xdg_dir_list = '/usr/local/share/:/usr/share/'
        elif data_type == 'CONFIG':
            xdg_dir_list = '/etc/xdg'
    xdg_dir_list = [os.path.join(d, SUBDIR_NAME)
                    for d in xdg_dir_list.split(':')]

    dir_list = [os.path.abspath('.'), xdg_home_dir] + xdg_dir_list +\
               [DEFAULT_GOOGLECL_DIR]
    if default_directories:
        dir_list += default_directories
    for directory in dir_list:
        config_path = os.path.join(directory, filename)
        if os.path.isfile(config_path):
            return config_path
    LOG.debug('Could not find ' + filename + ' in any of ' + str(dir_list))

    if os.name == 'posix':
        default_dir = xdg_home_dir
        mode = 0700
    else:
        default_dir = DEFAULT_GOOGLECL_DIR
        mode = 0777
    if not os.path.isdir(default_dir) and create_missing_dir:
        try:
            os.makedirs(default_dir, mode)
        except OSError, err:
            LOG.error(err)
            return ''
    return os.path.join(default_dir, filename)
dataset/ETHPy150Open vinitkumar/googlecl/src/googlecl/__init__.py/get_xdg_path/original