/usr/lib/python2.7/dist-packages/dirspec/basedir.py is in python-dirspec 13.10-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | # -*- coding: utf-8 -*-
#
# Copyright 2011-2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""XDG Base Directory paths."""
from __future__ import unicode_literals, print_function
import os
from dirspec.utils import (default_cache_home,
default_config_path, default_config_home,
default_data_path, default_data_home,
get_env_path, unicode_path)
__all__ = ['xdg_cache_home',
'xdg_config_home',
'xdg_data_home',
'xdg_config_dirs',
'xdg_data_dirs',
'load_config_paths',
'load_data_paths',
'load_first_config',
'save_config_path',
'save_data_path',
]
def get_xdg_cache_home():
"""Get the path for XDG cache directory in user's HOME."""
return get_env_path('XDG_CACHE_HOME', default_cache_home)
def get_xdg_config_home():
"""Get the path for XDG config directory in user's HOME."""
return get_env_path('XDG_CONFIG_HOME', default_config_home)
def get_xdg_data_home():
"""Get the path for XDG data directory in user's HOME."""
return get_env_path('XDG_DATA_HOME', default_data_home)
def get_xdg_config_dirs():
"""Get the paths for the XDG config directories."""
result = [get_xdg_config_home()]
result.extend([x.encode('utf-8') for x in get_env_path(
'XDG_CONFIG_DIRS',
default_config_path).decode('utf-8').split(os.pathsep)])
return result
def get_xdg_data_dirs():
"""Get the paths for the XDG data directories."""
result = [get_xdg_data_home()]
result.extend([x.encode('utf-8') for x in get_env_path(
'XDG_DATA_DIRS',
default_data_path).decode('utf-8').split(os.pathsep)])
return result
def load_config_paths(*resource):
"""Iterator of configuration paths.
Return an iterator which gives each directory named 'resource' in
the configuration search path. Information provided by earlier
directories should take precedence over later ones (ie, the user's
config dir comes first).
"""
resource = os.path.join(*resource)
assert not resource.startswith('/')
for config_dir in get_xdg_config_dirs():
path = os.path.join(config_dir, resource.encode('utf-8'))
# access the file system always with unicode
# to properly behave in some operating systems
if os.path.exists(unicode_path(path)):
yield path
def load_data_paths(*resource):
"""Iterator of data paths.
Return an iterator which gives each directory named 'resource' in
the stored data search path. Information provided by earlier
directories should take precedence over later ones.
"""
resource = os.path.join(*resource)
assert not resource.startswith('/')
for data_dir in get_xdg_data_dirs():
path = os.path.join(data_dir, resource.encode('utf-8'))
# access the file system always with unicode
# to properly behave in some operating systems
if os.path.exists(unicode_path(path)):
yield path
def load_first_config(*resource):
"""Returns the first result from load_config_paths, or None if nothing
is found to load.
"""
for path in load_config_paths(*resource):
return path
return None
def save_config_path(*resource):
"""Path to save configuration.
Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path.
'resource' should normally be the name of your application. Use this
when SAVING configuration settings. Use the xdg_config_dirs variable
for loading.
"""
resource = os.path.join(*resource)
assert not resource.startswith('/')
path = os.path.join(get_xdg_config_home(), resource.encode('utf-8'))
# access the file system always with unicode
# to properly behave in some operating systems
if not os.path.isdir(unicode_path(path)):
os.makedirs(unicode_path(path), 0o700)
return path
def save_data_path(*resource):
"""Path to save data.
Ensure $XDG_DATA_HOME/<resource>/ exists, and return its path.
'resource' should normally be the name of your application. Use this
when STORING a resource. Use the xdg_data_dirs variable for loading.
"""
resource = os.path.join(*resource)
assert not resource.startswith('/')
path = os.path.join(get_xdg_data_home(), resource.encode('utf-8'))
# access the file system always with unicode
# to properly behave in some operating systems
if not os.path.isdir(unicode_path(path)):
os.makedirs(unicode_path(path), 0o700)
return path
# pylint: disable=C0103
xdg_cache_home = get_xdg_cache_home()
xdg_config_home = get_xdg_config_home()
xdg_data_home = get_xdg_data_home()
xdg_config_dirs = [x for x in get_xdg_config_dirs() if x]
xdg_data_dirs = [x for x in get_xdg_data_dirs() if x]
# pylint: disable=C0103
|