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-06-13 23:58:06 +02:00
|
|
|
from gajim.common import defs
|
2017-03-04 21:22:46 +01:00
|
|
|
from enum import Enum, unique
|
2010-02-04 20:08:30 +01: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
|
|
|
|
|
|
|
# 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.
|
2007-01-02 18:56:26 +01:00
|
|
|
|
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):
|
|
|
|
return gajimpaths[key]
|
|
|
|
|
|
|
|
|
2006-11-04 19:15:38 +01:00
|
|
|
class ConfigPaths:
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
2017-02-07 21:18:41 +01:00
|
|
|
# {'name': (type, path), } type can be Type.CONFIG, Type.CACHE, Type.DATA
|
2010-02-08 15:08:40 +01:00
|
|
|
# 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 = \
|
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 = '.'
|
2010-02-08 15:08:40 +01:00
|
|
|
else: # Unices
|
|
|
|
# Pass in an Unicode string, and hopefully get one back.
|
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-09-16 12:18:22 +02:00
|
|
|
basedir = os.environ.get('GAJIM_BASEDIR', defs.basedir)
|
2017-01-06 19:49:16 +01:00
|
|
|
self.add('ICONS', None, os.path.join(basedir, 'icons'))
|
2017-01-06 18:08:52 +01:00
|
|
|
self.add('HOME', None, os.path.expanduser('~'))
|
2017-01-06 19:49:16 +01:00
|
|
|
self.add('PLUGINS_BASE', None, os.path.join(basedir, '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])
|
|
|
|
|
2016-09-06 23:02:24 +02:00
|
|
|
def init(self, root=None, profile='', profile_separation=False):
|
2010-02-08 15:08:40 +01:00
|
|
|
if root is not None:
|
|
|
|
self.config_root = self.cache_root = self.data_root = root
|
|
|
|
|
2016-09-06 23:02:24 +02:00
|
|
|
self.init_profile(profile)
|
2017-01-06 17:44:31 +01:00
|
|
|
|
2016-09-06 23:02:24 +02:00
|
|
|
if len(profile) > 0 and profile_separation:
|
|
|
|
profile = u'.' + profile
|
|
|
|
else:
|
|
|
|
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',
|
2013-11-10 08:39:50 +01:00
|
|
|
'MY_EMOTS': 'emoticons', 'MY_ICONSETS': 'iconsets',
|
|
|
|
'MY_MOOD_ICONSETS': 'moods', 'MY_ACTIVITY_ICONSETS': 'activities',
|
2014-04-15 12:42:09 +02:00
|
|
|
'PLUGINS_USER': 'plugins',
|
2016-09-06 23:02:24 +02:00
|
|
|
'RNG_SEED': 'rng_seed'}
|
2010-02-08 15:08:40 +01:00
|
|
|
for name in d:
|
2016-09-05 01:43:39 +02:00
|
|
|
d[name] += profile
|
2017-02-07 21:18:41 +01:00
|
|
|
self.add(name, Type.DATA, windowsify(d[name]))
|
2016-09-06 16:19:44 +02:00
|
|
|
if len(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
|
|
|
|
2016-09-05 00:01:29 +02:00
|
|
|
d = {'CACHE_DB': 'cache.db', 'VCARD': 'vcards',
|
2016-09-06 23:02:24 +02:00
|
|
|
'AVATAR': 'avatars'}
|
2010-02-08 15:08:40 +01:00
|
|
|
for name in d:
|
2016-09-05 01:43:39 +02:00
|
|
|
d[name] += profile
|
2017-02-07 21:18:41 +01:00
|
|
|
self.add(name, Type.CACHE, windowsify(d[name]))
|
2016-09-06 16:19:44 +02:00
|
|
|
if len(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
|
|
|
|
2016-09-06 16:19:44 +02:00
|
|
|
if len(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:
|
2013-01-01 19:36:56 +01: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
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
gajimpaths = ConfigPaths()
|