Skip to content
Myr

Merge pull request #1213 from defuz/config-patch

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

Add trim_namespace argument for app.config.get_namespace (with tests)

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 7b3e52b, 5e8d503 · Browse files · 2 files+15 added−2 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+15 added−2 removed

  • tests:+8 added−0 removed
  • other:+7 added−2 removed

2 files+15 added−2 removed

modified:flask/config.py+7 added-2 removed
211 unmodified lines
212 if key.isupper():213 self[key] = value214 return True215216 def get_namespace(self, namespace, lowercase=True):216 def get_namespace(self, namespace, lowercase=True, trim_namespace=True):217 """Returns a dictionary containing a subset of configuration options218 that match the specified namespace/prefix. Example usage::219220 app.config['IMAGE_STORE_TYPE'] = 'fs'
14 unmodified lines
235236 :param namespace: a configuration namespace237 :param lowercase: a flag indicating if the keys of the resulting238 dictionary should be lowercase239 :param trim_namespace: a flag indicating if the keys of the resulting240 dictionary should not include the namespace241242 .. versionadded:: 1.0243 """244 rv = {}245 for k, v in iteritems(self):246 if not k.startswith(namespace):247 continue248 if trim_namespace:246 key = k[len(namespace):]249 key = k[len(namespace):]250 else:251 key = k252 if lowercase:253 key = key.lower()254 rv[key] = v255 return rv
3 unmodified lines
modified:tests/test_config.py+8 added
180 unmodified lines
181 bar_options = app.config.get_namespace('BAR_', lowercase=False)182 assert 2 == len(bar_options)183 assert 'bar stuff 1' == bar_options['STUFF_1']184 assert 'bar stuff 2' == bar_options['STUFF_2']185 foo_options = app.config.get_namespace('FOO_', trim_namespace=False)186 assert 2 == len(foo_options)187 assert 'foo option 1' == foo_options['foo_option_1']188 assert 'foo option 2' == foo_options['foo_option_2']189 bar_options = app.config.get_namespace('BAR_', lowercase=False, trim_namespace=False)190 assert 2 == len(bar_options)191 assert 'bar stuff 1' == bar_options['BAR_STUFF_1']192 assert 'bar stuff 2' == bar_options['BAR_STUFF_2']