Mercurial > libervia-backend
annotate src/tools/config.py @ 1320:3012c2f15dae frontends_multi_profiles
merges souliane commits
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 09 Feb 2015 21:40:45 +0100 |
parents | 251ae99a6c0e |
children | 069ad98b360d |
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 |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
29 from ConfigParser import SafeConfigParser, DEFAULTSECT, NoOptionError, NoSectionError |
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 |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
32 import csv |
1046
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 |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
35 def fixConfigOption(section, option, value, silent=True): |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
36 """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
|
37 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
|
38 |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
39 @param section (str): the config section |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
40 @param option (str): the config option |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
41 @param value (str): the new value |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
42 @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
|
43 """ |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
44 config = SafeConfigParser() |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
45 target_file = None |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
46 for file_ in C.CONFIG_FILES[::-1]: |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
47 # 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
|
48 if not silent: |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
49 log.debug(_("Testing file %s") % file_) |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
50 if os.path.isfile(file_): |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
51 if file_.startswith(os.path.expanduser('~')): |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
52 config.read([file_]) |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
53 target_file = file_ |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
54 break |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
55 if not target_file: |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
56 # ... 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
|
57 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
|
58 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
|
59 config.add_section(section) |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
60 config.set(section, option, value) |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
61 with open(target_file, 'wb') as configfile: |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
62 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
|
63 if not silent: |
1236
251ae99a6c0e
core (config): fixed a bad option check
Goffi <goffi@goffi.org>
parents:
1235
diff
changeset
|
64 if option in ('passphrase',): # list here the options storing a password |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
65 value = '******' |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
66 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
|
67 {'option': option, 'value': value, 'config_file': target_file}) |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
68 |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
69 |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
70 def getConfig(config, section, name, default=None): |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
71 """Get a configuration option |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
72 |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
73 @param config (SafeConfigParser): the configuration instance |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
74 @param section (str): section of the config file (None or '' for DEFAULT) |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
75 @param name (str): name of the option |
1235
80d2ed788b40
core (config): added the Exception default value which raise an exception instead of returning the default in getConfig
Goffi <goffi@goffi.org>
parents:
1234
diff
changeset
|
76 @param default: value to use if not found, or Exception to raise an exception |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
77 @return: str, list or dict |
1235
80d2ed788b40
core (config): added the Exception default value which raise an exception instead of returning the default in getConfig
Goffi <goffi@goffi.org>
parents:
1234
diff
changeset
|
78 @raise: NoOptionError if option is not present and default is Exception |
80d2ed788b40
core (config): added the Exception default value which raise an exception instead of returning the default in getConfig
Goffi <goffi@goffi.org>
parents:
1234
diff
changeset
|
79 NoSectionError if section doesn't exists and default is Exception |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
80 """ |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
81 if not section: |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
82 section = DEFAULTSECT |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
83 |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
84 try: |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
85 value = config.get(section, name) |
1235
80d2ed788b40
core (config): added the Exception default value which raise an exception instead of returning the default in getConfig
Goffi <goffi@goffi.org>
parents:
1234
diff
changeset
|
86 except (NoOptionError, NoSectionError) as e: |
80d2ed788b40
core (config): added the Exception default value which raise an exception instead of returning the default in getConfig
Goffi <goffi@goffi.org>
parents:
1234
diff
changeset
|
87 if default is Exception: |
80d2ed788b40
core (config): added the Exception default value which raise an exception instead of returning the default in getConfig
Goffi <goffi@goffi.org>
parents:
1234
diff
changeset
|
88 raise e |
1234
9c17bd37e6e5
core: better management of default value in getConfig
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
89 return default |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
90 |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
91 if name.endswith('_path') or name.endswith('_dir'): |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
92 value = os.path.expanduser(value) |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
93 # thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873) |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
94 elif name.endswith('_list'): |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
95 value = csv.reader([value], delimiter=',', quotechar='"').next() |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
96 elif name.endswith('_dict'): |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
97 value = dict(csv.reader([item], delimiter=':', quotechar='"').next() |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
98 for item in csv.reader([value], delimiter=',', quotechar='"').next()) |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
99 return value |