Mercurial > libervia-backend
annotate libervia/backend/tools/config.py @ 4303:a7ec325246fb
component email-gateway: first draft:
Initial implementation of the Email Gateway.
This component uses XEP-0100 for registration. Upon registration and subsequent startups,
a connection is made to registered IMAP services, and incoming emails (in `INBOX`
mailboxes) are immediately forwarded as XMPP messages.
In the opposite direction, an SMTP connection is established to send emails on incoming
XMPP messages.
rel 449
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 06 Sep 2024 18:07:17 +0200 |
parents | 0d7bb4df2343 |
children |
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 |
3479 | 5 # Copyright (C) 2009-2021 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 |
3634
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
26 from typing import Any |
3031
98d1f34ce5b9
tools (config), memory: renamed SafeConfigParser following Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
27 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
|
28 from xdg import BaseDirectory |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
29 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
30 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
31 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
32 from libervia.backend.core import exceptions |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
33 |
2835
6aa22011bc6d
tools (config): log error message if config can't be read
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
34 log = getLogger(__name__) |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
35 |
2965
121c4a2a567c
core (config): if flatpak is detected, config is also looked after in /app
Goffi <goffi@goffi.org>
parents:
2836
diff
changeset
|
36 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3634
diff
changeset
|
37 def fix_config_option(section, option, value, silent=True): |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
38 """Force a configuration option value |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
39 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
40 the option will be written in the first found user config file, a new user |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
41 config will be created if none is found. |
1046
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 @param section (str): the config section |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
44 @param option (str): the config option |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
45 @param value (str): the new value |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
46 @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
|
47 """ |
3031
98d1f34ce5b9
tools (config), memory: renamed SafeConfigParser following Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
48 config = ConfigParser() |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
49 target_file = None |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
50 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
|
51 # 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
|
52 # if it's a user personal file... |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
53 if not silent: |
3028 | 54 log.debug(_("Testing file %s") % file_) |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
55 if os.path.isfile(file_): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
56 if file_.startswith(os.path.expanduser("~")): |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
57 config.read([file_]) |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
58 target_file = file_ |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
59 break |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
60 if not target_file: |
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
61 # ... otherwise we create a new config file for that user |
3482
acb28399480f
tools (config): show a warning when no config file has been found + don't use hardcoded filename in `fixConfigOption`
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
62 target_file = ( |
acb28399480f
tools (config): show a warning when no config file has been found + don't use hardcoded filename in `fixConfigOption`
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
63 f"{BaseDirectory.save_config_path(C.APP_NAME_FILE)}/{C.APP_NAME_FILE}.conf" |
acb28399480f
tools (config): show a warning when no config file has been found + don't use hardcoded filename in `fixConfigOption`
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
64 ) |
1056
a5cfa9bb4541
tools (config): fixConfigOption creates the section if it doesn't exist
souliane <souliane@mailoo.org>
parents:
1046
diff
changeset
|
65 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
|
66 config.add_section(section) |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
67 config.set(section, option, value) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
68 with open(target_file, "wb") as configfile: |
1046
a874a79ad0f5
tools: add missing file src/tools/config.py
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
69 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
|
70 if not silent: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
71 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
|
72 value = "******" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
73 log.warning( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
74 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
75 "Config auto-update: {option} set to {value} in the file " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
76 "{config_file}." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
77 ).format(option=option, value=value, config_file=target_file) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
78 ) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
80 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3634
diff
changeset
|
81 def parse_main_conf(log_filenames=False): |
3148
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
82 """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
|
83 |
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
84 @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
|
85 """ |
3031
98d1f34ce5b9
tools (config), memory: renamed SafeConfigParser following Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
86 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
|
87 try: |
3148
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
88 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
|
89 except Exception as e: |
3028 | 90 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
|
91 else: |
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
92 if log_filenames: |
3482
acb28399480f
tools (config): show a warning when no config file has been found + don't use hardcoded filename in `fixConfigOption`
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
93 if filenames: |
acb28399480f
tools (config): show a warning when no config file has been found + don't use hardcoded filename in `fixConfigOption`
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
94 log.info( |
acb28399480f
tools (config): show a warning when no config file has been found + don't use hardcoded filename in `fixConfigOption`
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
95 _("Configuration was read from: {filenames}").format( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
96 filenames=", ".join(filenames) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
97 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
98 ) |
3482
acb28399480f
tools (config): show a warning when no config file has been found + don't use hardcoded filename in `fixConfigOption`
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
99 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
100 log.warning(_("No configuration file found, using default settings")) |
3148
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
101 |
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
|
102 return config |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
103 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
104 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3634
diff
changeset
|
105 def config_get(config, section, name, default=None): |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
106 """Get a configuration option |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
107 |
3031
98d1f34ce5b9
tools (config), memory: renamed SafeConfigParser following Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
108 @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
|
109 @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
|
110 @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
|
111 @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
|
112 @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
|
113 @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
|
114 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
|
115 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
|
116 """ |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
117 if not section: |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
118 section = DEFAULTSECT |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
119 |
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
120 try: |
3028 | 121 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
|
122 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
|
123 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
|
124 raise e |
1234
9c17bd37e6e5
core: better management of default value in getConfig
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
125 return default |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1056
diff
changeset
|
126 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
127 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
|
128 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
|
129 # 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
|
130 elif name.endswith("_list"): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
131 value = next( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
132 csv.reader([value], delimiter=",", quotechar='"', skipinitialspace=True) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
133 ) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
134 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
|
135 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
|
136 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
|
137 except ValueError as e: |
3028 | 138 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
|
139 if not isinstance(value, dict): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
140 raise exceptions.ParsingError( |
3028 | 141 "{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
|
142 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
143 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
|
144 try: |
d1153ce68ca0
tools (config): complexe data can now be set using json and the "_json" suffix
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
145 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
|
146 except ValueError as e: |
3028 | 147 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
|
148 return value |
3634
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
149 |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
150 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3634
diff
changeset
|
151 def get_conf( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
152 conf: ConfigParser, prefix: str, section: str, name: str, default: Any |
3634
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
153 ) -> Any: |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
154 """Get configuration value from environment or config file |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
155 |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
156 @param str: prefix to use for the varilable name (see `name` below) |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
157 @param section: config section to use |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
158 @param name: unsuffixed name. |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
159 For environment variable, `LIBERVIA_<prefix>_` will be prefixed (and name |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
160 will be set to uppercase). |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
161 For config file, `<prefix>_` will be prefixed (and DEFAULT section will be |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
162 used). |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
163 Environment variable has priority over config values. If Environment variable |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
164 is set but empty string, config value will be used. |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
165 @param default: default value to use if varilable is set neither in environment, |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
166 nor in config |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
167 """ |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
168 # XXX: This is a temporary method until parameters are refactored |
3c7a64d6f49f
bridge: bridge can now be set using environment variable:
Goffi <goffi@goffi.org>
parents:
3482
diff
changeset
|
169 value = os.getenv(f"LIBERVIA_{prefix}_{name}".upper()) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3634
diff
changeset
|
170 return value or config_get(conf, section, f"{prefix}_{name}", default) |