Skip to content

Commit 829aa65

Browse files
pgjonesdavidism
authored andcommitted
Support loading configuration from text files
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.
1 parent 7df10cd commit 829aa65

File tree

3 files changed

+56
-16
lines changed

3 files changed

+56
-16
lines changed

docs/config.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ The following configuration values are used internally by Flask:
393393
Added :data:`MAX_COOKIE_SIZE` to control a warning from Werkzeug.
394394

395395

396-
Configuring from Files
397-
----------------------
396+
Configuring from Python Files
397+
-----------------------------
398398

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

443+
Configuring from files
444+
----------------------
445+
446+
It is also possible to load configure from a flat file in a format of
447+
your choice, for example to load from a TOML (or JSON) formatted
448+
file::
449+
450+
import json
451+
import toml
452+
453+
app.config.from_file("config.toml", load=toml.load)
454+
# Alternatively, if you prefer JSON
455+
app.config.from_file("config.json", load=json.load)
456+
443457

444458
Configuring from Environment Variables
445459
--------------------------------------

src/flask/config.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import errno
1212
import os
1313
import types
14+
import warnings
1415

1516
from werkzeug.utils import import_string
1617

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

179-
def from_json(self, filename, silent=False):
180-
"""Updates the values in the config from a JSON file. This function
181-
behaves as if the JSON object was a dictionary and passed to the
182-
:meth:`from_mapping` function.
180+
def from_file(self, filename, load, silent=False):
181+
"""Update the values in the config from a file that is loaded using
182+
the *load* argument. This method passes the loaded Mapping
183+
to the :meth:`from_mapping` function.
183184
184185
:param filename: the filename of the JSON file. This can either be an
185186
absolute filename or a filename relative to the
186187
root path.
188+
:param load: a callable that takes a file handle and returns a mapping
189+
from the file.
190+
:type load: Callable[[Reader], Mapping]. Where Reader is a Protocol
191+
that implements a read method.
187192
:param silent: set to ``True`` if you want silent failure for missing
188193
files.
189194
190-
.. versionadded:: 0.11
195+
.. versionadded:: 1.2
191196
"""
192197
filename = os.path.join(self.root_path, filename)
193-
194198
try:
195-
with open(filename) as json_file:
196-
obj = json.loads(json_file.read())
199+
with open(filename) as file_:
200+
obj = load(file_)
197201
except IOError as e:
198202
if silent and e.errno in (errno.ENOENT, errno.EISDIR):
199203
return False
200204
e.strerror = "Unable to load configuration file (%s)" % e.strerror
201205
raise
202206
return self.from_mapping(obj)
203207

208+
def from_json(self, filename, silent=False):
209+
"""Updates the values in the config from a JSON file. This function
210+
behaves as if the JSON object was a dictionary and passed to the
211+
:meth:`from_mapping` function.
212+
213+
:param filename: the filename of the JSON file. This can either be an
214+
absolute filename or a filename relative to the
215+
root path.
216+
:param silent: set to ``True`` if you want silent failure for missing
217+
files.
218+
219+
.. versionadded:: 0.11
220+
"""
221+
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+
)
226+
)
227+
return self.from_file(filename, json.load, silent=silent)
228+
204229
def from_mapping(self, *mapping, **kwargs):
205230
"""Updates the config like :meth:`update` ignoring items with non-upper
206231
keys.

tests/test_config.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
:copyright: 2010 Pallets
77
:license: BSD-3-Clause
88
"""
9+
import json
910
import os
1011
import textwrap
1112
from datetime import timedelta
@@ -27,7 +28,7 @@ def common_object_test(app):
2728
assert "TestConfig" not in app.config
2829

2930

30-
def test_config_from_file():
31+
def test_config_from_pyfile():
3132
app = flask.Flask(__name__)
3233
app.config.from_pyfile(__file__.rsplit(".", 1)[0] + ".py")
3334
common_object_test(app)
@@ -39,10 +40,10 @@ def test_config_from_object():
3940
common_object_test(app)
4041

4142

42-
def test_config_from_json():
43+
def test_config_from_file():
4344
app = flask.Flask(__name__)
4445
current_dir = os.path.dirname(os.path.abspath(__file__))
45-
app.config.from_json(os.path.join(current_dir, "static", "config.json"))
46+
app.config.from_file(os.path.join(current_dir, "static", "config.json"), json.load)
4647
common_object_test(app)
4748

4849

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

118119

119-
def test_config_missing_json():
120+
def test_config_missing_file():
120121
app = flask.Flask(__name__)
121122
with pytest.raises(IOError) as e:
122-
app.config.from_json("missing.json")
123+
app.config.from_file("missing.json", load=json.load)
123124
msg = str(e.value)
124125
assert msg.startswith(
125126
"[Errno 2] Unable to load configuration file (No such file or directory):"
126127
)
127128
assert msg.endswith("missing.json'")
128-
assert not app.config.from_json("missing.json", silent=True)
129+
assert not app.config.from_file("missing.json", load=json.load, silent=True)
129130

130131

131132
def test_custom_config_class():

0 commit comments

Comments
 (0)