Mercurial > libervia-backend
annotate sat/tools/config.py @ 3274:430204a3cc10
tools (common/template): new `icon_from_client` method:
this method is available from template, and returns the name of the icon to use according
to disco client data.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 18 May 2020 23:41:36 +0200 |
parents | 60a9e47ef988 |
children | a3639d6d9643 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
1046
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 |
3136 | 5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) |
1766 | 6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) |
1046
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 |
2835
6aa22011bc6d
tools (config): log error message if config can't be read
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
23 import os |
6aa22011bc6d
tools (config): log error message if config can't be read
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
24 import csv |
6aa22011bc6d
tools (config): log error message if config can't be read
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
25 import json |
3031
98d1f34ce5b9
tools (config), memory: renamed SafeConfigParser following Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
26 from configparser import ConfigParser, DEFAULTSECT, NoOptionError, NoSectionError |
2835
6aa22011bc6d
tools (config): log error message if config can't be read
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
27 from xdg import BaseDirectory |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
28 from sat.core.log import getLogger |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
29 from sat.core.constants import Const as C |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
30 from sat.core.i18n import _ |
1835
5b8a859d5bb4
core (config): getConfig now returns unicode and raise exceptions.ParsingError in case of parsing problem
Goffi <goffi@goffi.org>
parents:
1833
diff
changeset
|
31 from sat.core import exceptions |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
32 |
2835
6aa22011bc6d
tools (config): log error message if config can't be read
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
33 log = getLogger(__name__) |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
34 |
2965
121c4a2a567c
core (config): if flatpak is detected, config is also looked after in /app
Goffi <goffi@goffi.org>
parents:
2836
diff
changeset
|
35 |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
36 def fixConfigOption(section, option, value, silent=True): |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
37 """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
|
38 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
|
39 |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
40 @param section (str): the config section |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
41 @param option (str): the config option |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
42 @param value (str): the new value |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
43 @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
|
44 """ |
3031
98d1f34ce5b9
tools (config), memory: renamed SafeConfigParser following Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
45 config = ConfigParser() |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
46 target_file = None |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
47 for file_ in C.CONFIG_FILES[::-1]: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
48 # we will eventually update the existing file with the highest priority, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
49 # if it's a user personal file... |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
50 if not silent: |
3028 | 51 log.debug(_("Testing file %s") % file_) |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
52 if os.path.isfile(file_): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 if file_.startswith(os.path.expanduser("~")): |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
54 config.read([file_]) |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
55 target_file = file_ |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
56 break |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
57 if not target_file: |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
58 # ... otherwise we create a new config file for that user |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
59 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
|
60 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
|
61 config.add_section(section) |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
62 config.set(section, option, value) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 with open(target_file, "wb") as configfile: |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
64 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
|
65 if not silent: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 if option in ("passphrase",): # list here the options storing a password |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
67 value = "******" |
3028 | 68 log.warning(_("Config auto-update: {option} set to {value} in the file " |
69 "{config_file}.").format(option=option, value=value, | |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
70 config_file=target_file)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
71 |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
72 |
3053
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
73 # XXX: tmp update code, will be removed in the future |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
74 # When you remove this, please add the default value for |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
75 # 'local_dir' in sat.core.constants.Const.DEFAULT_CONFIG |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
76 def fixLocalDir(silent=True): |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
77 """Retro-compatibility with the previous local_dir default value. |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
78 |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
79 @param silent (boolean): toggle logging output (must be True when called from sat.sh) |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
80 """ |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
81 user_config = ConfigParser() |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
82 try: |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
83 user_config.read(C.CONFIG_FILES) |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
84 except: |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
85 pass # file is readable but its structure if wrong |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
86 try: |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
87 current_value = user_config.get("DEFAULT", "local_dir") |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
88 except (NoOptionError, NoSectionError): |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
89 current_value = "" |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
90 if current_value: |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
91 return # nothing to do |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
92 old_default = "~/.sat" |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
93 if os.path.isfile(os.path.expanduser(old_default) + "/" + C.SAVEFILE_DATABASE): |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
94 if not silent: |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
95 log.warning(_( |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
96 "A database has been found in the default local_dir for previous " |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
97 "versions (< 0.5)" |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
98 )) |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
99 fixConfigOption("", "local_dir", old_default, silent) |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
100 |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3031
diff
changeset
|
101 |
3148
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
102 def parseMainConf(log_filenames=False): |
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
103 """Look for main .ini configuration file, and parse it |
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
104 |
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
105 @param log_filenames(bool): if True, log filenames of read config files |
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
106 """ |
3031
98d1f34ce5b9
tools (config), memory: renamed SafeConfigParser following Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
107 config = ConfigParser(defaults=C.DEFAULT_CONFIG) |
1859
ac2ac7fe8a9b
core (memory, config): moved parseMainConf to tools/config so it can be used by frontends too
Goffi <goffi@goffi.org>
parents:
1835
diff
changeset
|
108 try: |
3148
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
109 filenames = config.read(C.CONFIG_FILES) |
2835
6aa22011bc6d
tools (config): log error message if config can't be read
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
110 except Exception as e: |
3028 | 111 log.error(_("Can't read main config: {msg}").format(msg=e), exc_info=True) |
3148
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
112 else: |
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
113 if log_filenames: |
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
114 log.info( |
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
115 _("Configuration was read from: {filenames}").format( |
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
116 filenames=', '.join(filenames))) |
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
117 |
1859
ac2ac7fe8a9b
core (memory, config): moved parseMainConf to tools/config so it can be used by frontends too
Goffi <goffi@goffi.org>
parents:
1835
diff
changeset
|
118 return config |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
119 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
120 |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
121 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
|
122 """Get a configuration option |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
123 |
3031
98d1f34ce5b9
tools (config), memory: renamed SafeConfigParser following Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
124 @param config (ConfigParser): the configuration instance |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
125 @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
|
126 @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
|
127 @param default: value to use if not found, or Exception to raise an exception |
1835
5b8a859d5bb4
core (config): getConfig now returns unicode and raise exceptions.ParsingError in case of parsing problem
Goffi <goffi@goffi.org>
parents:
1833
diff
changeset
|
128 @return (unicode, list, dict): parsed value |
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
|
129 @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
|
130 NoSectionError if section doesn't exists and default is Exception |
1835
5b8a859d5bb4
core (config): getConfig now returns unicode and raise exceptions.ParsingError in case of parsing problem
Goffi <goffi@goffi.org>
parents:
1833
diff
changeset
|
131 exceptions.ParsingError error while parsing value |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
132 """ |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
133 if not section: |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
134 section = DEFAULTSECT |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
135 |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
136 try: |
3028 | 137 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
|
138 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
|
139 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
|
140 raise e |
1234
9c17bd37e6e5
core: better management of default value in getConfig
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
141 return default |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
142 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
143 if name.endswith("_path") or name.endswith("_dir"): |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
144 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
|
145 # thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 elif name.endswith("_list"): |
3028 | 147 value = next(csv.reader( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
148 [value], delimiter=",", quotechar='"', skipinitialspace=True |
3028 | 149 )) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
150 elif name.endswith("_dict"): |
1835
5b8a859d5bb4
core (config): getConfig now returns unicode and raise exceptions.ParsingError in case of parsing problem
Goffi <goffi@goffi.org>
parents:
1833
diff
changeset
|
151 try: |
5b8a859d5bb4
core (config): getConfig now returns unicode and raise exceptions.ParsingError in case of parsing problem
Goffi <goffi@goffi.org>
parents:
1833
diff
changeset
|
152 value = json.loads(value) |
5b8a859d5bb4
core (config): getConfig now returns unicode and raise exceptions.ParsingError in case of parsing problem
Goffi <goffi@goffi.org>
parents:
1833
diff
changeset
|
153 except ValueError as e: |
3028 | 154 raise exceptions.ParsingError("Error while parsing data: {}".format(e)) |
1833
a123e881f9e5
core (config): _dict values are now handled with json syntax
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
155 if not isinstance(value, dict): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
156 raise exceptions.ParsingError( |
3028 | 157 "{name} value is not a dict: {value}".format(name=name, value=value) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
158 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
159 elif name.endswith("_json"): |
2451
d1153ce68ca0
tools (config): complexe data can now be set using json and the "_json" suffix
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
160 try: |
d1153ce68ca0
tools (config): complexe data can now be set using json and the "_json" suffix
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
161 value = json.loads(value) |
d1153ce68ca0
tools (config): complexe data can now be set using json and the "_json" suffix
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
162 except ValueError as e: |
3028 | 163 raise exceptions.ParsingError("Error while parsing data: {}".format(e)) |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
164 return value |