Welcome to mirror list, hosted at ThFree Co, Russian Federation.

configpaths.py « common « src - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ab153f7d6c2a0bf31f23409ebe4609c18cd9a99e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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())

def windowsify(s):
	if os.name == 'nt':
		return s.capitalize()
	return s

class ConfigPaths:
	def __init__(self, root=None):
		self.root = root
		self.paths = {}

		if self.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.
					self.root = os.path.join(fse(os.environ[u'appdata']), u'Gajim')
				except KeyError:
					# win9x, in cwd
					self.root = u'.'
			else: # Unices
				# Pass in an Unicode string, and hopefully get one back.
				self.root = os.path.expanduser(u'~/.gajim')

	def add_from_root(self, name, path):
		self.paths[name] = (True, path)

	def add(self, name, path):
		self.paths[name] = (False, path)

	def __getitem__(self, key):
		relative, path = self.paths[key]
		if not relative:
			return path
		return os.path.join(self.root, path)

	def get(self, key, default=None):
		try:
			return self[key]
		except KeyError:
			return default

	def iteritems(self):
		for key in self.paths.iterkeys():
			yield (key, self[key])

	def init(self, root = None):
		if root is not None:
			self.root = root

		# LOG is deprecated
		k = ( 'LOG',   'LOG_DB',   'VCARD',   'AVATAR',   'MY_EMOTS',
			'MY_ICONSETS', 'MY_MOOD_ICONSETS',
			'MY_ACTIVITY_ICONSETS', 'MY_CACERTS')
		v = (u'logs', u'logs.db', u'vcards', u'avatars', u'emoticons',
			u'iconsets',  u'moods', u'activities', u'cacerts.pem')

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

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

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

		try:
			import svn_config
			svn_config.configure(self)
		except (ImportError, AttributeError):
			pass

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

	def init_profile(self, profile = ''):
		conffile = windowsify(u'config')
		pidfile = windowsify(u'gajim')
		secretsfile = windowsify(u'secrets')

		if len(profile) > 0:
			conffile += u'.' + profile
			pidfile += u'.' + profile
			secretsfile += u'.' + profile
		pidfile += u'.pid'
		self.add_from_root('CONFIG_FILE', conffile)
		self.add_from_root('PID_FILE', pidfile)
		self.add_from_root('SECRETS_FILE', secretsfile)

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

gajimpaths = ConfigPaths()

# vim: se ts=3: