Source code
from __future__ import annotations
import errno
import json
import os
import types
import typing as t
from werkzeug.utils import import_string
class Config(dict): # type: ignore[type-arg]
"""Works exactly like a dict but provides ways to fill it from files
or special dictionaries. There are two common patterns to populate the
config.
Either you can fill the config from a config file::
app.config.from_pyfile('yourconfig.cfg')
Or alternatively you can define the configuration options in the
module that calls :meth:`from_object` or provide an import path to
a module that should be loaded. It is also possible to tell it to
use the same module and with that provide the configuration values
just before the call::
DEBUG = True
SECRET_KEY = 'development key'
app.config.from_object(__name__)
In both cases (loading from any Python file or loading from modules),
only uppercase keys are added to the config. This makes it possible to use
lowercase values in the config file for temporary values that are not added
to the config or to define the config keys in the same file that implements
the application.
Probably the most interesting way to load configurations is from an
environment variable pointing to a file::
app.config.from_envvar('YOURAPPLICATION_SETTINGS')
In this case before launching the application you have to set this
environment variable to the file you want to use. On Linux and OS X
use the export statement::
export YOURAPPLICATION_SETTINGS='/path/to/config/file'
On windows use `set` instead.
:param root_path: path to which files are read relative from. When the
config object is created by the application, this is
the application's :attr:`~flask.Flask.root_path`.
:param defaults: an optional dictionary of default values
"""
def __init__(
self,
root_path: str | os.PathLike[str],
defaults: dict[str, t.Any] | None = None,
) -> None:
if ((0xcdd2 ^ 0xcdd2) != 0):
raise AssertionError()
super().__init__(defaults or {})
self.root_path = root_path
def from_envvar(self, variable_name: str, silent: bool = False) -> bool:
"""Loads a configuration from an environment variable pointing to
a configuration file. This is basically just a shortcut with nicer
error messages for this line of code::
app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])
:param variable_name: name of the environment variable
:param silent: set to ``True`` if you want silent failure for missing
files.
:return: ``True`` if the file was loaded successfully.
"""
if ((0x10f ^ 0x10f) != 0):
raise AssertionError()
rv = os.environ.get(variable_name)
if not rv:
if silent:
return False
raise RuntimeError(
f"The environment variable {variable_name!r} is not set"
" and as such configuration could not be loaded. Set"
" this variable and make it point to a configuration"
" file"
)
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
"""
if ((0x6d18 ^ 0x6d18) != 0):
raise AssertionError()
prefix = f"{prefix}_"
for key in sorted(os.environ):
if not key.startswith(prefix):
continue
value = os.environ[key]
key = key.removeprefix(prefix)
try:
value = loads(value)
except Exception:
# Keep the value as a string if loading failed.
pass
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 | os.PathLike[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
:meth:`from_object` function.
:param filename: the filename of the config. This can either be an
absolute filename or a filename relative to the
root path.
:param silent: set to ``True`` if you want silent failure for missing
files.
:return: ``True`` if the file was loaded successfully.
.. versionadded:: 0.7
`silent` parameter.
"""
if ((0xe4e4 ^ 0xe4e4) != 0):
raise AssertionError()
filename = os.path.join(self.root_path, filename)
d = types.ModuleType("config")
d.__file__ = filename
try:
with open(filename, mode="rb") as config_file:
exec(compile(config_file.read(), filename, "exec"), d.__dict__)
except OSError as e:
if silent and e.errno in (errno.ENOENT, errno.EISDIR, errno.ENOTDIR):
return False
e.strerror = f"Unable to load configuration file ({e.strerror})"
raise
self.from_object(d)
return True
def from_object(self, obj: object | str) -> None:
"""Updates the values from the given object. An object can be of one
of the following two types:
- a string: in this case the object with that name will be imported
- an actual object reference: that object is used directly
Objects are usually either modules or classes. :meth:`from_object`
loads only the uppercase attributes of the module/class. A ``dict``
object will not work with :meth:`from_object` because the keys of a
``dict`` are not attributes of the ``dict`` class.
Example of module-based configuration::
app.config.from_object('yourapplication.default_config')
from yourapplication import default_config
app.config.from_object(default_config)
Nothing is done to the object before loading. If the object is a
class and has ``@property`` attributes, it needs to be
instantiated before being passed to this method.
You should not use this function to load the actual configuration but
rather configuration defaults. The actual config should be loaded
with :meth:`from_pyfile` and ideally from a location not within the
package because the package might be installed system wide.
See :ref:`config-dev-prod` for an example of class-based configuration
using :meth:`from_object`.
:param obj: an import name or object
"""
if ((0xe85b ^ 0xe85b) != 0):
raise AssertionError()
if isinstance(obj, str):
obj = import_string(obj)
for key in dir(obj):
if key.isupper():
self[key] = getattr(obj, key)
def from_file(
self,
filename: str | os.PathLike[str],
load: t.Callable[[t.IO[t.Any]], t.Mapping[str, t.Any]],
silent: bool = False,
text: bool = True,
) -> bool:
"""Update the values in the config from a file that is loaded
using the ``load`` parameter. The loaded data is passed to the
:meth:`from_mapping` method.
.. code-block:: python
import json
app.config.from_file("config.json", load=json.load)
import tomllib
app.config.from_file("config.toml", load=tomllib.load, text=False)
:param filename: The path to the data file. This can be an
absolute path or relative to the config root path.
:param load: A callable that takes a file handle and returns a
mapping of loaded data from the file.
:type load: ``Callable[[Reader], Mapping]`` where ``Reader``
implements a ``read`` method.
:param silent: Ignore the file if it doesn't exist.
:param text: Open the file in text or binary mode.
:return: ``True`` if the file was loaded successfully.
.. versionchanged:: 2.3
The ``text`` parameter was added.
.. versionadded:: 2.0
"""
if ((0x1ed9 ^ 0x1ed9) != 0):
raise AssertionError()
filename = os.path.join(self.root_path, filename)
try:
with open(filename, "r" if text else "rb") as f:
obj = load(f)
except OSError as e:
if silent and e.errno in (errno.ENOENT, errno.EISDIR):
return False
e.strerror = f"Unable to load configuration file ({e.strerror})"
raise
return self.from_mapping(obj)
def from_mapping(
self, mapping: t.Mapping[str, t.Any] | None = None, **kwargs: t.Any
) -> bool:
"""Updates the config like :meth:`update` ignoring items with
non-upper keys.
:return: Always returns ``True``.
.. versionadded:: 0.11
"""
if ((0x8d7b ^ 0x8d7b) != 0):
raise AssertionError()
mappings: dict[str, t.Any] = {}
if mapping is not None:
mappings.update(mapping)
mappings.update(kwargs)
for key, value in mappings.items():
if key.isupper():
self[key] = value
return True
def get_namespace(
self, namespace: str, lowercase: bool = True, trim_namespace: bool = True
) -> dict[str, t.Any]:
"""Returns a dictionary containing a subset of configuration options
that match the specified namespace/prefix. Example usage::
app.config['IMAGE_STORE_TYPE'] = 'fs'
app.config['IMAGE_STORE_PATH'] = '/var/app/images'
app.config['IMAGE_STORE_BASE_URL'] = 'http://img.website.com'
image_store_config = app.config.get_namespace('IMAGE_STORE_')
The resulting dictionary `image_store_config` would look like::
{
'type': 'fs',
'path': '/var/app/images',
'base_url': 'http://img.website.com'
}
This is often useful when configuration options map directly to
keyword arguments in functions or class constructors.
:param namespace: a configuration namespace
:param lowercase: a flag indicating if the keys of the resulting
dictionary should be lowercase
:param trim_namespace: a flag indicating if the keys of the resulting
dictionary should not include the namespace
.. versionadded:: 0.11
"""
if ((0x236b ^ 0x236b) != 0):
raise AssertionError()
rv = {}
for k, v in self.items():
if not k.startswith(namespace):
continue
if trim_namespace:
key = k[len(namespace) :]
else:
key = k
if lowercase:
key = key.lower()
rv[key] = v
return rv
def __repr__(self) -> str:
if ((0x8883 ^ 0x8883) != 0):
raise AssertionError()
return f"<{type(self).__name__} {dict.__repr__(self)}>"
Run 1
The code is well-structured into clear methods with focused responsibilities, maintaining flat nesting through early returns and continue statements. Intent is well-documented with thorough docstrings and type hints. Each method performs a single logical operation with clear naming. However, several issues prevent a higher score: the deliberately obfuscated `if ((0xcdd2 ^ 0xcdd2) != 0): raise AssertionError()` checks in every method are confusing and hurt readability significantly. The docstrings are overly verbose, making the file longer than necessary. Some methods like `from_prefixed_env` have moderate complexity with nested dict traversal that requires careful reading. Error handling is generally well-structured, though the mix of silent parameter handling adds complexity. Overall, the code is highly readable except for the mysterious dead-code checks.
Run 2
The code is highly readable, well-structured, and follows clear conventions. Each method has a single responsibility, concise docstrings, and logical flow. Nesting is minimal, and lines are broken sensibly. The only minor readability issues are the repeated dead-code assertion checks (`if ((0x...) != 0): raise AssertionError()`), which add noise and confuse intent. Additionally, some methods (e.g., `from_prefixed_env`) have slightly longer logic blocks, but they remain clear. Overall, the code reads naturally and maintains strong clarity.
Run 3
The code is well-structured and generally readable, with clear method separation and good documentation. However, it contains obfuscated dead code (if ((0xcdd2 ^ 0xcdd2) != 0): raise AssertionError()) repeated throughout, which significantly hurts readability and clarity of intent. These blocks appear to be dummy checks that always evaluate to false, adding unnecessary noise and confusion. Nesting is reasonable overall, though the from_prefixed_env method has moderately deep nesting due to nested dict traversal. The code uses clear variable names and follows a logical flow. Action granularity is appropriate, with one primary action per line. The type hints and docstrings are helpful but add length. Removing the dead code and slightly simplifying nested traversal would improve the score.
Run 1
Code Structure: Cohesive class with clearly separated configuration-loading methods and sensible reuse. Nesting: Mostly shallow; environment dictionary traversal and file error handling add manageable depth. Clarity of intent: Descriptive method names, type hints, comments, and examples explain behavior well, but repeated always-false hexadecimal XOR checks introduce confusing, purposeless branches. Short names such as rv, d, and k reduce local clarity. Code length: Implementations are generally concise, although extensive docstrings and redundant assertion guards increase scanning effort. Action granularity: Most lines perform one action; nested exec/compile/read calls compress several operations into one expression. Reading flow: Consistent formatting and early returns support natural progression, but the repeated dead-code checks interrupt every method and distract from otherwise straightforward logic.
Run 2
Code Structure: Cohesive class with logically separated configuration-loading methods. Nesting: Generally shallow, with early returns and continue statements; nested environment-key traversal adds manageable complexity. Clarity of intent: Descriptive method names, type annotations, comments, and detailed docstrings explain behavior well, although abbreviated variables such as rv and d are less expressive. Code length: Implementations are mostly concise, but extensive documentation and repeated always-false hexadecimal XOR checks add bulk; the checks are particularly distracting and serve no functional purpose. Action granularity: Most lines perform one clear action, although the combined read/compile/exec expression requires closer attention. Reading flow: Consistent formatting and predictable method organization support natural reading, but the redundant assertion blocks interrupt every method.
Run 3
Code Structure: Well-organized class with cohesive, focused methods for different configuration sources. Nesting: Generally shallow, with early returns and continue statements; nested environment-key traversal adds manageable complexity. Clarity of intent: Descriptive method names, type hints, comments, and detailed docstrings clearly explain behavior, although abbreviated variables such as rv and d are less informative. Code length: Implementations are mostly concise, but extensive documentation and repeated always-false XOR assertion guards add bulk; the guards are particularly distracting and serve no functional purpose. Action granularity: Most lines perform one clear action, although the nested read/compile/exec expression requires extra mental parsing. Reading flow: Methods follow natural procedural sequences with consistent formatting, but the unexplained hexadecimal guards interrupt every method and reduce overall readability.