configpaths.py 7.29 KB
Newer Older
roidelapluie's avatar
roidelapluie committed
1
# -*- coding:utf-8 -*-
roidelapluie's avatar
roidelapluie committed
2
3
4
## src/common/configpaths.py
##
## Copyright (C) 2006 Jean-Marie Traissard <jim AT lapin.org>
roidelapluie's avatar
roidelapluie committed
5
##                    Junglecow J <junglecow AT gmail.com>
Dicson's avatar
Dicson committed
6
## Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
roidelapluie's avatar
roidelapluie committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## Copyright (C) 2007 Brendan Taylor <whateley AT gmail.com>
## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
##
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
##
## Gajim 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 General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
##

25
26
27
import os
import sys
import tempfile
28
from common import defs
29
30
31
32
33
34

(
TYPE_CONFIG,
TYPE_CACHE,
TYPE_DATA
) = range(3)
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

# Note on path and filename encodings:
#
# In general it is very difficult to do this correctly.
# We may pull information from environment variables, and what encoding that is
# in is anyone's guess. Any information we request directly from the file
# system will be in filesystemencoding, and (parts of) paths that we write in
# this source code will be in whatever encoding the source is in. (I hereby
# declare this file to be UTF-8 encoded.)
#
# To make things more complicated, modern Windows filesystems use UTF-16, but
# the API tends to hide this from us.
#
# I tried to minimize problems by passing Unicode strings to OS functions as
# much as possible. Hopefully this makes the function return an Unicode string
# as well. If not, we get an 8-bit string in filesystemencoding, which we can
# happily pass to functions that operate on files and directories, so we can
# just leave it as is. Since these paths are meant to be internal to Gajim and
# not displayed to the user, Unicode is not really necessary here.
54
55

def fse(s):
56
57
58
    """
    Convert from filesystem encoding if not already Unicode
    """
Yann Leboulanger's avatar
Yann Leboulanger committed
59
    return s
60

61
def windowsify(s):
62
63
64
    if os.name == 'nt':
        return s.capitalize()
    return s
65

66
class ConfigPaths:
67
68
69
70
71
72
73
74
75
76
77
78
79
    def __init__(self):
        # {'name': (type, path), } type can be TYPE_CONFIG, TYPE_CACHE, TYPE_DATA
        # or None
        self.paths = {}

        if os.name == 'nt':
            try:
                # Documents and Settings\[User Name]\Application Data\Gajim

                # How are we supposed to know what encoding the environment
                # variable 'appdata' is in? Assuming it to be in filesystem
                # encoding.
                self.config_root = self.cache_root = self.data_root = \
80
                        os.path.join(fse(os.environ['appdata']), 'Gajim')
81
82
            except KeyError:
                # win9x, in cwd
83
                self.config_root = self.cache_root = self.data_root = '.'
84
85
        else: # Unices
            # Pass in an Unicode string, and hopefully get one back.
86
87
88
89
90
91
92
93
94
95
96
97
98
            expand = os.path.expanduser
            base = os.getenv('XDG_CONFIG_HOME')
            if base is None or base[0] != '/':
                base = expand('~/.config')
            self.config_root = os.path.join(base, 'gajim')
            base = os.getenv('XDG_CACHE_HOME')
            if base is None or base[0] != '/':
                base = expand('~/.cache')
            self.cache_root = os.path.join(base, 'gajim')
            base = os.getenv('XDG_DATA_HOME')
            if base is None or base[0] != '/':
                base = expand('~/.local/share')
            self.data_root = os.path.join(base, 'gajim')
99

