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>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2006-2007 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/>.
|
|
|
|
##
|
|
|
|
|
2006-11-04 19:15:38 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import tempfile
|
2009-11-28 12:54:41 +01:00
|
|
|
import defs
|
2010-02-04 20:08:30 +01:00
|
|
|
HAVE_XDG = True
|
|
|
|
try:
|
|
|
|
import xdg.BaseDirectory
|
|
|
|
except:
|
|
|
|
HAVE_XDG = False
|
|
|
|
|
|
|
|
(
|
|
|
|
TYPE_CONFIG,
|
|
|
|
TYPE_CACHE,
|
|
|
|
TYPE_DATA
|
|
|
|
) = range(3)
|
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
|
|
|
|
|
|
|
def fse(s):
|
2009-11-26 11:21:43 +01:00
|
|
|
"""
|
|
|
|
Convert from filesystem encoding if not already Unicode
|
|
|
|
"""
|
2007-01-02 18:56:26 +01:00
|
|
|
return unicode(s, sys.getfilesystemencoding())
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
def windowsify(s):
|
|
|
|
if os.name == 'nt':
|
|
|
|
return s.capitalize()
|
|
|
|
return s
|
|
|
|
|
2006-11-04 19:15:38 +01:00
|
|
|
class ConfigPaths:
|
2010-02-04 20:08:30 +01:00
|
|
|
def __init__(self):
|
|
|
|
# {'name': (type, path), } type can be TYPE_CONFIG, TYPE_CACHE, TYPE_DATA
|
|
|
|
# or None
|
2006-11-21 19:46:33 +01:00
|
|
|
self.paths = {}
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2010-02-04 20:08:30 +01:00
|
|
|
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 = \
|
|
|
|
os.path.join(fse(os.environ[u'appdata']), u'Gajim')
|
|
|
|
except KeyError:
|
|
|
|
# win9x, in cwd
|
|
|
|
self.config_root = self.cache_root = self.data_root = u'.'
|
|
|
|
else: # Unices
|
|
|
|
# Pass in an Unicode string, and hopefully get one back.
|
|
|
|
if HAVE_XDG:
|
|
|
|
self.config_root = xdg.BaseDirectory.load_first_config('gajim')
|
|
|
|
if not self.config_root:
|
|
|
|
# Folder doesn't exist yet.
|
|
|
|
self.config_root = os.path.join(xdg.BaseDirectory.\
|
|
|
|
xdg_config_dirs[0], u'gajim')
|
|
|
|
|
|
|
|
self.cache_root = os.path.join(xdg.BaseDirectory.xdg_cache_home,
|
|
|
|
u'gajim')
|
|
|
|
|
|
|
|
self.data_root = xdg.BaseDirectory.save_data_path('gajim')
|
|
|
|
if not self.data_root:
|
|
|
|
self.data_root = os.path.join(xdg.BaseDirectory.\
|
|
|
|
xdg_data_dirs[0], u'gajim')
|
|
|
|
else:
|
2010-02-07 17:52:15 +01:00
|
|
|
expand = os.path.expanduser
|
|
|
|
base = os.getenv('XDG_CONFIG_HOME') or expand(u'~/.config')
|
|
|
|
self.config_root = os.path.join(base, u'gajim')
|
|
|
|
base = os.getenv('XDG_CACHE_HOME') or expand(u'~/.cache')
|
|
|
|
self.cache_root = os.path.join(base, u'gajim')
|
|
|
|
base = os.getenv('XDG_DATA_HOME') or expand(u'~/.local/share')
|
|
|
|
self.data_root = os.path.join(base, u'gajim')
|
2010-02-04 20:08:30 +01:00
|
|
|
|
|
|
|
def add(self, name, type_, path):
|
|
|
|
self.paths[name] = (type_, path)
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2006-11-21 19:46:33 +01:00
|
|
|
def __getitem__(self, key):
|
2010-02-04 20:08:30 +01:00
|
|
|
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
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2006-11-21 19:46:33 +01:00
|
|
|
def get(self, key, default=None):
|
2006-11-04 19:15:38 +01:00
|
|
|
try:
|
2006-11-21 19:46:33 +01:00
|
|
|
return self[key]
|
2006-11-04 19:15:38 +01:00
|
|
|
except KeyError:
|
|
|
|
return default
|
|
|
|
|
2006-11-21 19:46:33 +01:00
|
|
|
def iteritems(self):
|
|
|
|
for key in self.paths.iterkeys():
|
|
|
|
yield (key, self[key])
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2010-02-04 20:08:30 +01:00
|
|
|
def init(self, root=None):
|
2007-07-26 01:06:33 +02:00
|
|
|
if root is not None:
|
2010-02-04 20:08:30 +01:00
|
|
|
self.config_root = self.cache_root = self.data_root = root
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2010-02-04 20:08:30 +01:00
|
|
|
d = {'MY_DATA': '', 'LOG_DB': u'logs.db', 'MY_CACERTS': u'cacerts.pem',
|
|
|
|
'MY_EMOTS': u'emoticons', 'MY_ICONSETS': u'iconsets',
|
2010-02-08 22:25:41 +01:00
|
|
|
'MY_MOOD_ICONSETS': u'moods', 'MY_ACTIVITY_ICONSETS': u'activities',
|
|
|
|
'PLUGINS_USER': u'plugins'}
|
2010-02-04 20:08:30 +01:00
|
|
|
for name in d:
|
|
|
|
self.add(name, TYPE_DATA, windowsify(d[name]))
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2010-02-04 20:08:30 +01:00
|
|
|
d = {'MY_CACHE': '', 'CACHE_DB': u'cache.db', 'VCARD': u'vcards',
|
|
|
|
'AVATAR': u'avatars'}
|
|
|
|
for name in d:
|
|
|
|
self.add(name, TYPE_CACHE, windowsify(d[name]))
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2010-02-04 20:08:30 +01:00
|
|
|
self.add('MY_CONFIG', TYPE_CONFIG, '')
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2009-11-28 12:54:41 +01:00
|
|
|
basedir = fse(os.environ.get(u'GAJIM_BASEDIR', defs.basedir))
|
2010-02-04 20:08:30 +01:00
|
|
|
self.add('DATA', None, os.path.join(basedir, windowsify(u'data')))
|
|
|
|
self.add('ICONS', None, os.path.join(basedir, windowsify(u'icons')))
|
|
|
|
self.add('HOME', None, fse(os.path.expanduser('~')))
|
2010-02-08 22:25:41 +01:00
|
|
|
self.add('PLUGINS_BASE', None, os.path.join(basedir,
|
|
|
|
windowsify(u'plugins')))
|
2009-01-21 08:14:03 +01:00
|
|
|
try:
|
2010-02-04 20:08:30 +01:00
|
|
|
self.add('TMP', None, fse(tempfile.gettempdir()))
|
2009-01-21 08:14:03 +01:00
|
|
|
except IOError, e:
|
|
|
|
print >> sys.stderr, 'Error opening tmp folder: %s\nUsing %s' % (
|
|
|
|
str(e), os.path.expanduser('~'))
|
2010-02-04 20:08:30 +01:00
|
|
|
self.add('TMP', None, fse(os.path.expanduser('~')))
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
try:
|
|
|
|
import svn_config
|
|
|
|
svn_config.configure(self)
|
|
|
|
except (ImportError, AttributeError):
|
|
|
|
pass
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2010-02-04 20:08:30 +01:00
|
|
|
def init_profile(self, profile=''):
|
2007-07-26 01:04:15 +02:00
|
|
|
conffile = windowsify(u'config')
|
|
|
|
pidfile = windowsify(u'gajim')
|
2007-08-26 00:42:35 +02:00
|
|
|
secretsfile = windowsify(u'secrets')
|
2008-07-29 21:09:28 +02:00
|
|
|
pluginsconfdir = windowsify(u'pluginsconfig')
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
if len(profile) > 0:
|
|
|
|
conffile += u'.' + profile
|
|
|
|
pidfile += u'.' + profile
|
2007-09-29 22:51:01 +02:00
|
|
|
secretsfile += u'.' + profile
|
2008-07-29 21:09:28 +02:00
|
|
|
pluginsconfdir += u'.' + profile
|
2007-07-26 01:04:15 +02:00
|
|
|
pidfile += u'.pid'
|
2010-02-04 20:08:30 +01:00
|
|
|
self.add('CONFIG_FILE', TYPE_CONFIG, conffile)
|
2010-02-07 19:33:09 +01:00
|
|
|
self.add('PID_FILE', TYPE_CACHE, pidfile)
|
2010-02-04 20:08:30 +01:00
|
|
|
self.add('SECRETS_FILE', TYPE_DATA, secretsfile)
|
2010-02-08 22:25:41 +01:00
|
|
|
self.add('PLUGINS_CONFIG_DIR', TYPE_CONFIG, pluginsconfdir)
|
2006-12-22 14:49:38 +01:00
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
gajimpaths = ConfigPaths()
|
2008-07-29 21:49:31 +02:00
|
|
|
|
2008-08-15 05:20:23 +02:00
|
|
|
# vim: se ts=3:
|