aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Flask/py3/flask/config.py
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2024-05-03 00:01:03 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2024-05-03 00:06:37 +0300
commit38934cfb0b963a0157cf16c13499977ac87f35b9 (patch)
tree124d141726b8a860761a75fe2b597eecce078ee7 /contrib/python/Flask/py3/flask/config.py
parent616419d508d3ee4a3b07a82e5cae5ab4544bd1e2 (diff)
downloadydb-38934cfb0b963a0157cf16c13499977ac87f35b9.tar.gz
Intermediate changes
Diffstat (limited to 'contrib/python/Flask/py3/flask/config.py')
-rw-r--r--contrib/python/Flask/py3/flask/config.py96
1 files changed, 69 insertions, 27 deletions
diff --git a/contrib/python/Flask/py3/flask/config.py b/contrib/python/Flask/py3/flask/config.py
index ca769022f66..7b6a137ada2 100644
--- a/contrib/python/Flask/py3/flask/config.py
+++ b/contrib/python/Flask/py3/flask/config.py
@@ -1,4 +1,5 @@
import errno
+import json
import os
import types
import typing as t
@@ -70,7 +71,7 @@ class Config(dict):
"""
def __init__(self, root_path: str, defaults: t.Optional[dict] = None) -> None:
- dict.__init__(self, defaults or {})
+ super().__init__(defaults or {})
self.root_path = root_path
def from_envvar(self, variable_name: str, silent: bool = False) -> bool:
@@ -97,6 +98,70 @@ class Config(dict):
)
return self.from_pyfile(rv, silent=silent)
+ def from_prefixed_env(
+ self, prefix: str = "FLASK", *, loads: t.Callable[[str], t.Any] = json.loads
+ ) -> bool:
+ """Load any environment variables that start with ``FLASK_``,
+ dropping the prefix from the env key for the config key. Values
+ are passed through a loading function to attempt to convert them
+ to more specific types than strings.
+
+ Keys are loaded in :func:`sorted` order.
+
+ The default loading function attempts to parse values as any
+ valid JSON type, including dicts and lists.
+
+ Specific items in nested dicts can be set by separating the
+ keys with double underscores (``__``). If an intermediate key
+ doesn't exist, it will be initialized to an empty dict.
+
+ :param prefix: Load env vars that start with this prefix,
+ separated with an underscore (``_``).
+ :param loads: Pass each string value to this function and use
+ the returned value as the config value. If any error is
+ raised it is ignored and the value remains a string. The
+ default is :func:`json.loads`.
+
+ .. versionadded:: 2.1
+ """
+ prefix = f"{prefix}_"
+ len_prefix = len(prefix)
+
+ for key in sorted(os.environ):
+ if not key.startswith(prefix):
+ continue
+
+ value = os.environ[key]
+
+ try:
+ value = loads(value)
+ except Exception:
+ # Keep the value as a string if loading failed.
+ pass
+
+ # Change to key.removeprefix(prefix) on Python >= 3.9.
+ key = key[len_prefix:]
+
+ if "__" not in key:
+ # A non-nested key, set directly.
+ self[key] = value
+ continue
+
+ # Traverse nested dictionaries with keys separated by "__".
+ current = self
+ *parts, tail = key.split("__")
+
+ for part in parts:
+ # If an intermediate dict does not exist, create it.
+ if part not in current:
+ current[part] = {}
+
+ current = current[part]
+
+ current[tail] = value
+
+ return True
+
def from_pyfile(self, filename: str, silent: bool = False) -> bool:
"""Updates the values in the config from a Python file. This function
behaves as if the file was imported as module with the
@@ -176,6 +241,9 @@ class Config(dict):
.. code-block:: python
+ import json
+ app.config.from_file("config.json", load=json.load)
+
import toml
app.config.from_file("config.toml", load=toml.load)
@@ -204,32 +272,6 @@ class Config(dict):
return self.from_mapping(obj)
- def from_json(self, filename: str, silent: bool = False) -> bool:
- """Update the values in the config from a JSON file. The loaded
- data is passed to the :meth:`from_mapping` method.
-
- :param filename: The path to the JSON file. This can be an
- absolute path or relative to the config root path.
- :param silent: Ignore the file if it doesn't exist.
- :return: ``True`` if the file was loaded successfully.
-
- .. deprecated:: 2.0.0
- Will be removed in Flask 2.1. Use :meth:`from_file` instead.
- This was removed early in 2.0.0, was added back in 2.0.1.
-
- .. versionadded:: 0.11
- """
- import warnings
- from . import json
-
- warnings.warn(
- "'from_json' is deprecated and will be removed in Flask"
- " 2.1. Use 'from_file(path, json.load)' instead.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self.from_file(filename, json.load, silent=silent)
-
def from_mapping(
self, mapping: t.Optional[t.Mapping[str, t.Any]] = None, **kwargs: t.Any
) -> bool: