Method: flask.config.Config.from_prefixed_env
Calls: 3, Exceptions: 3, Paths: 2Back
Path 1: 2 calls (0.67)
'FLASK' (1) 'NOT_FLASK' (1)
loads def (2)
True (2)
JSONDecodeError (2)
1def from_prefixed_env(
2 self, prefix: str = "FLASK", *, loads: t.Callable[[str], t.Any] = json.loads
3 ) -> bool:
4 """Load any environment variables that start with ``FLASK_``,
5 dropping the prefix from the env key for the config key. Values
6 are passed through a loading function to attempt to convert them
7 to more specific types than strings.
8
9 Keys are loaded in :func:`sorted` order.
10
11 The default loading function attempts to parse values as any
12 valid JSON type, including dicts and lists.
13
14 Specific items in nested dicts can be set by separating the
15 keys with double underscores (``__``). If an intermediate key
16 doesn't exist, it will be initialized to an empty dict.
17
18 :param prefix: Load env vars that start with this prefix,
19 separated with an underscore (``_``).
20 :param loads: Pass each string value to this function and use
21 the returned value as the config value. If any error is
22 raised it is ignored and the value remains a string. The
23 default is :func:`json.loads`.
24
25 .. versionadded:: 2.1
26 """
27 prefix = f"{prefix}_"
28 len_prefix = len(prefix)
29
30 for key in sorted(os.environ):
31 if not key.startswith(prefix):
32 continue
33
34 value = os.environ[key]
35
36 try:
37 value = loads(value)
38 except Exception:
39 # Keep the value as a string if loading failed.
40 pass
41
42 # Change to key.removeprefix(prefix) on Python >= 3.9.
43 key = key[len_prefix:]
44
45 if "__" not in key:
46 # A non-nested key, set directly.
47 self[key] = value
48 continue
49
50 # Traverse nested dictionaries with keys separated by "__".
51 current = self
52 *parts, tail = key.split("__")
53
54 for part in parts:
55 # If an intermediate dict does not exist, create it.
56 if part not in current:
57 current[part] = {}
58
59 current = current[part]
60
61 current[tail] = value
62
63 return True
Path 2: 1 calls (0.33)
'FLASK' (1)
loads def (1)
True (1)
JSONDecodeError (1)
1def from_prefixed_env(
2 self, prefix: str = "FLASK", *, loads: t.Callable[[str], t.Any] = json.loads
3 ) -> bool:
4 """Load any environment variables that start with ``FLASK_``,
5 dropping the prefix from the env key for the config key. Values
6 are passed through a loading function to attempt to convert them
7 to more specific types than strings.
8
9 Keys are loaded in :func:`sorted` order.
10
11 The default loading function attempts to parse values as any
12 valid JSON type, including dicts and lists.
13
14 Specific items in nested dicts can be set by separating the
15 keys with double underscores (``__``). If an intermediate key
16 doesn't exist, it will be initialized to an empty dict.
17
18 :param prefix: Load env vars that start with this prefix,
19 separated with an underscore (``_``).
20 :param loads: Pass each string value to this function and use
21 the returned value as the config value. If any error is
22 raised it is ignored and the value remains a string. The
23 default is :func:`json.loads`.
24
25 .. versionadded:: 2.1
26 """
27 prefix = f"{prefix}_"
28 len_prefix = len(prefix)
29
30 for key in sorted(os.environ):
31 if not key.startswith(prefix):
32 continue
33
34 value = os.environ[key]
35
36 try:
37 value = loads(value)
38 except Exception:
39 # Keep the value as a string if loading failed.
40 pass
41
42 # Change to key.removeprefix(prefix) on Python >= 3.9.
43 key = key[len_prefix:]
44
45 if "__" not in key:
46 # A non-nested key, set directly.
47 self[key] = value
48 continue
49
50 # Traverse nested dictionaries with keys separated by "__".
51 current = self
52 *parts, tail = key.split("__")
53
54 for part in parts:
55 # If an intermediate dict does not exist, create it.
56 if part not in current:
57 current[part] = {}
58
59 current = current[part]
60
61 current[tail] = value
62
63 return True