forked from orbit-oss/flask
silent option added to 'from_pyfile' to mirror 'from_envvar'.
This commit is contained in:
parent
b3fc9eb36b
commit
5cc40f4728
1 changed files with 8 additions and 3 deletions
|
|
@ -83,13 +83,13 @@ class Config(dict):
|
||||||
|
|
||||||
def from_envvar(self, variable_name, silent=False):
|
def from_envvar(self, variable_name, silent=False):
|
||||||
"""Loads a configuration from an environment variable pointing to
|
"""Loads a configuration from an environment variable pointing to
|
||||||
a configuration file. This basically is just a shortcut with nicer
|
a configuration file. This is basically just a shortcut with nicer
|
||||||
error messages for this line of code::
|
error messages for this line of code::
|
||||||
|
|
||||||
app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])
|
app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])
|
||||||
|
|
||||||
:param variable_name: name of the environment variable
|
:param variable_name: name of the environment variable
|
||||||
:param silent: set to `True` if you want silent failing for missing
|
:param silent: set to `True` if you want silent to fail for missing
|
||||||
files.
|
files.
|
||||||
:return: bool. `True` if able to load config, `False` otherwise.
|
:return: bool. `True` if able to load config, `False` otherwise.
|
||||||
"""
|
"""
|
||||||
|
|
@ -105,7 +105,7 @@ class Config(dict):
|
||||||
self.from_pyfile(rv)
|
self.from_pyfile(rv)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def from_pyfile(self, filename):
|
def from_pyfile(self, filename, silent=False):
|
||||||
"""Updates the values in the config from a Python file. This function
|
"""Updates the values in the config from a Python file. This function
|
||||||
behaves as if the file was imported as module with the
|
behaves as if the file was imported as module with the
|
||||||
:meth:`from_object` function.
|
:meth:`from_object` function.
|
||||||
|
|
@ -113,6 +113,8 @@ class Config(dict):
|
||||||
:param filename: the filename of the config. This can either be an
|
:param filename: the filename of the config. This can either be an
|
||||||
absolute filename or a filename relative to the
|
absolute filename or a filename relative to the
|
||||||
root path.
|
root path.
|
||||||
|
:param silent: set to `True` if you want silent to fail for missing
|
||||||
|
files.
|
||||||
"""
|
"""
|
||||||
filename = os.path.join(self.root_path, filename)
|
filename = os.path.join(self.root_path, filename)
|
||||||
d = imp.new_module('config')
|
d = imp.new_module('config')
|
||||||
|
|
@ -120,9 +122,12 @@ class Config(dict):
|
||||||
try:
|
try:
|
||||||
execfile(filename, d.__dict__)
|
execfile(filename, d.__dict__)
|
||||||
except IOError, e:
|
except IOError, e:
|
||||||
|
if silent and e.errno in (errno.ENOENT, errno.EISDIR):
|
||||||
|
return False
|
||||||
e.strerror = 'Unable to load configuration file (%s)' % e.strerror
|
e.strerror = 'Unable to load configuration file (%s)' % e.strerror
|
||||||
raise
|
raise
|
||||||
self.from_object(d)
|
self.from_object(d)
|
||||||
|
return True
|
||||||
|
|
||||||
def from_object(self, obj):
|
def from_object(self, obj):
|
||||||
"""Updates the values from the given object. An object can be of one
|
"""Updates the values from the given object. An object can be of one
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue