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
Provenance
- Commit
95a1ce4- Tree
66f23f5- Author
Daniel Neuhäuser <dasdasich@gmail.com>- Committer
Daniel Neuhäuser <dasdasich@gmail.com>- Parent 1
98754b0- Parent 2
b290bf4
3 files changed+50 added−0 removed
added:flask/testsuite/static/config.json+4 added
1234{ "TEST_KEY": "foo", "SECRET_KEY": "devkey"}modified:flask/testsuite/config.py+19 added
39 unmodified lines40 app = flask.Flask(__name__)41 app.config.from_object(__name__)42 self.common_object_test(app)43 44+ 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)49+50 def test_config_from_class(self):51 class Base(object):52 TEST_KEY = 'foo'53 class Test(Base):51 unmodified lines105 else:106 self.assert_true(0, 'expected config')107 self.assert_false(app.config.from_pyfile('missing.cfg', silent=True))108 109+ 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))121+122 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 linesmodified:flask/config.py+27 added
13 unmodified lines14 import errno15 16 from werkzeug.utils import import_string17 from ._compat import string_types18+from . import json19 20 21 class ConfigAttribute(object):22 """Makes an attribute forward to the config"""141 unmodified lines164 for key in dir(obj):165 if key.isupper():166 self[key] = getattr(obj, key)167 168+ 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.172+173+ :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)180+181+ 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 True193+194 def __repr__(self):195 return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))