Add ability to config from a JSON file
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Matt Wright · committed by Matt Wright · parent 1b08d52 · 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
b290bf4- Tree
19c303b- Author
Matt Wright <matt@nobien.net>- Committer
Matt Wright <matt@nobien.net>- Parent
1b08d52
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
38 unmodified lines39 app = flask.Flask(__name__)40 app.config.from_object(__name__)41 self.common_object_test(app)42 43+ def test_config_from_json(self):44+ app = flask.Flask(__name__)45+ current_dir = os.path.dirname(os.path.abspath(__file__))46+ app.config.from_json(os.path.join(current_dir, 'static', 'config.json'))47+ self.common_object_test(app)48+49 def test_config_from_class(self):50 class Base(object):51 TEST_KEY = 'foo'52 class Test(Base):51 unmodified lines104 else:105 self.assert_true(0, 'expected config')106 self.assert_false(app.config.from_pyfile('missing.cfg', silent=True))107 108+ def test_config_missing_json(self):109+ app = flask.Flask(__name__)110+ try:111+ app.config.from_json('missing.json')112+ except IOError as e:113+ msg = str(e)114+ self.assert_true(msg.startswith('[Errno 2] Unable to load configuration '115+ 'file (No such file or directory):'))116+ self.assert_true(msg.endswith("missing.json'"))117+ else:118+ self.assert_true(0, 'expected config')119+ self.assert_false(app.config.from_json('missing.json', silent=True))120+121 def test_session_lifetime(self):122 app = flask.Flask(__name__)123 app.config['PERMANENT_SESSION_LIFETIME'] = 42124 self.assert_equal(app.permanent_session_lifetime.seconds, 42)194 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))