Skip to content

Commit

Permalink
Support loading configuration from text files
Browse files Browse the repository at this point in the history
TOML is a very popular format now, and is taking hold in the Python
ecosystem via pyproject.toml (among others). This allows toml config
files via,

    app.config.from_file("config.toml", toml.loads)

it also allows for any other file format whereby there is a loader
that takes a string and returns a mapping.
  • Loading branch information
pgjones authored and davidism committed Oct 18, 2019
1 parent 7df10cd commit 829aa65
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
18 changes: 16 additions & 2 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ The following configuration values are used internally by Flask:
Added :data:`MAX_COOKIE_SIZE` to control a warning from Werkzeug.


Configuring from Files
----------------------
Configuring from Python Files
-----------------------------

Configuration becomes more useful if you can store it in a separate file,
ideally located outside the actual application package. This makes
Expand Down Expand Up @@ -440,6 +440,20 @@ methods on the config object as well to load from individual files. For a
complete reference, read the :class:`~flask.Config` object's
documentation.

Configuring from files
----------------------

It is also possible to load configure from a flat file in a format of
your choice, for example to load from a TOML (or JSON) formatted
file::

import json
import toml

app.config.from_file("config.toml", load=toml.load)
# Alternatively, if you prefer JSON
app.config.from_file("config.json", load=json.load)


Configuring from Environment Variables
--------------------------------------
Expand Down
41 changes: 33 additions & 8 deletions src/flask/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import errno
import os
import types
import warnings

from werkzeug.utils import import_string

Expand Down Expand Up @@ -176,31 +177,55 @@ class and has ``@property`` attributes, it needs to be
if key.isupper():
self[key] = getattr(obj, key)

def from_json(self, filename, silent=False):
"""Updates the values in the config from a JSON file. This function
behaves as if the JSON object was a dictionary and passed to the
:meth:`from_mapping` function.
def from_file(self, filename, load, silent=False):
"""Update the values in the config from a file that is loaded using
the *load* argument. This method passes the loaded Mapping
to the :meth:`from_mapping` function.
:param filename: the filename of the JSON file. This can either be an
absolute filename or a filename relative to the
root path.
:param load: a callable that takes a file handle and returns a mapping
from the file.
:type load: Callable[[Reader], Mapping]. Where Reader is a Protocol
that implements a read method.
:param silent: set to ``True`` if you want silent failure for missing
files.
.. versionadded:: 0.11
.. versionadded:: 1.2
"""
filename = os.path.join(self.root_path, filename)

try:
with open(filename) as json_file:
obj = json.loads(json_file.read())
with open(filename) as file_:
obj = load(file_)
except IOError as e:
if silent and e.errno in (errno.ENOENT, errno.EISDIR):
return False
e.strerror = "Unable to load configuration file (%s)" % e.strerror
raise
return self.from_mapping(obj)

def from_json(self, filename, silent=False):
"""Updates the values in the config from a JSON file. This function
behaves as if the JSON object was a dictionary and passed to the
:meth:`from_mapping` function.
:param filename: the filename of the JSON file. 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.
.. versionadded:: 0.11
"""
warnings.warn(
DeprecationWarning(
'"from_json" is deprecated and will be removed in 2.0. Use'
' "from_file(filename, load=json.load)" instead.'
)
)
return self.from_file(filename, json.load, silent=silent)

def from_mapping(self, *mapping, **kwargs):
"""Updates the config like :meth:`update` ignoring items with non-upper
keys.
Expand Down
13 changes: 7 additions & 6 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
:copyright: 2010 Pallets
:license: BSD-3-Clause
"""
import json
import os
import textwrap
from datetime import timedelta
Expand All @@ -27,7 +28,7 @@ def common_object_test(app):
assert "TestConfig" not in app.config


def test_config_from_file():
def test_config_from_pyfile():
app = flask.Flask(__name__)
app.config.from_pyfile(__file__.rsplit(".", 1)[0] + ".py")
common_object_test(app)
Expand All @@ -39,10 +40,10 @@ def test_config_from_object():
common_object_test(app)


def test_config_from_json():
def test_config_from_file():
app = flask.Flask(__name__)
current_dir = os.path.dirname(os.path.abspath(__file__))
app.config.from_json(os.path.join(current_dir, "static", "config.json"))
app.config.from_file(os.path.join(current_dir, "static", "config.json"), json.load)
common_object_test(app)


Expand Down Expand Up @@ -116,16 +117,16 @@ def test_config_missing():
assert not app.config.from_pyfile("missing.cfg", silent=True)


def test_config_missing_json():
def test_config_missing_file():
app = flask.Flask(__name__)
with pytest.raises(IOError) as e:
app.config.from_json("missing.json")
app.config.from_file("missing.json", load=json.load)
msg = str(e.value)
assert msg.startswith(
"[Errno 2] Unable to load configuration file (No such file or directory):"
)
assert msg.endswith("missing.json'")
assert not app.config.from_json("missing.json", silent=True)
assert not app.config.from_file("missing.json", load=json.load, silent=True)


def test_custom_config_class():
Expand Down

0 comments on commit 829aa65

Please sign in to comment.