2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/common/configpaths.py
|
|
|
|
##
|
|
|
|
## Copyright (C) 2006 Jean-Marie Traissard <jim AT lapin.org>
|
2008-08-15 19:31:51 +02:00
|
|
|
## Junglecow J <junglecow AT gmail.com>
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:20:23 +02:00
|
|
|
## 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/>.
|
|
|
|
##
|
|
|
|
|
2006-11-04 19:15:38 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import tempfile
|
2017-03-04 21:22:46 +01:00
|
|
|
from enum import Enum, unique
|
2010-02-04 20:08:30 +01:00
|
|
|
|
2018-04-21 12:44:10 +02:00
|
|
|
|
2017-03-04 21:22:46 +01:00
|
|
|
@unique
|
2017-02-07 21:18:41 +01:00
|
|
|
class Type(Enum):
|
|
|
|
CONFIG = 0
|
|
|
|
CACHE = 1
|
|
|
|
DATA = 2
|
2006-11-04 19:15:38 +01:00
|
|
|
|
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
def windowsify(s):
|
2010-02-08 15:08:40 +01:00
|
|
|
if os.name == 'nt':
|
|
|
|
return s.capitalize()
|
|
|
|
return s
|
2007-07-26 01:04:15 +02:00
|
|
|
|
2017-01-06 18:08:52 +01:00
|
|
|
|
2017-01-06 19:38:51 +01:00
|
|
|
def get(key):
|
2018-04-21 12:44:10 +02:00
|
|
|
return _paths[key]
|
|
|
|
|
|
|
|
|
|
|
|
def set_separation(active: bool):
|
|
|
|
_paths.profile_separation = active
|
|
|
|
|
|
|
|
|
|
|
|
def set_profile(profile: str):
|
|
|
|
_paths.profile = profile
|
|
|
|
|
|
|
|
|
|
|
|
def set_config_root(config_root: str):
|
2018-04-21 14:47:47 +02:00
|
|
|
_paths.custom_config_root = config_root
|
2018-04-21 12:44:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def init():
|
|
|
|
_paths.init()
|
2017-01-06 19:38:51 +01:00
|
|
|
|
|
|
|
|
2006-11-04 19:15:38 +01:00
|
|
|
class ConfigPaths:
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.paths = {}
|
2018-04-21 12:44:10 +02:00
|
|
|
self.profile = ''
|
|
|
|
self.profile_separation = False
|
2018-04-21 14:47:47 +02:00
|
|
|
self.custom_config_root = None
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
try:
|
|
|
|
# Documents and Settings\[User Name]\Application Data\Gajim
|
|
|
|
self.config_root = self.cache_root = self.data_root = \
|
2017-01-06 18:08:52 +01:00
|
|
|
os.path.join(os.environ['appdata'], 'Gajim')
|
2010-02-08 15:08:40 +01:00
|
|
|
except KeyError:
|
|
|
|
# win9x, in cwd
|
2013-01-01 19:44:25 +01:00
|
|
|
self.config_root = self.cache_root = self.data_root = '.'
|
2018-04-21 12:44:10 +02:00
|
|
|
else:
|
2016-11-05 15:49:38 +01:00
|
|
|
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')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-09-16 12:18:22 +02:00
|
|
|
import pkg_resources
|
|
|
|
basedir = pkg_resources.resource_filename("gajim", ".")
|
2017-01-06 19:49:16 +01:00
|
|
|
self.add('DATA', None, os.path.join(basedir, 'data'))
|
2017-01-06 19:52:50 +01:00
|
|
|
self.add('GUI', None, os.path.join(basedir, 'data', 'gui'))
|
2017-07-20 00:43:06 +02:00
|
|
|
self.add('ICONS', None, os.path.join(basedir, 'data', 'icons'))
|
2017-01-06 18:08:52 +01:00
|
|
|
self.add('HOME', None, os.path.expanduser('~'))
|
2017-09-22 21:34:06 +02:00
|
|
|
self.add('PLUGINS_BASE', None, os.path.join(basedir, 'data', 'plugins'))
|
2017-01-06 17:44:31 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def add(self, name, type_, path):
|
|
|
|
self.paths[name] = (type_, path)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
type_, path = self.paths[key]
|
2017-02-07 21:18:41 +01:00
|
|
|
if type_ == Type.CONFIG:
|
2010-02-08 15:08:40 +01:00
|
|
|
return os.path.join(self.config_root, path)
|
2017-02-07 21:18:41 +01:00
|
|
|
elif type_ == Type.CACHE:
|
2010-02-08 15:08:40 +01:00
|
|
|
return os.path.join(self.cache_root, path)
|
2017-02-07 21:18:41 +01:00
|
|
|
elif type_ == Type.DATA:
|
2010-02-08 15:08:40 +01:00
|
|
|
return os.path.join(self.data_root, path)
|
|
|
|
return path
|
|
|
|
|
|
|
|
def get(self, key, default=None):
|
|
|
|
try:
|
|
|
|
return self[key]
|
|
|
|
except KeyError:
|
|
|
|
return default
|
|
|
|
|
2013-01-02 13:54:02 +01:00
|
|
|
def items(self):
|
2013-01-05 09:52:09 +01:00
|
|
|
for key in self.paths.keys():
|
2010-02-08 15:08:40 +01:00
|
|
|
yield (key, self[key])
|
|
|
|
|
2018-04-21 12:44:10 +02:00
|
|
|
def init(self):
|
2018-04-21 14:47:47 +02:00
|
|
|
if self.custom_config_root:
|
|
|
|
self.cache_root = self.data_root = self.config_root = self.custom_config_root
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-04-21 12:44:10 +02:00
|
|
|
self.add('CONFIG_ROOT', None, self.config_root)
|
|
|
|
self.add('CACHE_ROOT', None, self.cache_root)
|
|
|
|
self.add('DATA_ROOT', None, self.data_root)
|
2017-01-06 17:44:31 +01:00
|
|
|
|
2018-04-21 12:44:10 +02:00
|
|
|
self.init_profile(self.profile)
|
|
|
|
|
|
|
|
if len(self.profile) > 0 and self.profile_separation:
|
|
|
|
self.profile = u'.' + self.profile
|
2016-09-06 23:02:24 +02:00
|
|
|
else:
|
2018-04-21 12:44:10 +02:00
|
|
|
self.profile = ''
|
2017-01-06 17:44:31 +01:00
|
|
|
|
2016-09-05 00:01:29 +02:00
|
|
|
d = {'LOG_DB': 'logs.db', 'MY_CACERTS': 'cacerts.pem',
|
2018-04-21 12:44:10 +02:00
|
|
|
'MY_EMOTS': 'emoticons', 'MY_ICONSETS': 'iconsets',
|
|
|
|
'MY_MOOD_ICONSETS': 'moods', 'MY_ACTIVITY_ICONSETS': 'activities',
|
|
|
|
'PLUGINS_USER': 'plugins'}
|
2010-02-08 15:08:40 +01:00
|
|
|
for name in d:
|
2018-04-21 12:44:10 +02:00
|
|
|
d[name] += self.profile
|
2017-02-07 21:18:41 +01:00
|
|
|
self.add(name, Type.DATA, windowsify(d[name]))
|
2018-04-21 12:44:10 +02:00
|
|
|
if len(self.profile):
|
2017-02-07 21:18:41 +01:00
|
|
|
self.add('MY_DATA', Type.DATA, 'data.dir')
|
2016-09-06 16:19:44 +02:00
|
|
|
else:
|
2017-02-07 21:18:41 +01:00
|
|
|
self.add('MY_DATA', Type.DATA, '')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-04-21 12:44:10 +02:00
|
|
|
d = {'CACHE_DB': 'cache.db',
|
|
|
|
'VCARD': 'vcards',
|
|
|
|
'AVATAR': 'avatars'}
|
2010-02-08 15:08:40 +01:00
|
|
|
for name in d:
|
2018-04-21 12:44:10 +02:00
|
|
|
d[name] += self.profile
|
2017-02-07 21:18:41 +01:00
|
|
|
self.add(name, Type.CACHE, windowsify(d[name]))
|
2018-04-21 12:44:10 +02:00
|
|
|
if len(self.profile):
|
2017-02-07 21:18:41 +01:00
|
|
|
self.add('MY_CACHE', Type.CACHE, 'cache.dir')
|
2016-09-06 16:19:44 +02:00
|
|
|
else:
|
2017-02-07 21:18:41 +01:00
|
|
|
self.add('MY_CACHE', Type.CACHE, '')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-04-21 12:44:10 +02:00
|
|
|
if len(self.profile):
|
2017-02-07 21:18:41 +01:00
|
|
|
self.add('MY_CONFIG', Type.CONFIG, 'config.dir')
|
2016-09-06 16:19:44 +02:00
|
|
|
else:
|
2017-02-07 21:18:41 +01:00
|
|
|
self.add('MY_CONFIG', Type.CONFIG, '')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
try:
|
2017-01-06 18:08:52 +01:00
|
|
|
self.add('TMP', None, tempfile.gettempdir())
|
2013-01-01 23:18:36 +01:00
|
|
|
except IOError as e:
|
2018-04-21 12:44:10 +02:00
|
|
|
print('Error opening tmp folder: %s\nUsing %s' % (
|
|
|
|
str(e), os.path.expanduser('~')), file=sys.stderr)
|
2017-01-06 18:08:52 +01:00
|
|
|
self.add('TMP', None, os.path.expanduser('~'))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2016-09-06 23:02:24 +02:00
|
|
|
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
|
|
|
|
|
2017-02-07 21:18:41 +01:00
|
|
|
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)
|
2016-09-06 23:02:24 +02:00
|
|
|
|
2018-04-21 12:44:10 +02:00
|
|
|
|
|
|
|
_paths = ConfigPaths()
|
|
|
|
|
|
|
|
# For backwards compatibility needed
|
|
|
|
# some plugins use that
|
|
|
|
gajimpaths = _paths
|