Add `get_namespace` method on `Config` object for convenient access of namespaced config options.
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 de7e2bc · Browse files · 2 files·+49 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
2 files+49 added−0 removed
- other:+49 added−0 removed
Provenance
- Commit
90a50f8- Tree
24dac73- Author
Matt Wright <matt@nobien.net>- Committer
Matt Wright <matt@nobien.net>- Parent
de7e2bc
2 files changed+49 added−0 removed
modified:flask/testsuite/config.py+15 added
103 unmodified lines104 app = flask.Flask(__name__)105 app.config['PERMANENT_SESSION_LIFETIME'] = 42106 self.assert_equal(app.permanent_session_lifetime.seconds, 42)107 108+ def test_get_namespace(self):109+ app = flask.Flask(__name__)110+ app.config['FOO_OPTION_1'] = 'foo option 1'111+ app.config['FOO_OPTION_2'] = 'foo option 2'112+ app.config['BAR_STUFF_1'] = 'bar stuff 1'113+ app.config['BAR_STUFF_2'] = 'bar stuff 2'114+ foo_options = app.config.get_namespace('FOO_')115+ self.assert_equal(2, len(foo_options))116+ self.assert_equal('foo option 1', foo_options['option_1'])117+ self.assert_equal('foo option 2', foo_options['option_2'])118+ bar_options = app.config.get_namespace('BAR_', lowercase=False)119+ self.assert_equal(2, len(bar_options))120+ self.assert_equal('bar stuff 1', bar_options['STUFF_1'])121+ self.assert_equal('bar stuff 2', bar_options['STUFF_2'])122 123+124 class LimitedLoaderMockWrapper(object):125 def __init__(self, loader):126 self.loader = loader127 200 unmodified linesmodified:flask/config.py+34 added
162 unmodified lines163 for key in dir(obj):164 if key.isupper():165 self[key] = getattr(obj, key)166 167+ def get_namespace(self, namespace, lowercase=True):168+ """Returns a dictionary containing a subset of configuration options169+ that match the specified namespace/prefix. Example usage::170+171+ app.config['IMAGE_STORE_TYPE'] = 'fs'172+ app.config['IMAGE_STORE_PATH'] = '/var/app/images'173+ app.config['IMAGE_STORE_BASE_URL'] = 'http://img.website.com'174+ image_store_config = app.config.get_namespace('IMAGE_STORE_')175+176+ The resulting dictionary `image_store` would look like::177+178+ {179+ 'type': 'fs',180+ 'path': '/var/app/images',181+ 'base_url': 'http://img.website.com'182+ }183+184+ This is often useful when configuration options map directly to185+ keyword arguments in functions or class constructors.186+187+ :param namespace: a configuration namespace188+ :param lowercase: a flag indicating if the keys of the resulting189+ dictionary should be lowercase190+ """191+ rv = {}192+ for k, v in self.iteritems():193+ if not k.startswith(namespace):194+ continue195+ key = k[len(namespace):]196+ if lowercase:197+ key = key.lower()198+ rv[key] = v199+ return rv200+201 def __repr__(self):202 return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))