configpaths.py 3.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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.
23 24 25 26

def fse(s):
	'''Convert from filesystem encoding if not already Unicode'''
	return unicode(s, sys.getfilesystemencoding())
27

28 29 30 31 32
def windowsify(s):
	if os.name == 'nt':
		return s.capitalize()
	return s

33
class ConfigPaths:
jimpp's avatar
jimpp committed
34 35 36
	def __init__(self, root=None):
		self.root = root
		self.paths = {}
37

jimpp's avatar
jimpp committed
38
		if self.root is None:
39 40 41 42 43 44 45
			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.
46
					self.root = os.path.join(fse(os.environ[u'appdata']), u'Gajim')
47 48
				except KeyError:
					# win9x, in cwd
49
					self.root = u'.'
50
			else: # Unices
51 52
				# Pass in an Unicode string, and hopefully get one back.
				self.root = os.path.expanduser(u'~/.gajim')
53

jimpp's avatar
jimpp committed
54 55
	def add_from_root(self, name, path):
		self.paths[name] = (True, path)
56

jimpp's avatar
jimpp committed
57 58
	def add(self, name, path):
		self.paths[name] = (False, path)
59

jimpp's avatar
jimpp committed
60 61
	def __getitem__(self, key):
		relative, path = self.paths[key]
62 63
		if not relative:
			return path
jimpp's avatar
jimpp committed
64
		return os.path.join(self.root, path)
65

jimpp's avatar
jimpp committed
66
	def get(self, key, default=None):
67
		try:
jimpp's avatar
jimpp committed
68
			return self[key]
69 70 71
		except KeyError:
			return default

jimpp's avatar
jimpp committed
72 73 74
	def iteritems(self):
		for key in self.paths.iterkeys():
			yield (key, self[key])
75

76
	def init(self, root = None):
Yann Leboulanger's avatar
Yann Leboulanger committed
77
		if root is not None:
78
			self.root = root
79

80
		# LOG is deprecated
81
		k = ( 'LOG',   'LOG_DB',   'VCARD',   'AVATAR',   'MY_EMOTS',
82
			'MY_ICONSETS', 'MY_CACERTS')
83
		v = (u'logs', u'logs.db', u'vcards', u'avatars', u'emoticons',
84
			u'iconsets',  u'cacerts.pem')
85

86 87
		if os.name == 'nt':
			v = map(lambda x: x.capitalize(), v)
88

89 90
		for n, p in zip(k, v):
			self.add_from_root(n, p)
91

92 93 94
		self.add('DATA', os.path.join(u'..', windowsify(u'data')))
		self.add('HOME', fse(os.path.expanduser('~')))
		self.add('TMP', fse(tempfile.gettempdir()))
95

96 97 98 99 100
		try:
			import svn_config
			svn_config.configure(self)
		except (ImportError, AttributeError):
			pass
101

102 103
		# for k, v in paths.iteritems():
		# 	print "%s: %s" % (repr(k), repr(v))
104

105
	def init_profile(self, profile = ''):
106 107
		conffile = windowsify(u'config')
		pidfile = windowsify(u'gajim')
108
		secretsfile = windowsify(u'secrets')
109

110 111 112
		if len(profile) > 0:
			conffile += u'.' + profile
			pidfile += u'.' + profile
113
			secretsfile += u'.' + profile
114 115 116
		pidfile += u'.pid'
		self.add_from_root('CONFIG_FILE', conffile)
		self.add_from_root('PID_FILE', pidfile)
117
		self.add_from_root('SECRETS_FILE', secretsfile)
118

119 120
		# for k, v in paths.iteritems():
		# 	print "%s: %s" % (repr(k), repr(v))
121

122
gajimpaths = ConfigPaths()