Skip to content
Myr

clean up config.from_file docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 829aa65 · Browse files · 3 files+54 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+54 added−38 removed

  • other:+54 added−38 removed

3 files+54 added−38 removed

modified:docs/config.rst+16 added-10 removed
438 unmodified lines
439methods on the config object as well to load from individual files. For a440complete reference, read the :class:`~flask.Config` object's441documentation.442443Configuring from files444----------------------443446It is also possible to load configure from a flat file in a format of447your choice, for example to load from a TOML (or JSON) formatted448file::444Configuring from Data Files445---------------------------446450 import json451 import toml447It is also possible to load configuration from a file in a format of448your choice using :meth:`~flask.Config.from_file`. For example to load449from a TOML file:450453 app.config.from_file("config.toml", load=toml.load)454 # Alternatively, if you prefer JSON455 app.config.from_file("config.json", load=json.load)451.. code-block:: python452453 import toml454 app.config.from_file("config.toml", load=toml.load)455456Or from a JSON file:457458.. code-block:: python459460 import json461 app.config.from_file("config.json", load=json.load)462463464Configuring from Environment Variables465--------------------------------------466467In addition to pointing to configuration files using environment variables, you
250 unmodified lines
modified:src/flask/config.py+35 added-28 removed
13 unmodified lines
14import warnings1516from werkzeug.utils import import_string1718from . import json18from ._compat import iteritems19from ._compat import string_types2021
154 unmodified lines
176 if key.isupper():177 self[key] = getattr(obj, key)178179 def from_file(self, filename, load, silent=False):181 """Update the values in the config from a file that is loaded using182 the *load* argument. This method passes the loaded Mapping183 to the :meth:`from_mapping` function.180 """Update the values in the config from a file that is loaded181 using the ``load`` parameter. The loaded data is passed to the182 :meth:`from_mapping` method.183185 :param filename: the filename of the JSON file. This can either be an186 absolute filename or a filename relative to the187 root path.188 :param load: a callable that takes a file handle and returns a mapping189 from the file.190 :type load: Callable[[Reader], Mapping]. Where Reader is a Protocol191 that implements a read method.192 :param silent: set to ``True`` if you want silent failure for missing193 files.184 .. code-block:: python185186 import toml187 app.config.from_file("config.toml", load=toml.load)188189 :param filename: The path to the data file. This can be an190 absolute path or relative to the config root path.191 :param load: A callable that takes a file handle and returns a192 mapping of loaded data from the file.193 :type load: ``Callable[[Reader], Mapping]`` where ``Reader``194 implements a ``read`` method.195 :param silent: Ignore the file if it doesn't exist.196197 .. versionadded:: 1.2198 """199 filename = os.path.join(self.root_path, filename)200201 try:199 with open(filename) as file_:200 obj = load(file_)202 with open(filename) as f:203 obj = load(f)204 except IOError as e:205 if silent and e.errno in (errno.ENOENT, errno.EISDIR):206 return False207208 e.strerror = "Unable to load configuration file (%s)" % e.strerror209 raise210211 return self.from_mapping(obj)212213 def from_json(self, filename, silent=False):209 """Updates the values in the config from a JSON file. This function210 behaves as if the JSON object was a dictionary and passed to the211 :meth:`from_mapping` function.214 """Update the values in the config from a JSON file. The loaded215 data is passed to the :meth:`from_mapping` method.216213 :param filename: the filename of the JSON file. This can either be an214 absolute filename or a filename relative to the215 root path.216 :param silent: set to ``True`` if you want silent failure for missing217 files.217 :param filename: The path to the JSON file. This can be an218 absolute path or relative to the config root path.219 :param silent: Ignore the file if it doesn't exist.220221 .. deprecated:: 1.2222 Use :meth:`from_file` with :meth:`json.load` instead.223224 .. versionadded:: 0.11225 """226 warnings.warn(222 DeprecationWarning(223 '"from_json" is deprecated and will be removed in 2.0. Use'224 ' "from_file(filename, load=json.load)" instead.'225 )227 "'from_json' is deprecated and will be removed in 2.0."228 " Use 'from_file(filename, load=json.load)' instead.",229 DeprecationWarning,230 stacklevel=2,231 )227 return self.from_file(filename, json.load, silent=silent)232 from .json import load233234 return self.from_file(filename, load, silent=silent)235236 def from_mapping(self, *mapping, **kwargs):237 """Updates the config like :meth:`update` ignoring items with non-upper238 keys.239
62 unmodified lines
modified:CHANGES.rst+3 added
5 unmodified lines
6Unreleased78- Add :meth:`sessions.SessionInterface.get_cookie_name` to allow9 setting the session cookie name dynamically. :pr:`3369`10- Add :meth:`Config.from_file` to load config using arbitrary file11 loaders, such as ``toml.load`` or ``json.load``.12 :meth:`Config.from_json` is deprecated in favor of this. :pr:`3398`131415Version 1.1.216-------------
1004 unmodified lines