[junglecow] refactor paths and directories in a single file. fixes #2629

This commit is contained in:
Yann Leboulanger 2006-11-04 18:15:38 +00:00
parent 1ea1be854b
commit 83a5014b14
4 changed files with 134 additions and 59 deletions

115
src/common/configpaths.py Normal file
View File

@ -0,0 +1,115 @@
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.
def fse(s):
'''Convert from filesystem encoding if not already Unicode'''
return unicode(s, sys.getfilesystemencoding())
class ConfigPaths:
def __init__(this, root=None):
this.root = root
this.paths = {}
if this.root is None:
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.
this.root = os.path.join(fse(os.environ[u'appdata']), u'Gajim')
except KeyError:
# win9x, in cwd
this.root = u''
else: # Unices
# Pass in an Unicode string, and hopefully get one back.
this.root = os.path.expanduser(u'~/.gajim')
def add_from_root(this, name, path):
this.paths[name] = (True, path)
def add(this, name, path):
this.paths[name] = (False, path)
def __getitem__(this, key):
relative, path = this.paths[key]
if not relative:
return path
return os.path.join(this.root, path)
def get(this, key, default=None):
try:
return this[key]
except KeyError:
return default
def iteritems(this):
for key in this.paths.iterkeys():
yield (key, this[key])
def windowsify(s):
if os.name == 'nt':
return s.capitalize()
return s
def init():
paths = ConfigPaths()
# LOG is deprecated
k = ( 'LOG', 'LOG_DB', 'VCARD', 'AVATAR', 'MY_EMOTS' )
v = (u'logs', u'logs.db', u'vcards', u'avatars', u'emoticons')
if os.name == 'nt':
v = map(lambda x: x.capitalize(), v)
for n, p in zip(k, v):
paths.add_from_root(n, p)
paths.add('DATA', os.path.join(u'..', windowsify(u'data')))
paths.add('HOME', os.path.expanduser(u'~'))
paths.add('TMP', fse(tempfile.gettempdir()))
try:
import svn_config
svn_config.configure(paths)
except (ImportError, AttributeError):
pass
#for k, v in paths.iteritems():
# print "%s: %s" % (k, v)
return paths
gajimpaths = init()
def init_profile(profile, paths=gajimpaths):
conffile = windowsify(u'config')
pidfile = windowsify(u'gajim')
if len(profile) > 0:
conffile += u'.' + profile
pidfile += u'.' + profile
pidfile += u'.pid'
paths.add_from_root('CONFIG_FILE', conffile)
paths.add_from_root('PID_FILE', pidfile)

View File

@ -66,38 +66,17 @@ log.addHandler(h)
import logger
logger = logger.Logger() # init the logger
if os.name == 'nt':
DATA_DIR = os.path.join('..', 'data')
try:
# Documents and Settings\[User Name]\Application Data\Gajim
LOGPATH = os.path.join(os.environ['appdata'], 'Gajim', 'Logs') # deprecated
VCARD_PATH = os.path.join(os.environ['appdata'], 'Gajim', 'Vcards')
AVATAR_PATH = os.path.join(os.environ['appdata'], 'Gajim', 'Avatars')
MY_EMOTS_PATH = os.path.join(os.environ['appdata'], 'Gajim', 'Emoticons')
except KeyError:
# win9x, in cwd
LOGPATH = 'Logs' # deprecated
VCARD_PATH = 'Vcards'
AVATAR_PATH = 'Avatars'
MY_EMOTS_PATH = 'Emoticons'
else: # Unices
DATA_DIR = '../data'
LOGPATH = os.path.expanduser('~/.gajim/logs') # deprecated
VCARD_PATH = os.path.expanduser('~/.gajim/vcards')
AVATAR_PATH = os.path.expanduser('~/.gajim/avatars')
MY_EMOTS_PATH = os.path.expanduser('~/.gajim/emoticons')
import configpaths
gajimpaths = configpaths.gajimpaths
HOME_DIR = os.path.expanduser('~')
TMP = tempfile.gettempdir()
LOGPATH = gajimpaths['LOG'] # deprecated
VCARD_PATH = gajimpaths['VCARD']
AVATAR_PATH = gajimpaths['AVATAR']
MY_EMOTS_PATH = gajimpaths['MY_EMOTS']
TMP = gajimpaths['TMP']
DATA_DIR = gajimpaths['DATA']
HOME_DIR = gajimpaths['HOME']
try:
LOGPATH = LOGPATH.decode(sys.getfilesystemencoding())
VCARD_PATH = VCARD_PATH.decode(sys.getfilesystemencoding())
TMP = TMP.decode(sys.getfilesystemencoding())
AVATAR_PATH = AVATAR_PATH.decode(sys.getfilesystemencoding())
MY_EMOTS_PATH = MY_EMOTS_PATH.decode(sys.getfilesystemencoding())
except:
pass
try:
LANG = locale.getdefaultlocale()[0] # en_US, fr_FR, el_GR etc..
except (ValueError, locale.Error):

View File

@ -29,20 +29,8 @@ except ImportError:
except ImportError:
raise exceptions.PysqliteNotAvailable
if os.name == 'nt':
try:
# Documents and Settings\[User Name]\Application Data\Gajim\logs.db
LOG_DB_PATH = os.path.join(os.environ['appdata'], 'Gajim', 'logs.db')
except KeyError:
# win9x, ./logs.db
LOG_DB_PATH = 'logs.db'
else: # Unices
LOG_DB_PATH = os.path.expanduser('~/.gajim/logs.db')
try:
LOG_DB_PATH = LOG_DB_PATH.decode(sys.getfilesystemencoding())
except:
pass
import configpaths
LOG_DB_PATH = configpaths.gajimpaths['LOG_DB']
class Constants:
def __init__(self):

View File

@ -131,23 +131,16 @@ for o, a in opts:
elif o in ('-p', '--profile'): # gajim --profile name
profile = a
pid_filename = os.path.expanduser('~/.gajim/gajim')
config_filename = os.path.expanduser('~/.gajim/config')
if os.name == 'nt':
try:
# Documents and Settings\[User Name]\Application Data\Gajim\logs
config_filename = os.environ['appdata'] + '/Gajim/config'
pid_filename = os.environ['appdata'] + '/Gajim/gajim'
except KeyError:
# win9x so ./config
config_filename = 'config'
pid_filename = 'gajim'
import locale
profile = unicode(profile, locale.getpreferredencoding())
if profile:
config_filename += '.%s' % profile
pid_filename += '.%s' % profile
import common.configpaths
common.configpaths.init_profile(profile)
gajimpaths = common.configpaths.gajimpaths
pid_filename = gajimpaths['PID_FILE']
config_filename = gajimpaths['CONFIG_FILE']
pid_filename += '.pid'
import dialogs
if os.path.exists(pid_filename):
path_to_file = os.path.join(gajim.DATA_DIR, 'pixmaps/gajim.png')