annotate src/tools/config.py @ 1059:b2b9c184033f

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