Skip to content

Commit db80968

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 ba904c2 commit db80968

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
@@ -759,6 +759,7 @@ def __init__(self, config_file, verbose=False, skip_log=False,
759759
self.default_page = None
760760
self.auth_logger_obj = None
761761
self.gdp_logger_obj = None
762+
self._context = None
762763

763764
configuration_options = copy.deepcopy(_CONFIGURATION_DEFAULTS)
764765

@@ -770,6 +771,26 @@ def __init__(self, config_file, verbose=False, skip_log=False,
770771
disable_auth_log=disable_auth_log,
771772
_config_file=config_file)
772773

774+
def context(self, namespace=None):
775+
"""Retrieve the context or a previously registered namespace.
776+
"""
777+
778+
if self._context is None:
779+
self._context = {}
780+
if namespace is None:
781+
return self._context
782+
# allow the KeyError to escape if the registered namespace is missing
783+
return self._context[namespace]
784+
785+
def context_set(self, value, namespace=None):
786+
"""Attach a value as named namespace within the active congifuration.
787+
"""
788+
assert namespace is not None
789+
790+
context = self.context()
791+
context[namespace] = value
792+
return value
793+
773794
def reload_config(self, verbose, skip_log=False, disable_auth_log=False,
774795
_config_file=None):
775796
"""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)