2006-11-04 19:15:38 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
# 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):
|
|
|
|
'''Convert from filesystem encoding if not already Unicode'''
|
|
|
|
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:
|
2006-11-21 19:46:33 +01:00
|
|
|
def __init__(self, root=None):
|
|
|
|
self.root = root
|
|
|
|
self.paths = {}
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2006-11-21 19:46:33 +01:00
|
|
|
if self.root is None:
|
2006-11-04 19:15:38 +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.
|
2007-01-02 18:56:26 +01:00
|
|
|
self.root = os.path.join(fse(os.environ[u'appdata']), u'Gajim')
|
2006-11-04 19:15:38 +01:00
|
|
|
except KeyError:
|
|
|
|
# win9x, in cwd
|
2007-01-06 16:29:09 +01:00
|
|
|
self.root = u'.'
|
2006-11-04 19:15:38 +01:00
|
|
|
else: # Unices
|
2007-01-02 18:56:26 +01:00
|
|
|
# Pass in an Unicode string, and hopefully get one back.
|
|
|
|
self.root = os.path.expanduser(u'~/.gajim')
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2006-11-21 19:46:33 +01:00
|
|
|
def add_from_root(self, name, path):
|
|
|
|
self.paths[name] = (True, path)
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2006-11-21 19:46:33 +01:00
|
|
|
def add(self, name, path):
|
|
|
|
self.paths[name] = (False, path)
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2006-11-21 19:46:33 +01:00
|
|
|
def __getitem__(self, key):
|
|
|
|
relative, path = self.paths[key]
|
2006-11-04 19:15:38 +01:00
|
|
|
if not relative:
|
|
|
|
return path
|
2006-11-21 19:46:33 +01:00
|
|
|
return os.path.join(self.root, 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
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
def init(self, root = None):
|
2007-07-26 01:06:33 +02:00
|
|
|
if root is not None:
|
2007-07-26 01:04:15 +02:00
|
|
|
self.root = root
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
# LOG is deprecated
|
2007-08-06 00:57:04 +02:00
|
|
|
k = ( 'LOG', 'LOG_DB', 'VCARD', 'AVATAR', 'MY_EMOTS',
|
|
|
|
'MY_ICONSETS' )
|
|
|
|
v = (u'logs', u'logs.db', u'vcards', u'avatars', u'emoticons',
|
|
|
|
u'iconsets')
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
if os.name == 'nt':
|
|
|
|
v = map(lambda x: x.capitalize(), v)
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
for n, p in zip(k, v):
|
|
|
|
self.add_from_root(n, p)
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
self.add('DATA', os.path.join(u'..', windowsify(u'data')))
|
|
|
|
self.add('HOME', fse(os.path.expanduser('~')))
|
|
|
|
self.add('TMP', fse(tempfile.gettempdir()))
|
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
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
# for k, v in paths.iteritems():
|
|
|
|
# print "%s: %s" % (repr(k), repr(v))
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2007-08-20 00:51:40 +02: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')
|
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
|
|
|
|
pidfile += u'.pid'
|
|
|
|
self.add_from_root('CONFIG_FILE', conffile)
|
|
|
|
self.add_from_root('PID_FILE', pidfile)
|
2007-08-26 00:42:35 +02:00
|
|
|
self.add_from_root('SECRETS_FILE', secretsfile)
|
2006-11-04 19:15:38 +01:00
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
# for k, v in paths.iteritems():
|
|
|
|
# print "%s: %s" % (repr(k), repr(v))
|
2006-12-22 14:49:38 +01:00
|
|
|
|
2007-07-26 01:04:15 +02:00
|
|
|
gajimpaths = ConfigPaths()
|