From 56a7e8361d978c9ae2981167f3b4d959f2cf21e2 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Fri, 19 Sep 2014 20:09:55 -0700 Subject: [PATCH 1/3] Update config.py I want to load all of my `CELERY_` options from the config and leave `CELERY_` on there. --- flask/config.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/flask/config.py b/flask/config.py index bb32f7a7..49d60907 100644 --- a/flask/config.py +++ b/flask/config.py @@ -213,7 +213,7 @@ class Config(dict): self[key] = value 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 that match the specified namespace/prefix. Example usage:: @@ -236,6 +236,8 @@ class Config(dict): :param namespace: a configuration namespace :param lowercase: a flag indicating if the keys of the resulting 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 """ @@ -243,7 +245,8 @@ class Config(dict): for k, v in iteritems(self): if not k.startswith(namespace): continue - key = k[len(namespace):] + if trim_namespace: + key = k[len(namespace):] if lowercase: key = key.lower() rv[key] = v From bbd9c2f10011be9f9e2b1857bc863d923459154f Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Fri, 19 Sep 2014 20:15:06 -0700 Subject: [PATCH 2/3] Update config.py --- flask/config.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/flask/config.py b/flask/config.py index 49d60907..6d7e8683 100644 --- a/flask/config.py +++ b/flask/config.py @@ -247,6 +247,8 @@ class Config(dict): continue if trim_namespace: key = k[len(namespace):] + else: + key = k if lowercase: key = key.lower() rv[key] = v From 5e8d503098747e0297f17978c23f5fd64106124b Mon Sep 17 00:00:00 2001 From: defuz Date: Fri, 24 Oct 2014 13:11:07 +0400 Subject: [PATCH 3/3] add tests for trim_namespace argument of app.config.get_namespace --- tests/test_config.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_config.py b/tests/test_config.py index 7487cb1d..edfe43cc 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -182,3 +182,11 @@ def test_get_namespace(): assert 2 == len(bar_options) assert 'bar stuff 1' == bar_options['STUFF_1'] 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']