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
Provenance
- Commit
aac0f58- Tree
9af0bb6- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
829aa65
3 files changed+54 added−38 removed
modified:docs/config.rst+16 added-10 removed
438 unmodified lines439 methods on the config object as well to load from individual files. For a440 complete reference, read the :class:`~flask.Config` object's441 documentation.442 443-Configuring from files444-----------------------443 446-It is also possible to load configure from a flat file in a format of447-your choice, for example to load from a TOML (or JSON) formatted448-file::444+Configuring from Data Files445+---------------------------446 450- import json451- import toml447+It is also possible to load configuration from a file in a format of448+your choice using :meth:`~flask.Config.from_file`. For example to load449+from a TOML file:450 453- 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:: python452 453+ import toml454+ app.config.from_file("config.toml", load=toml.load)455 456+Or from a JSON file:457+458+.. code-block:: python459+460+ import json461+ app.config.from_file("config.json", load=json.load)462+463+464 Configuring from Environment Variables465 --------------------------------------466 467 In addition to pointing to configuration files using environment variables, you250 unmodified linesmodified:src/flask/config.py+35 added-28 removed
13 unmodified lines14 import warnings15 16 from werkzeug.utils import import_string17 18-from . import json18 from ._compat import iteritems19 from ._compat import string_types20 21 154 unmodified lines176 if key.isupper():177 self[key] = getattr(obj, key)178 179 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.183 185- :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:: python185 186+ import toml187+ app.config.from_file("config.toml", load=toml.load)188+189+ :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.196+197 .. versionadded:: 1.2198 """199 filename = os.path.join(self.root_path, filename)200+201 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 False207+208 e.strerror = "Unable to load configuration file (%s)" % e.strerror209 raise210+211 return self.from_mapping(obj)212 213 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.216 213- :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.220 221+ .. deprecated:: 1.2222+ Use :meth:`from_file` with :meth:`json.load` instead.223+224 .. 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 load233 234+ return self.from_file(filename, load, silent=silent)235+236 def from_mapping(self, *mapping, **kwargs):237 """Updates the config like :meth:`update` ignoring items with non-upper238 keys.239 62 unmodified linesmodified:CHANGES.rst+3 added
5 unmodified lines6 Unreleased7 8 - 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`13 14 15 Version 1.1.216 -------------1004 unmodified lines