Skip to content

Commit 5cc40f4

Browse files
committed
silent option added to 'from_pyfile' to mirror 'from_envvar'.
1 parent b3fc9eb commit 5cc40f4

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

flask/config.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ def __init__(self, root_path, defaults=None):
8383

8484
def from_envvar(self, variable_name, silent=False):
8585
"""Loads a configuration from an environment variable pointing to
86-
a configuration file. This basically is just a shortcut with nicer
86+
a configuration file. This is basically just a shortcut with nicer
8787
error messages for this line of code::
8888
8989
app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])
9090
9191
:param variable_name: name of the environment variable
92-
:param silent: set to `True` if you want silent failing for missing
92+
:param silent: set to `True` if you want silent to fail for missing
9393
files.
9494
:return: bool. `True` if able to load config, `False` otherwise.
9595
"""
@@ -105,24 +105,29 @@ def from_envvar(self, variable_name, silent=False):
105105
self.from_pyfile(rv)
106106
return True
107107

108-
def from_pyfile(self, filename):
108+
def from_pyfile(self, filename, silent=False):
109109
"""Updates the values in the config from a Python file. This function
110110
behaves as if the file was imported as module with the
111111
:meth:`from_object` function.
112112
113113
:param filename: the filename of the config. This can either be an
114114
absolute filename or a filename relative to the
115115
root path.
116+
:param silent: set to `True` if you want silent to fail for missing
117+
files.
116118
"""
117119
filename = os.path.join(self.root_path, filename)
118120
d = imp.new_module('config')
119121
d.__file__ = filename
120122
try:
121123
execfile(filename, d.__dict__)
122124
except IOError, e:
125+
if silent and e.errno in (errno.ENOENT, errno.EISDIR):
126+
return False
123127
e.strerror = 'Unable to load configuration file (%s)' % e.strerror
124128
raise
125129
self.from_object(d)
130+
return True
126131

127132
def from_object(self, obj):
128133
"""Updates the values from the given object. An object can be of one

0 commit comments

Comments
 (0)