gajim-plural/gajim/common/configpaths.py

201 lines
6.4 KiB
Python
Raw Normal View History

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