Skip to content
Myr

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

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

Conflicts: flask/config.py

authored by Daniel Neuhäuser · committed by Daniel Neuhäuser · parents 2c2c240, 90a50f8 · 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

2 files+49 added−0 removed

modified:flask/testsuite/config.py+15 added
132 unmodified lines
133 app = flask.Flask(__name__)134 app.config['PERMANENT_SESSION_LIFETIME'] = 42135 self.assert_equal(app.permanent_session_lifetime.seconds, 42)136137 def test_get_namespace(self):138 app = flask.Flask(__name__)139 app.config['FOO_OPTION_1'] = 'foo option 1'140 app.config['FOO_OPTION_2'] = 'foo option 2'141 app.config['BAR_STUFF_1'] = 'bar stuff 1'142 app.config['BAR_STUFF_2'] = 'bar stuff 2'143 foo_options = app.config.get_namespace('FOO_')144 self.assert_equal(2, len(foo_options))145 self.assert_equal('foo option 1', foo_options['option_1'])146 self.assert_equal('foo option 2', foo_options['option_2'])147 bar_options = app.config.get_namespace('BAR_', lowercase=False)148 self.assert_equal(2, len(bar_options))149 self.assert_equal('bar stuff 1', bar_options['STUFF_1'])150 self.assert_equal('bar stuff 2', bar_options['STUFF_2'])151152153class LimitedLoaderMockWrapper(object):154 def __init__(self, loader):155 self.loader = loader156
200 unmodified lines
modified:flask/config.py+34 added
191 unmodified lines
192 if key.isupper():193 self[key] = obj[key]194 return True195196 def get_namespace(self, namespace, lowercase=True):197 """Returns a dictionary containing a subset of configuration options198 that match the specified namespace/prefix. Example usage::199200 app.config['IMAGE_STORE_TYPE'] = 'fs'201 app.config['IMAGE_STORE_PATH'] = '/var/app/images'202 app.config['IMAGE_STORE_BASE_URL'] = 'http://img.website.com'203 image_store_config = app.config.get_namespace('IMAGE_STORE_')204205 The resulting dictionary `image_store` would look like::206207 {208 'type': 'fs',209 'path': '/var/app/images',210 'base_url': 'http://img.website.com'211 }212213 This is often useful when configuration options map directly to214 keyword arguments in functions or class constructors.215216 :param namespace: a configuration namespace217 :param lowercase: a flag indicating if the keys of the resulting218 dictionary should be lowercase219 """220 rv = {}221 for k, v in self.iteritems():222 if not k.startswith(namespace):223 continue224 key = k[len(namespace):]225 if lowercase:226 key = key.lower()227 rv[key] = v228 return rv229230 def __repr__(self):231 return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))