100
101
102
103
104
105
106
        basedir = fse(os.environ.get('GAJIM_BASEDIR', defs.basedir))
        self.add('DATA', None, os.path.join(basedir, windowsify('data')))
        self.add('ICONS', None, os.path.join(basedir, windowsify('icons')))
        self.add('HOME', None, fse(os.path.expanduser('~')))
        self.add('PLUGINS_BASE', None,
                 os.path.join(basedir, windowsify('plugins')))

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    def add(self, name, type_, path):
        self.paths[name] = (type_, path)

    def __getitem__(self, key):
        type_, path = self.paths[key]
        if type_ == TYPE_CONFIG:
            return os.path.join(self.config_root, path)
        elif type_ == TYPE_CACHE:
            return os.path.join(self.cache_root, path)
        elif type_ == TYPE_DATA:
            return os.path.join(self.data_root, path)
        return path

    def get(self, key, default=None):
        try:
            return self[key]
        except KeyError:
            return default

126
    def items(self):
127
        for key in self.paths.keys():
128
129
            yield (key, self[key])

130
    def init(self, root=None, profile='', profile_separation=False):
131
132
133
        if root is not None:
            self.config_root = self.cache_root = self.data_root = root

134
        self.init_profile(profile)
135

136
137
138
139
        if len(profile) > 0 and profile_separation:
            profile = u'.' + profile
        else:
            profile = ''
140

141
        d = {'LOG_DB': 'logs.db', 'MY_CACERTS': 'cacerts.pem',
142
143
            'MY_EMOTS': 'emoticons', 'MY_ICONSETS': 'iconsets',
            'MY_MOOD_ICONSETS': 'moods', 'MY_ACTIVITY_ICONSETS': 'activities',
144
            'PLUGINS_USER': 'plugins',
145
            'RNG_SEED': 'rng_seed'}
146
        for name in d:
147
            d[name] += profile
148
            self.add(name, TYPE_DATA, windowsify(d[name]))
149
150
151
152
        if len(profile):
            self.add('MY_DATA', TYPE_DATA, 'data.dir')
        else:
            self.add('MY_DATA', TYPE_DATA, '')
153

154
        d = {'CACHE_DB': 'cache.db', 'VCARD': 'vcards',
155
                'AVATAR': 'avatars'}
156
        for name in d:
157
            d[name] += profile
158
            self.add(name, TYPE_CACHE, windowsify(d[name]))
159
160
161
162
        if len(profile):
            self.add('MY_CACHE', TYPE_CACHE, 'cache.dir')
        else:
            self.add('MY_CACHE', TYPE_CACHE, '')
163

164
165
166
167
        if len(profile):
            self.add('MY_CONFIG', TYPE_CONFIG, 'config.dir')
        else:
            self.add('MY_CONFIG', TYPE_CONFIG, '')
168
169
170

        try:
            self.add('TMP', None, fse(tempfile.gettempdir()))
Yann Leboulanger's avatar
Yann Leboulanger committed
171
        except IOError as e:
Yann Leboulanger's avatar
Yann Leboulanger committed
172
173
            print('Error opening tmp folder: %s\nUsing %s' % (str(e),
                os.path.expanduser('~')), file=sys.stderr)
174
175
176
177
178
179
180
181
            self.add('TMP', None, fse(os.path.expanduser('~')))

        try:
            import svn_config
            svn_config.configure(self)
        except (ImportError, AttributeError):
            pass

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    def init_profile(self, profile):
        conffile = windowsify('config')
        secretsfile = windowsify('secrets')
        pluginsconfdir = windowsify('pluginsconfig')
        certsdir = windowsify(u'certs')
        localcertsdir = windowsify(u'localcerts')

        if len(profile) > 0:
            conffile += '.' + profile
            secretsfile += '.' + profile
            pluginsconfdir += '.' + profile
            certsdir += u'.' + profile
            localcertsdir += u'.' + profile

        self.add('SECRETS_FILE', TYPE_DATA, secretsfile)
        self.add('MY_PEER_CERTS', TYPE_DATA, certsdir)
        self.add('CONFIG_FILE', TYPE_CONFIG, conffile)
        self.add('PLUGINS_CONFIG_DIR', TYPE_CONFIG, pluginsconfdir)
        self.add('MY_CERT', TYPE_CONFIG, localcertsdir)

202
gajimpaths = ConfigPaths()