comparison src/tools/config.py @ 1046:a874a79ad0f5

tools: add missing file src/tools/config.py
author souliane <souliane@mailoo.org>
date Fri, 23 May 2014 10:03:33 +0200
parents
children a5cfa9bb4541
comparison
equal deleted inserted replaced
1045:65fffdcb97f1 1046:a874a79ad0f5
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # SAT: a jabber client
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013, 2014 Adrien Cossa (souliane@mailoo.org)
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 """ Configuration related useful methods """
22
23 from sat.core.log import getLogger
24 log = getLogger(__name__)
25
26 from sat.core.constants import Const as C
27 from sat.core.i18n import _
28
29 from ConfigParser import SafeConfigParser
30 from xdg import BaseDirectory
31 import os
32
33
34 def fixConfigOption(section, option, value, silent=True):
35 """Force a configuration option value, writing it in the first found user
36 config file, eventually creating a new user config file if none is found.
37
38 @param section (str): the config section
39 @param option (str): the config option
40 @param value (str): the new value
41 @param silent (boolean): toggle logging output (must be True when called from sat.sh)
42 """
43 config = SafeConfigParser()
44 target_file = None
45 for file_ in C.CONFIG_FILES[::-1]:
46 # we will eventually update the existing file with the highest priority, if it's a user personal file...
47 if not silent:
48 log.debug(_("Testing file %s") % file_)
49 if os.path.isfile(file_):
50 if file_.startswith(os.path.expanduser('~')):
51 config.read([file_])
52 target_file = file_
53 break
54 if not target_file:
55 # ... otherwise we create a new config file for that user
56 target_file = BaseDirectory.save_config_path('sat') + '/sat.conf'
57 config.set(section, option, value)
58 with open(target_file, 'wb') as configfile:
59 config.write(configfile) # for the next time that user launches sat
60 if not silent:
61 if option in ('passphrase'): # list here the options storing a password
62 value = '******'
63 log.warning(_("Config auto-update: %(option)s set to %(value)s in the file %(config_file)s") %
64 {'option': option, 'value': value, 'config_file': target_file})