Skip to content
Myr

Merge branch 'config-from-json' of https://github.com/mattupstate/flask into mattupstate-config-from-json

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Daniel Neuhäuser · committed by Daniel Neuhäuser · parents 98754b0, b290bf4 · Browse files · 3 files+50 added−0 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+50 added−0 removed

  • other:+50 added−0 removed

3 files+50 added−0 removed

added:flask/testsuite/static/config.json+4 added
modified:flask/testsuite/config.py+19 added
39 unmodified lines
40 app = flask.Flask(__name__)41 app.config.from_object(__name__)42 self.common_object_test(app)4344 def test_config_from_json(self):45 app = flask.Flask(__name__)46 current_dir = os.path.dirname(os.path.abspath(__file__))47 app.config.from_json(os.path.join(current_dir, 'static', 'config.json'))48 self.common_object_test(app)4950 def test_config_from_class(self):51 class Base(object):52 TEST_KEY = 'foo'53 class Test(Base):
51 unmodified lines
105 else:106 self.assert_true(0, 'expected config')107 self.assert_false(app.config.from_pyfile('missing.cfg', silent=True))108109 def test_config_missing_json(self):110 app = flask.Flask(__name__)111 try:112 app.config.from_json('missing.json')113 except IOError as e:114 msg = str(e)115 self.assert_true(msg.startswith('[Errno 2] Unable to load configuration '116 'file (No such file or directory):'))117 self.assert_true(msg.endswith("missing.json'"))118 else:119 self.assert_true(0, 'expected config')120 self.assert_false(app.config.from_json('missing.json', silent=True))121122 def test_session_lifetime(self):123 app = flask.Flask(__name__)124 app.config['PERMANENT_SESSION_LIFETIME'] = 42125 self.assert_equal(app.permanent_session_lifetime.seconds, 42)
206 unmodified lines
modified:flask/config.py+27 added
13 unmodified lines
14import errno1516from werkzeug.utils import import_string17from ._compat import string_types18from . import json192021class ConfigAttribute(object):22 """Makes an attribute forward to the config"""
141 unmodified lines
164 for key in dir(obj):165 if key.isupper():166 self[key] = getattr(obj, key)167168 def from_json(self, filename, silent=False):169 """Updates the values in the config from a JSON file. This function170 behaves as if the JSON object was a dictionary and passed ot the171 :meth:`from_object` function.172173 :param filename: the filename of the JSON file. This can either be an174 absolute filename or a filename relative to the175 root path.176 :param silent: set to `True` if you want silent failure for missing177 files.178 """179 filename = os.path.join(self.root_path, filename)180181 try:182 with open(filename) as json_file:183 obj = json.loads(json_file.read())184 except IOError as e:185 if silent and e.errno in (errno.ENOENT, errno.EISDIR):186 return False187 e.strerror = 'Unable to load configuration file (%s)' % e.strerror188 raise189 for key in obj.keys():190 if key.isupper():191 self[key] = obj[key]192 return True193194 def __repr__(self):195 return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))