forked from orbit-oss/flask
Merge pull request #1213 from defuz/config-patch
Add trim_namespace argument for app.config.get_namespace (with tests)
This commit is contained in:
commit
fe2d75e1f4
2 changed files with 15 additions and 2 deletions
|
|
@ -213,7 +213,7 @@ class Config(dict):
|
||||||
self[key] = value
|
self[key] = value
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def get_namespace(self, namespace, lowercase=True):
|
def get_namespace(self, namespace, lowercase=True, trim_namespace=True):
|
||||||
"""Returns a dictionary containing a subset of configuration options
|
"""Returns a dictionary containing a subset of configuration options
|
||||||
that match the specified namespace/prefix. Example usage::
|
that match the specified namespace/prefix. Example usage::
|
||||||
|
|
||||||
|
|
@ -236,6 +236,8 @@ class Config(dict):
|
||||||
:param namespace: a configuration namespace
|
:param namespace: a configuration namespace
|
||||||
:param lowercase: a flag indicating if the keys of the resulting
|
:param lowercase: a flag indicating if the keys of the resulting
|
||||||
dictionary should be lowercase
|
dictionary should be lowercase
|
||||||
|
:param trim_namespace: a flag indicating if the keys of the resulting
|
||||||
|
dictionary should not include the namespace
|
||||||
|
|
||||||
.. versionadded:: 1.0
|
.. versionadded:: 1.0
|
||||||
"""
|
"""
|
||||||
|
|
@ -243,7 +245,10 @@ class Config(dict):
|
||||||
for k, v in iteritems(self):
|
for k, v in iteritems(self):
|
||||||
if not k.startswith(namespace):
|
if not k.startswith(namespace):
|
||||||
continue
|
continue
|
||||||
key = k[len(namespace):]
|
if trim_namespace:
|
||||||
|
key = k[len(namespace):]
|
||||||
|
else:
|
||||||
|
key = k
|
||||||
if lowercase:
|
if lowercase:
|
||||||
key = key.lower()
|
key = key.lower()
|
||||||
rv[key] = v
|
rv[key] = v
|
||||||
|
|
|
||||||
|
|
@ -182,3 +182,11 @@ def test_get_namespace():
|
||||||
assert 2 == len(bar_options)
|
assert 2 == len(bar_options)
|
||||||
assert 'bar stuff 1' == bar_options['STUFF_1']
|
assert 'bar stuff 1' == bar_options['STUFF_1']
|
||||||
assert 'bar stuff 2' == bar_options['STUFF_2']
|
assert 'bar stuff 2' == bar_options['STUFF_2']
|
||||||
|
foo_options = app.config.get_namespace('FOO_', trim_namespace=False)
|
||||||
|
assert 2 == len(foo_options)
|
||||||
|
assert 'foo option 1' == foo_options['foo_option_1']
|
||||||
|
assert 'foo option 2' == foo_options['foo_option_2']
|
||||||
|
bar_options = app.config.get_namespace('BAR_', lowercase=False, trim_namespace=False)
|
||||||
|
assert 2 == len(bar_options)
|
||||||
|
assert 'bar stuff 1' == bar_options['BAR_STUFF_1']
|
||||||
|
assert 'bar stuff 2' == bar_options['BAR_STUFF_2']
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue