Skip to content

Commit e984f18

Browse files
committed
Add support for adding request contextual data to configuration objects.
This change adds a small namespace to configuraton objects that behaves like a dictonary allowing objects to made available to e.g. request handlers. Such functionality it needed to support templates without restorting to globals within the current system architecture; only congifuration objects are already threaded through most of the places this would need to be available with the correct semantics of an instance being created wherever it is needed.
1 parent e2eab5b commit e984f18

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

mig/shared/configuration.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ def __init__(self, config_file, verbose=False, skip_log=False,
761761
self.default_page = None
762762
self.auth_logger_obj = None
763763
self.gdp_logger_obj = None
764+
self._context = None
764765

765766
configuration_options = copy.deepcopy(_CONFIGURATION_DEFAULTS)
766767

@@ -772,6 +773,26 @@ def __init__(self, config_file, verbose=False, skip_log=False,
772773
disable_auth_log=disable_auth_log,
773774
_config_file=config_file)
774775

776+
def context(self, namespace=None):
777+
"""Retrieve the context or a previously registered namespace.
778+
"""
779+
780+
if self._context is None:
781+
self._context = {}
782+
if namespace is None:
783+
return self._context
784+
# allow the KeyError to escape if the registered namespace is missing
785+
return self._context[namespace]
786+
787+
def context_set(self, value, namespace=None):
788+
"""Attach a value as named namespace within the active congifuration.
789+
"""
790+
assert namespace is not None
791+
792+
context = self.context()
793+
context[namespace] = value
794+
return value
795+
775796
def reload_config(self, verbose, skip_log=False, disable_auth_log=False,
776797
_config_file=None):
777798
"""Re-read and parse configuration file. Optional skip_log arg

tests/test_mig_shared_configuration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _is_method(value):
4242

4343
def _to_dict(obj):
4444
return {k: v for k, v in inspect.getmembers(obj)
45-
if not (k.startswith('__') or _is_method(v))}
45+
if not (k.startswith('_') or _is_method(v))}
4646

4747

4848
class MigSharedConfiguration(MigTestCase):

0 commit comments

Comments
 (0)