~ubuntu-branches/ubuntu/precise/ubuntuone-client/precise-201112142106

« back to all changes in this revision

Viewing changes to ubuntuone/platform/xdg_base_directory/windows.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-08-25 16:11:47 UTC
  • mfrom: (1.1.54 upstream)
  • Revision ID: james.westby@ubuntu.com-20110825161147-v6zedpznh2evnurj
Tags: 1.7.2-0ubuntu1
* New upstream release.
  - Work correctly with static and GI bindings of gobject (LP: #829186)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Authors: Manuel de la Pena <manuel@canonical.com>
2
 
#          Diego Sarmentero <diego.sarmentero@canonical.com>
3
 
#
4
 
# Copyright 2011 Canonical Ltd.
5
 
#
6
 
# This program is free software: you can redistribute it and/or modify it
7
 
# under the terms of the GNU General Public License version 3, as published
8
 
# by the Free Software Foundation.
9
 
#
10
 
# This program is distributed in the hope that it will be useful, but
11
 
# WITHOUT ANY WARRANTY; without even the implied warranties of
12
 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13
 
# PURPOSE.  See the GNU General Public License for more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License along
16
 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
 
18
 
"""XDG helpers for windows."""
19
 
 
20
 
import os
21
 
 
22
 
 
23
 
def get_special_folders():
24
 
    # Routine to grab all the Windows Special Folders locations.
25
 
    # If successful, returns dictionary
26
 
    # of shell folder locations indexed on Windows keyword for each;
27
 
    # otherwise, returns an empty dictionary.
28
 
    special_folders = {}
29
 
 
30
 
    from win32com.shell import shell, shellcon
31
 
    # CSIDL_LOCAL_APPDATA = C:\Users\<username>\AppData\Local
32
 
    # CSIDL_PROFILE = C:\Users\<username>
33
 
    # CSIDL_COMMON_APPDATA = C:\ProgramData
34
 
    # More information on these at
35
 
    # http://msdn.microsoft.com/en-us/library/bb762494(v=vs.85).aspx
36
 
    get_path = lambda name: shell.SHGetFolderPath(
37
 
        0, getattr(shellcon, name), None, 0).encode('utf8')
38
 
    special_folders['Personal'] = get_path("CSIDL_PROFILE")
39
 
    special_folders['Local AppData'] = get_path("CSIDL_LOCAL_APPDATA")
40
 
    special_folders['AppData'] = os.path.dirname(
41
 
        special_folders['Local AppData'])
42
 
    special_folders['Common AppData'] = get_path("CSIDL_COMMON_APPDATA")
43
 
    return special_folders
44
 
 
45
 
special_folders = get_special_folders()
46
 
 
47
 
home_path = special_folders['Personal']
48
 
app_local_data_path = special_folders['Local AppData']
49
 
app_global_data_path = special_folders['Common AppData']
50
 
 
51
 
# use the non roaming app data
52
 
xdg_data_home = os.environ.get('XDG_DATA_HOME',
53
 
    os.path.join(app_local_data_path, 'xdg'))
54
 
 
55
 
 
56
 
def get_data_dirs():
57
 
    """Returns XDG data directories."""
58
 
    return os.environ.get('XDG_DATA_DIRS',
59
 
        '{0}{1}{2}'.format(app_local_data_path, os.pathsep,
60
 
        app_global_data_path)).split(os.pathsep)
61
 
 
62
 
xdg_data_dirs = get_data_dirs()
63
 
 
64
 
# we will return the roaming data wich is as close as we get in windows
65
 
# regarding caching.
66
 
xdg_cache_home = os.environ.get('XDG_CACHE_HOME',
67
 
    os.path.join(xdg_data_home, 'cache'))
68
 
 
69
 
# point to the not roaming app data for the user
70
 
xdg_config_home = os.environ.get('XDG_CONFIG_HOME',
71
 
    app_local_data_path)
72
 
 
73
 
 
74
 
def get_config_dirs():
75
 
    """Return XDG config directories."""
76
 
    return [xdg_config_home] + \
77
 
        os.environ.get('XDG_CONFIG_DIRS',
78
 
            app_global_data_path,
79
 
            ).split(os.pathsep)
80
 
 
81
 
xdg_config_dirs = get_config_dirs()
82
 
 
83
 
xdg_data_dirs = filter(lambda x: x, xdg_data_dirs)
84
 
xdg_config_dirs = filter(lambda x: x, xdg_config_dirs)
85
 
 
86
 
 
87
 
def load_config_paths(*resource):
88
 
    """Iterator of configuration paths.
89
 
 
90
 
    Return an iterator which gives each directory named 'resource' in
91
 
    the configuration search path. Information provided by earlier
92
 
    directories should take precedence over later ones (ie, the user's
93
 
    config dir comes first).
94
 
    """
95
 
    resource = os.path.join(*resource)
96
 
    for config_dir in xdg_config_dirs:
97
 
        path = os.path.join(config_dir, resource)
98
 
        if os.path.exists(path):
99
 
            yield path
100
 
 
101
 
 
102
 
def save_config_path(*resource):
103
 
    """Path to save configuration.
104
 
 
105
 
    Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path.
106
 
    'resource' should normally be the name of your application. Use this
107
 
    when SAVING configuration settings. Use the xdg_config_dirs variable
108
 
    for loading.
109
 
    """
110
 
    resource = os.path.join(*resource)
111
 
    assert not resource.startswith('/')
112
 
    path = os.path.join(xdg_config_home, resource)
113
 
    if not os.path.isdir(path):
114
 
        os.makedirs(path, 0700)
115
 
    return path